Skip to content

Commit 1a18ff5

Browse files
committedMar 7, 2025
Merge rust-bitcoin#4127: units: Prevent casting pub enums as ints
97453ef units: Prevent casting pub enums as ints (Tobin C. Harding) Pull request description: 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`. ACKs for top commit: Kixunil: ACK 97453ef apoelstra: ACK 97453ef; successfully ran local tests Tree-SHA512: 2515152107fb21a2dbdef9b46308fef6bd45f4a9719da7a39149b3bdbce6a93dc0f98e112ac246eb32dbe4df1210d5e6328c26ea8678e3da15276e893b39cc9c
2 parents 5581c49 + 97453ef commit 1a18ff5

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed
 

‎units/src/amount/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ pub enum Denomination {
9191
Bit,
9292
/// satoshi (1 BTC = 100,000,000 satoshi).
9393
Satoshi,
94+
/// Stops users from casting this enum to an integer.
95+
// May get removed if one day Rust supports disabling casts natively.
96+
#[doc(hidden)]
97+
_DoNotUse(Infallible),
9498
}
9599

96100
impl Denomination {
@@ -109,6 +113,7 @@ impl Denomination {
109113
Denomination::MicroBitcoin => -2,
110114
Denomination::Bit => -2,
111115
Denomination::Satoshi => 0,
116+
Denomination::_DoNotUse(infallible) => match infallible {}
112117
}
113118
}
114119

@@ -121,6 +126,7 @@ impl Denomination {
121126
Denomination::MicroBitcoin => "uBTC",
122127
Denomination::Bit => "bits",
123128
Denomination::Satoshi => "satoshi",
129+
Denomination::_DoNotUse(infallible) => match infallible {}
124130
}
125131
}
126132

0 commit comments

Comments
 (0)
Please sign in to comment.