From 99b32c95b834166020ce85580246dc8ff193d467 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 12 Dec 2024 12:15:50 +1100 Subject: [PATCH] Change paramater type used for whole bitcoin We have multiple constructors for `Amount`, one of which accepts an integer representing whole bitcoin. In an attempt to make the API crystal clear change the parameter type used for whole bitcoin to either `Into` or `u32` instead of a `u64` which may well be mapped to sats in the mind of developers (it is in mine anyway). --- units/src/amount/signed.rs | 8 ++++---- units/src/amount/tests.rs | 4 ---- units/src/amount/unsigned.rs | 7 ++++--- 3 files changed, 8 insertions(+), 11 deletions(-) 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"),