From e6f7b26d80a8f2680ca4732013d89e029bcb2abc Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 28 Feb 2025 09:41:10 +1100 Subject: [PATCH] Use _unchecked in amount const types We are about to start enforcing the MAX_MONEY invariant. Doing so will change constructors to return an error type. In preparation use the `_unchecked` constructor for all the consts. Internal change only, no logic changes. --- units/src/amount/signed.rs | 12 ++++++------ units/src/amount/unsigned.rs | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/units/src/amount/signed.rs b/units/src/amount/signed.rs index 7551ac197..d4c2d0227 100644 --- a/units/src/amount/signed.rs +++ b/units/src/amount/signed.rs @@ -50,17 +50,17 @@ pub struct SignedAmount(i64); impl SignedAmount { /// The zero amount. - pub const ZERO: Self = SignedAmount(0); + pub const ZERO: Self = SignedAmount::from_sat_unchecked(0); /// Exactly one satoshi. - pub const ONE_SAT: Self = SignedAmount(1); + pub const ONE_SAT: Self = SignedAmount::from_sat_unchecked(1); /// Exactly one bitcoin. - pub const ONE_BTC: Self = SignedAmount(100_000_000); + pub const ONE_BTC: Self = SignedAmount::from_sat_unchecked(100_000_000); /// Exactly fifty bitcoin. - pub const FIFTY_BTC: Self = Self::from_sat_unchecked(50 * 100_000_000); + pub const FIFTY_BTC: Self = SignedAmount::from_sat_unchecked(50 * 100_000_000); /// The maximum value allowed as an amount. Useful for sanity checking. - pub const MAX_MONEY: Self = SignedAmount(21_000_000 * 100_000_000); + pub const MAX_MONEY: Self = SignedAmount::from_sat_unchecked(21_000_000 * 100_000_000); /// The minimum value of an amount. - pub const MIN: Self = SignedAmount(-21_000_000 * 100_000_000); + pub const MIN: Self = SignedAmount::from_sat_unchecked(-21_000_000 * 100_000_000); /// The maximum value of an amount. pub const MAX: Self = SignedAmount::MAX_MONEY; diff --git a/units/src/amount/unsigned.rs b/units/src/amount/unsigned.rs index 89d7eb35f..71ee58de4 100644 --- a/units/src/amount/unsigned.rs +++ b/units/src/amount/unsigned.rs @@ -42,15 +42,15 @@ pub struct Amount(u64); impl Amount { /// The zero amount. - pub const ZERO: Self = Amount(0); + pub const ZERO: Self = Amount::from_sat_unchecked(0); /// Exactly one satoshi. - pub const ONE_SAT: Self = Amount(1); + pub const ONE_SAT: Self = Amount::from_sat_unchecked(1); /// Exactly one bitcoin. - pub const ONE_BTC: Self = Self::from_int_btc_const(1); + pub const ONE_BTC: Self = Amount::from_sat_unchecked(100_000_000); /// Exactly fifty bitcoin. - pub const FIFTY_BTC: Self = Self::from_sat_unchecked(50 * 100_000_000); + pub const FIFTY_BTC: Self = Amount::from_sat_unchecked(50 * 100_000_000); /// The maximum value allowed as an amount. Useful for sanity checking. - pub const MAX_MONEY: Self = Self::from_int_btc_const(21_000_000); + pub const MAX_MONEY: Self = Amount::from_sat_unchecked(21_000_000 * 100_000_000); /// The minimum value of an amount. pub const MIN: Self = Amount::ZERO; /// The maximum value of an amount.