From 353c23fa0146bf01c1f4ca9af4b8fdc4a8f092ec Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Tue, 18 Feb 2025 18:29:02 +0000 Subject: [PATCH] units: pull generic op impls on NumOpResult into macro --- units/src/amount/result.rs | 126 +++++++++++-------------------------- 1 file changed, 38 insertions(+), 88 deletions(-) diff --git a/units/src/amount/result.rs b/units/src/amount/result.rs index a526fbd0b..b2091a1b5 100644 --- a/units/src/amount/result.rs +++ b/units/src/amount/result.rs @@ -169,23 +169,6 @@ crate::internal_macros::impl_op_for_references! { fn rem(self, modulus: u64) -> Self::Output { self.checked_rem(modulus).valid_or_error() } } - // FIXME these two should be covered by generic impls below - impl ops::Add for NumOpResult { - type Output = NumOpResult; - - fn add(self, rhs: Amount) -> Self::Output { rhs + self } - } - impl ops::Sub for NumOpResult { - type Output = NumOpResult; - - fn sub(self, rhs: Amount) -> Self::Output { - match self { - R::Valid(amount) => amount - rhs, - R::Error(_) => self, - } - } - } - impl ops::Add for SignedAmount { type Output = NumOpResult; @@ -208,29 +191,12 @@ crate::internal_macros::impl_op_for_references! { fn sub(self, rhs: NumOpResult) -> Self::Output { match rhs { - R::Valid(amount) => amount - rhs, + R::Valid(amount) => self - amount, R::Error(_) => rhs, } } } - impl ops::Add for NumOpResult { - type Output = NumOpResult; - - fn add(self, rhs: SignedAmount) -> Self::Output { rhs + self } - } - - impl ops::Sub for NumOpResult { - type Output = NumOpResult; - - fn sub(self, rhs: SignedAmount) -> Self::Output { - match self { - R::Valid(amount) => amount - rhs, - R::Error(_) => self, - } - } - } - impl ops::Mul for SignedAmount { type Output = NumOpResult; @@ -260,6 +226,43 @@ crate::internal_macros::impl_op_for_references! { } } } + + impl ops::Add for NumOpResult + where + (T: Copy + ops::Add, Output = NumOpResult>) + { + type Output = NumOpResult; + + fn add(self, rhs: T) -> Self::Output { rhs + self } + } + + impl ops::Sub> for NumOpResult + where + (T: Copy + ops::Sub>) + { + type Output = NumOpResult; + + fn sub(self, rhs: Self) -> Self::Output { + match (self, rhs) { + (R::Valid(lhs), R::Valid(rhs)) => lhs - rhs, + (_, _) => R::Error(NumOpError {}), + } + } + } + + impl ops::Sub for NumOpResult + where + (T: Copy + ops::Sub>) + { + type Output = NumOpResult; + + fn sub(self, rhs: T) -> Self::Output { + match self { + R::Valid(amount) => amount - rhs, + R::Error(_) => self, + } + } + } } impl ops::Neg for SignedAmount { @@ -268,59 +271,6 @@ impl ops::Neg for SignedAmount { fn neg(self) -> Self::Output { Self::from_sat(self.to_sat().neg()) } } -impl ops::Sub for NumOpResult -where - T: ops::Sub>, -{ - type Output = NumOpResult; - - fn sub(self, rhs: Self) -> Self::Output { - match (self, rhs) { - (R::Valid(lhs), R::Valid(rhs)) => lhs - rhs, - (_, _) => R::Error(NumOpError {}), - } - } -} -impl ops::Sub> for &NumOpResult -where - T: ops::Sub> + Copy, -{ - type Output = NumOpResult; - - fn sub(self, rhs: NumOpResult) -> Self::Output { - match (self, rhs) { - (R::Valid(lhs), R::Valid(rhs)) => *lhs - rhs, - (_, _) => R::Error(NumOpError {}), - } - } -} -impl ops::Sub<&NumOpResult> for NumOpResult -where - T: ops::Sub> + Copy, -{ - type Output = NumOpResult; - - fn sub(self, rhs: &NumOpResult) -> Self::Output { - match (self, rhs) { - (R::Valid(lhs), R::Valid(rhs)) => lhs - *rhs, - (_, _) => R::Error(NumOpError {}), - } - } -} -impl ops::Sub for &NumOpResult -where - T: ops::Sub> + Copy, -{ - type Output = NumOpResult; - - fn sub(self, rhs: Self) -> Self::Output { - match (self, rhs) { - (R::Valid(lhs), R::Valid(rhs)) => *lhs - *rhs, - (_, _) => R::Error(NumOpError {}), - } - } -} - impl core::iter::Sum> for NumOpResult { fn sum(iter: I) -> Self where