units: Prevent casting pub enums as ints

A public enum with simple variants gets an automatic integer variant
that can be cast by library consumers. This puts a unnecessary
maintenance burden upon us because we cannot then add variants in the
middle of others.

Add a hidden variant to the single public non-error enum in `units`.
This commit is contained in:
Tobin C. Harding 2025-02-26 12:18:36 +11:00
parent 6483244280
commit 97453ef9bc
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 6 additions and 0 deletions

View File

@ -91,6 +91,10 @@ pub enum Denomination {
Bit, Bit,
/// satoshi (1 BTC = 100,000,000 satoshi). /// satoshi (1 BTC = 100,000,000 satoshi).
Satoshi, Satoshi,
/// Stops users from casting this enum to an integer.
// May get removed if one day Rust supports disabling casts natively.
#[doc(hidden)]
_DoNotUse(Infallible),
} }
impl Denomination { impl Denomination {
@ -109,6 +113,7 @@ impl Denomination {
Denomination::MicroBitcoin => -2, Denomination::MicroBitcoin => -2,
Denomination::Bit => -2, Denomination::Bit => -2,
Denomination::Satoshi => 0, Denomination::Satoshi => 0,
Denomination::_DoNotUse(infallible) => match infallible {}
} }
} }
@ -121,6 +126,7 @@ impl Denomination {
Denomination::MicroBitcoin => "uBTC", Denomination::MicroBitcoin => "uBTC",
Denomination::Bit => "bits", Denomination::Bit => "bits",
Denomination::Satoshi => "satoshi", Denomination::Satoshi => "satoshi",
Denomination::_DoNotUse(infallible) => match infallible {}
} }
} }