Skip to content

Commit ef92709

Browse files
committed
Fix amount whole bitcoin constructors
I royally botched the recent effort to make const amount constructors use a smaller type. I left in an unnecessary panic and forgot to do both of them. Note these function return values will change again very shortly when we start enforcing the MAX_MONEY invariant. However the 64 to 32 bit change is unrelated to that and is easier to review if done separately. Whole bitcoin can non in any sane environment be greater than 21,000,000 which fits in 32 bits so we can take a 32 bit integer in the whole bitcoin constructors without loss of utility. Doing so removes the potential panic. This is a breaking API change. We elect not to deprecate because we want to keep the same function names.
1 parent 1702fdd commit ef92709

2 files changed

Lines changed: 14 additions & 35 deletions

File tree

units/src/amount/signed.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,19 @@ impl SignedAmount {
121121
}
122122

123123
/// Converts from a value expressing a whole number of bitcoin to a [`SignedAmount`].
124-
///
125-
/// # Errors
126-
///
127-
/// The function errors if the argument multiplied by the number of sats
128-
/// per bitcoin overflows an `i64` type.
129-
pub fn from_int_btc<T: Into<i64>>(whole_bitcoin: T) -> Result<SignedAmount, OutOfRangeError> {
130-
match whole_bitcoin.into().checked_mul(100_000_000) {
131-
Some(amount) => Ok(Self::from_sat(amount)),
132-
None => Err(OutOfRangeError { is_signed: true, is_greater_than_max: true }),
133-
}
124+
#[allow(clippy::missing_panics_doc)]
125+
pub fn from_int_btc<T: Into<i32>>(whole_bitcoin: T) -> SignedAmount {
126+
SignedAmount::from_int_btc_const(whole_bitcoin.into())
134127
}
135128

136129
/// Converts from a value expressing a whole number of bitcoin to a [`SignedAmount`]
137130
/// in const context.
138-
///
139-
/// # Panics
140-
///
141-
/// The function panics if the argument multiplied by the number of sats
142-
/// per bitcoin overflows an `i64` type.
143-
pub const fn from_int_btc_const(whole_bitcoin: i64) -> SignedAmount {
144-
match whole_bitcoin.checked_mul(100_000_000) {
131+
#[allow(clippy::missing_panics_doc)]
132+
pub const fn from_int_btc_const(whole_bitcoin: i32) -> SignedAmount {
133+
let btc = whole_bitcoin as i64; // Can't call `into` in const context.
134+
match btc.checked_mul(100_000_000) {
145135
Some(amount) => SignedAmount::from_sat(amount),
146-
None => panic!("checked_mul overflowed"),
136+
None => panic!("cannot overflow in i64"),
147137
}
148138
}
149139

units/src/amount/unsigned.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,19 @@ impl Amount {
122122
}
123123

124124
/// Converts from a value expressing a whole number of bitcoin to an [`Amount`].
125-
///
126-
/// # Errors
127-
///
128-
/// The function errors if the argument multiplied by the number of sats
129-
/// per bitcoin overflows a `u64` type.
130-
pub fn from_int_btc<T: Into<u64>>(whole_bitcoin: T) -> Result<Amount, OutOfRangeError> {
131-
match whole_bitcoin.into().checked_mul(100_000_000) {
132-
Some(amount) => Ok(Self::from_sat(amount)),
133-
None => Err(OutOfRangeError { is_signed: false, is_greater_than_max: true }),
134-
}
125+
#[allow(clippy::missing_panics_doc)]
126+
pub fn from_int_btc<T: Into<u32>>(whole_bitcoin: T) -> Amount {
127+
Amount::from_int_btc_const(whole_bitcoin.into())
135128
}
136129

137130
/// Converts from a value expressing a whole number of bitcoin to an [`Amount`]
138131
/// in const context.
139-
///
140-
/// # Panics
141-
///
142-
/// The function panics if the argument multiplied by the number of sats
143-
/// per bitcoin overflows a `u64` type.
132+
#[allow(clippy::missing_panics_doc)]
144133
pub const fn from_int_btc_const(whole_bitcoin: u32) -> Amount {
145-
let btc = whole_bitcoin as u64; // Can't call u64::from in const context.
134+
let btc = whole_bitcoin as u64; // Can't call `into` in const context.
146135
match btc.checked_mul(100_000_000) {
147136
Some(amount) => Amount::from_sat(amount),
148-
None => panic!("checked_mul overflowed"),
137+
None => panic!("cannot overflow a u64"),
149138
}
150139
}
151140

0 commit comments

Comments
 (0)