Add missing Mul impls for amount types

Add and test missing `Mul` impls for both amount types.
This commit is contained in:
Tobin C. Harding 2025-03-03 07:51:54 +11:00
parent 501c9ab89e
commit b57bfb9bc5
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 24 additions and 2 deletions

View File

@ -161,6 +161,16 @@ crate::internal_macros::impl_op_for_references! {
fn mul(self, rhs: u64) -> Self::Output { self.and_then(|lhs| lhs * rhs) } fn mul(self, rhs: u64) -> Self::Output { self.and_then(|lhs| lhs * rhs) }
} }
impl ops::Mul<Amount> for u64 {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: Amount) -> Self::Output { rhs.checked_mul(self).valid_or_error() }
}
impl ops::Mul<NumOpResult<Amount>> for u64 {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: NumOpResult<Amount>) -> Self::Output { rhs.and_then(|rhs| self * rhs) }
}
impl ops::Div<u64> for Amount { impl ops::Div<u64> for Amount {
type Output = NumOpResult<Amount>; type Output = NumOpResult<Amount>;
@ -221,6 +231,16 @@ crate::internal_macros::impl_op_for_references! {
fn mul(self, rhs: i64) -> Self::Output { self.and_then(|lhs| lhs * rhs) } fn mul(self, rhs: i64) -> Self::Output { self.and_then(|lhs| lhs * rhs) }
} }
impl ops::Mul<SignedAmount> for i64 {
type Output = NumOpResult<SignedAmount>;
fn mul(self, rhs: SignedAmount) -> Self::Output { rhs.checked_mul(self).valid_or_error() }
}
impl ops::Mul<NumOpResult<SignedAmount>> for i64 {
type Output = NumOpResult<SignedAmount>;
fn mul(self, rhs: NumOpResult<SignedAmount>) -> Self::Output { rhs.and_then(|rhs| self * rhs) }
}
impl ops::Div<i64> for SignedAmount { impl ops::Div<i64> for SignedAmount {
type Output = NumOpResult<SignedAmount>; type Output = NumOpResult<SignedAmount>;

View File

@ -1252,8 +1252,10 @@ fn op_int_combos() {
assert_eq!(res(23) * 31, res(713)); assert_eq!(res(23) * 31, res(713));
assert_eq!(sres(23) * 31, sres(713)); assert_eq!(sres(23) * 31, sres(713));
// assert_eq!(31 * sat(23), res(713)); assert_eq!(31 * sat(23), res(713));
// assert_eq!(31 * ssat(23), sres(713)); assert_eq!(31 * ssat(23), sres(713));
assert_eq!(31 * res(23), res(713));
assert_eq!(31 * sres(23), sres(713));
// No remainder. // No remainder.
assert_eq!(sat(1897) / 7, res(271)); assert_eq!(sat(1897) / 7, res(271));