From b57bfb9bc53107d7a2f6f9f7cb2f4c6c5d733534 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 3 Mar 2025 07:51:54 +1100 Subject: [PATCH] Add missing Mul impls for amount types Add and test missing `Mul` impls for both amount types. --- units/src/amount/result.rs | 20 ++++++++++++++++++++ units/src/amount/tests.rs | 6 ++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/units/src/amount/result.rs b/units/src/amount/result.rs index 724230833..5f1d7742b 100644 --- a/units/src/amount/result.rs +++ b/units/src/amount/result.rs @@ -161,6 +161,16 @@ crate::internal_macros::impl_op_for_references! { fn mul(self, rhs: u64) -> Self::Output { self.and_then(|lhs| lhs * rhs) } } + impl ops::Mul for u64 { + type Output = NumOpResult; + + fn mul(self, rhs: Amount) -> Self::Output { rhs.checked_mul(self).valid_or_error() } + } + impl ops::Mul> for u64 { + type Output = NumOpResult; + + fn mul(self, rhs: NumOpResult) -> Self::Output { rhs.and_then(|rhs| self * rhs) } + } impl ops::Div for Amount { type Output = NumOpResult; @@ -221,6 +231,16 @@ crate::internal_macros::impl_op_for_references! { fn mul(self, rhs: i64) -> Self::Output { self.and_then(|lhs| lhs * rhs) } } + impl ops::Mul for i64 { + type Output = NumOpResult; + + fn mul(self, rhs: SignedAmount) -> Self::Output { rhs.checked_mul(self).valid_or_error() } + } + impl ops::Mul> for i64 { + type Output = NumOpResult; + + fn mul(self, rhs: NumOpResult) -> Self::Output { rhs.and_then(|rhs| self * rhs) } + } impl ops::Div for SignedAmount { type Output = NumOpResult; diff --git a/units/src/amount/tests.rs b/units/src/amount/tests.rs index 3ab69af1d..4a87ba420 100644 --- a/units/src/amount/tests.rs +++ b/units/src/amount/tests.rs @@ -1252,8 +1252,10 @@ fn op_int_combos() { assert_eq!(res(23) * 31, res(713)); assert_eq!(sres(23) * 31, sres(713)); - // assert_eq!(31 * sat(23), res(713)); - // assert_eq!(31 * ssat(23), sres(713)); + assert_eq!(31 * sat(23), res(713)); + assert_eq!(31 * ssat(23), sres(713)); + assert_eq!(31 * res(23), res(713)); + assert_eq!(31 * sres(23), sres(713)); // No remainder. assert_eq!(sat(1897) / 7, res(271));