From 8e6784dd417dfb06b985407a60f3aeea4ec99db9 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Sat, 28 Dec 2024 07:55:19 +1100 Subject: [PATCH] Introduce Amount::from_sat_unchecked As we did for `SignedAmount` add a constructor to the `Amount` type that does no checks on its argument. (This is in preparation for enforcing the MAX_MONEY invariant.) Use the `_unchecked` version in a single unit test. The rest of the unit tests will be refactored later to minimise the size of this patch. --- units/src/amount/signed.rs | 4 +--- units/src/amount/tests.rs | 4 ++-- units/src/amount/unsigned.rs | 5 +++++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/units/src/amount/signed.rs b/units/src/amount/signed.rs index 2d3846ab5..4580af033 100644 --- a/units/src/amount/signed.rs +++ b/units/src/amount/signed.rs @@ -88,9 +88,7 @@ impl SignedAmount { /// Caller to guarantee that `satoshi` is within valid range. /// /// See [`Self::MIN`] and [`Self::MAX_MONEY`]. - pub const fn from_sat_unchecked(satoshi: i64) -> SignedAmount { - SignedAmount(satoshi) - } + pub const fn from_sat_unchecked(satoshi: i64) -> SignedAmount { SignedAmount(satoshi) } /// Converts from a value expressing a decimal number of bitcoin to a [`SignedAmount`]. /// diff --git a/units/src/amount/tests.rs b/units/src/amount/tests.rs index adc1f6352..ecb5f3885 100644 --- a/units/src/amount/tests.rs +++ b/units/src/amount/tests.rs @@ -560,9 +560,9 @@ fn from_str() { fn to_from_string_in() { use super::Denomination as D; let ua_str = Amount::from_str_in; - let ua_sat = Amount::from_sat; + let ua_sat = Amount::from_sat_unchecked; let sa_str = SignedAmount::from_str_in; - let sa_sat = SignedAmount::from_sat; + let sa_sat = SignedAmount::from_sat_unchecked; assert_eq!("0.5", ua_sat(50).to_string_in(D::Bit)); assert_eq!("-0.5", sa_sat(-50).to_string_in(D::Bit)); diff --git a/units/src/amount/unsigned.rs b/units/src/amount/unsigned.rs index db7017792..f6cb57eb5 100644 --- a/units/src/amount/unsigned.rs +++ b/units/src/amount/unsigned.rs @@ -90,6 +90,11 @@ impl Amount { /// ``` pub const fn to_sat(self) -> u64 { self.0 } + /// Constructs a new [`Amount`] with satoshi precision and the given number of satoshis. + /// + /// Caller to guarantee that `satoshi` is within valid range. See [`Self::MAX_MONEY`]. + pub const fn from_sat_unchecked(satoshi: u64) -> Amount { Self(satoshi) } + /// Converts from a value expressing a decimal number of bitcoin to an [`Amount`]. /// /// # Errors