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.
This commit is contained in:
Tobin C. Harding 2024-12-28 07:55:19 +11:00
parent f32af7dac6
commit 8e6784dd41
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
3 changed files with 8 additions and 5 deletions

View File

@ -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`].
///

View File

@ -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));

View File

@ -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