diff --git a/units/src/amount/result.rs b/units/src/amount/result.rs index 67f76a6c2..a526fbd0b 100644 --- a/units/src/amount/result.rs +++ b/units/src/amount/result.rs @@ -246,6 +246,20 @@ crate::internal_macros::impl_op_for_references! { fn rem(self, modulus: i64) -> Self::Output { self.checked_rem(modulus).valid_or_error() } } + + impl ops::Add> for NumOpResult + where + (T: Copy + ops::Add>) + { + type Output = NumOpResult; + + fn add(self, rhs: Self) -> Self::Output { + match (self, rhs) { + (R::Valid(lhs), R::Valid(rhs)) => lhs + rhs, + (_, _) => R::Error(NumOpError {}), + } + } + } } impl ops::Neg for SignedAmount { @@ -254,59 +268,6 @@ impl ops::Neg for SignedAmount { fn neg(self) -> Self::Output { Self::from_sat(self.to_sat().neg()) } } -impl ops::Add for NumOpResult -where - T: ops::Add>, -{ - type Output = NumOpResult; - - fn add(self, rhs: Self) -> Self::Output { - match (self, rhs) { - (R::Valid(lhs), R::Valid(rhs)) => lhs + rhs, - (_, _) => R::Error(NumOpError {}), - } - } -} -impl ops::Add> for &NumOpResult -where - T: ops::Add> + Copy, -{ - type Output = NumOpResult; - - fn add(self, rhs: NumOpResult) -> Self::Output { - match (self, rhs) { - (R::Valid(lhs), R::Valid(rhs)) => *lhs + rhs, - (_, _) => R::Error(NumOpError {}), - } - } -} -impl ops::Add<&NumOpResult> for NumOpResult -where - T: ops::Add> + Copy, -{ - type Output = NumOpResult; - - fn add(self, rhs: &NumOpResult) -> Self::Output { - match (self, rhs) { - (R::Valid(lhs), R::Valid(rhs)) => lhs + *rhs, - (_, _) => R::Error(NumOpError {}), - } - } -} -impl ops::Add for &NumOpResult -where - T: ops::Add> + Copy, -{ - type Output = NumOpResult; - - fn add(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>, diff --git a/units/src/internal_macros.rs b/units/src/internal_macros.rs index 668272aeb..c5f0ac6fc 100644 --- a/units/src/internal_macros.rs +++ b/units/src/internal_macros.rs @@ -17,37 +17,49 @@ /// /// You must specify `$other_ty` and you may not use `Self`. So e.g. you need /// to write `impl ops::Add for Amount { ... }` when calling this macro. +/// +/// Your where clause must include extra parenthesis, like `where (T: Copy)`. macro_rules! impl_op_for_references { ($( - impl $($op_trait:ident)::+<$other_ty:ty> for $ty:ty { + impl$(<$gen:ident>)? $($op_trait:ident)::+<$other_ty:ty> for $ty:ty + $(where ($($bounds:tt)*))? + { type Output = $($main_output:ty)*; fn $op:ident($($main_args:tt)*) -> Self::Output { $($main_impl:tt)* } } )+) => {$( - impl $($op_trait)::+<$other_ty> for $ty { + impl$(<$gen>)? $($op_trait)::+<$other_ty> for $ty + $(where $($bounds)*)? + { type Output = $($main_output)*; fn $op($($main_args)*) -> Self::Output { $($main_impl)* } } - impl $($op_trait)::+<$other_ty> for &$ty { + impl$(<$gen>)? $($op_trait)::+<$other_ty> for &$ty + $(where $($bounds)*)? + { type Output = <$ty as $($op_trait)::+<$other_ty>>::Output; fn $op(self, rhs: $other_ty) -> Self::Output { (*self).$op(rhs) } } - impl $($op_trait)::+<&$other_ty> for $ty { + impl$(<$gen>)? $($op_trait)::+<&$other_ty> for $ty + $(where $($bounds)*)? + { type Output = <$ty as $($op_trait)::+<$other_ty>>::Output; fn $op(self, rhs: &$other_ty) -> Self::Output { self.$op(*rhs) } } - impl<'a> $($op_trait)::+<&'a $other_ty> for &$ty { + impl<'a, $($gen)?> $($op_trait)::+<&'a $other_ty> for &$ty + $(where $($bounds)*)? + { type Output = <$ty as $($op_trait)::+<$other_ty>>::Output; fn $op(self, rhs: &$other_ty) -> Self::Output { (*self).$op(*rhs)