diff --git a/units/src/amount/signed.rs b/units/src/amount/signed.rs index 0ce121425..2ae36f715 100644 --- a/units/src/amount/signed.rs +++ b/units/src/amount/signed.rs @@ -80,8 +80,8 @@ impl SignedAmount { /// /// The function errors if the argument multiplied by the number of sats /// per bitcoin overflows an `i64` type. - pub fn from_int_btc(btc: i64) -> Result { - match btc.checked_mul(100_000_000) { + pub fn from_int_btc>(whole_bitcoin: T) -> Result { + match whole_bitcoin.into().checked_mul(100_000_000) { Some(amount) => Ok(SignedAmount::from_sat(amount)), None => Err(OutOfRangeError { is_signed: true, is_greater_than_max: true }), } @@ -94,8 +94,8 @@ impl SignedAmount { /// /// The function panics if the argument multiplied by the number of sats /// per bitcoin overflows an `i64` type. - pub const fn from_int_btc_const(btc: i64) -> SignedAmount { - match btc.checked_mul(100_000_000) { + pub const fn from_int_btc_const(whole_bitcoin: i64) -> SignedAmount { + match whole_bitcoin.checked_mul(100_000_000) { Some(amount) => SignedAmount::from_sat(amount), None => panic!("checked_mul overflowed"), } diff --git a/units/src/amount/tests.rs b/units/src/amount/tests.rs index baa120026..9d3687f6a 100644 --- a/units/src/amount/tests.rs +++ b/units/src/amount/tests.rs @@ -71,10 +71,6 @@ fn from_int_btc() { assert_eq!(Amount::from_sat(200_000_000), amt); } -#[should_panic] -#[test] -fn from_int_btc_panic() { Amount::from_int_btc_const(u64::MAX); } - #[test] fn test_signed_amount_try_from_amount() { let ua_positive = Amount::from_sat(123); diff --git a/units/src/amount/unsigned.rs b/units/src/amount/unsigned.rs index 8fa95eece..f18ea3e72 100644 --- a/units/src/amount/unsigned.rs +++ b/units/src/amount/unsigned.rs @@ -101,8 +101,8 @@ impl Amount { /// /// The function errors if the argument multiplied by the number of sats /// per bitcoin overflows a `u64` type. - pub fn from_int_btc(btc: u64) -> Result { - match btc.checked_mul(100_000_000) { + pub fn from_int_btc>(whole_bitcoin: T) -> Result { + match whole_bitcoin.into().checked_mul(100_000_000) { Some(amount) => Ok(Amount::from_sat(amount)), None => Err(OutOfRangeError { is_signed: false, is_greater_than_max: true }), } @@ -115,7 +115,8 @@ impl Amount { /// /// The function panics if the argument multiplied by the number of sats /// per bitcoin overflows a `u64` type. - pub const fn from_int_btc_const(btc: u64) -> Amount { + pub const fn from_int_btc_const(whole_bitcoin: u32) -> Amount { + let btc = whole_bitcoin as u64; // Can't call u64::from in const context. match btc.checked_mul(100_000_000) { Some(amount) => Amount::from_sat(amount), None => panic!("checked_mul overflowed"),