diff --git a/units/src/amount/result.rs b/units/src/amount/result.rs index 98ad8df4f..67f76a6c2 100644 --- a/units/src/amount/result.rs +++ b/units/src/amount/result.rs @@ -135,270 +135,118 @@ crate::internal_macros::impl_op_for_references! { fn add(self, rhs: NumOpResult) -> Self::Output { rhs.and_then(|a| a + self) } } -} -crate::internal_macros::impl_op_for_references! { impl ops::Sub for Amount { type Output = NumOpResult; fn sub(self, rhs: Amount) -> Self::Output { self.checked_sub(rhs).valid_or_error() } } -} -impl ops::Sub> for Amount { - type Output = NumOpResult; + impl ops::Sub> for Amount { + type Output = NumOpResult; - fn sub(self, rhs: NumOpResult) -> Self::Output { - match rhs { - R::Valid(amount) => self - amount, - R::Error(_) => rhs, + fn sub(self, rhs: NumOpResult) -> Self::Output { + match rhs { + R::Valid(amount) => self - amount, + R::Error(_) => rhs, + } } } -} -impl ops::Sub> for &Amount { - type Output = NumOpResult; - fn sub(self, rhs: NumOpResult) -> Self::Output { - match rhs { - R::Valid(amount) => self - amount, - R::Error(_) => rhs, + impl ops::Mul for Amount { + type Output = NumOpResult; + + fn mul(self, rhs: u64) -> Self::Output { self.checked_mul(rhs).valid_or_error() } + } + impl ops::Div for Amount { + type Output = NumOpResult; + + fn div(self, rhs: u64) -> Self::Output { self.checked_div(rhs).valid_or_error() } + } + impl ops::Rem for Amount { + type Output = NumOpResult; + + 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::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; + + fn add(self, rhs: SignedAmount) -> Self::Output { self.checked_add(rhs).valid_or_error() } + } + + impl ops::Add> for SignedAmount { + type Output = NumOpResult; + + fn add(self, rhs: NumOpResult) -> Self::Output { rhs.and_then(|a| a + self) } + } + + impl ops::Sub for SignedAmount { + type Output = NumOpResult; + + fn sub(self, rhs: SignedAmount) -> Self::Output { self.checked_sub(rhs).valid_or_error() } + } + impl ops::Sub> for SignedAmount { + type Output = NumOpResult; + + fn sub(self, rhs: NumOpResult) -> Self::Output { + match rhs { + R::Valid(amount) => amount - rhs, + R::Error(_) => rhs, + } } } -} -impl ops::Sub<&Amount> 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 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 Amount { - type Output = NumOpResult; + impl ops::Mul for SignedAmount { + type Output = NumOpResult; - fn mul(self, rhs: u64) -> Self::Output { self.checked_mul(rhs).valid_or_error() } -} -impl ops::Mul<&u64> for Amount { - type Output = NumOpResult; - - fn mul(self, rhs: &u64) -> Self::Output { self.mul(*rhs) } -} -impl ops::Mul for &Amount { - type Output = NumOpResult; - - fn mul(self, rhs: u64) -> Self::Output { (*self).mul(rhs) } -} -impl ops::Mul<&u64> for &Amount { - type Output = NumOpResult; - - fn mul(self, rhs: &u64) -> Self::Output { self.mul(*rhs) } -} - -impl ops::Div for Amount { - type Output = NumOpResult; - - fn div(self, rhs: u64) -> Self::Output { self.checked_div(rhs).valid_or_error() } -} -impl ops::Div<&u64> for Amount { - type Output = NumOpResult; - - fn div(self, rhs: &u64) -> Self::Output { self.div(*rhs) } -} -impl ops::Div for &Amount { - type Output = NumOpResult; - - fn div(self, rhs: u64) -> Self::Output { (*self).div(rhs) } -} -impl ops::Div<&u64> for &Amount { - type Output = NumOpResult; - - fn div(self, rhs: &u64) -> Self::Output { (*self).div(*rhs) } -} - -impl ops::Rem for Amount { - type Output = NumOpResult; - - fn rem(self, modulus: u64) -> Self::Output { self.checked_rem(modulus).valid_or_error() } -} -impl ops::Rem<&u64> for Amount { - type Output = NumOpResult; - - fn rem(self, modulus: &u64) -> Self::Output { self.rem(*modulus) } -} -impl ops::Rem for &Amount { - type Output = NumOpResult; - - fn rem(self, modulus: u64) -> Self::Output { (*self).rem(modulus) } -} -impl ops::Rem<&u64> for &Amount { - type Output = NumOpResult; - - fn rem(self, modulus: &u64) -> Self::Output { (*self).rem(*modulus) } -} - -// 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::Add<&Amount> for NumOpResult { - type Output = NumOpResult; - - fn add(self, rhs: &Amount) -> Self::Output { rhs + self } -} - -impl ops::Add for SignedAmount { - type Output = NumOpResult; - - fn add(self, rhs: SignedAmount) -> Self::Output { self.checked_add(rhs).valid_or_error() } -} -crate::internal_macros::impl_add_for_amount_references!(SignedAmount); - -impl ops::Add> for SignedAmount { - type Output = NumOpResult; - - fn add(self, rhs: NumOpResult) -> Self::Output { rhs.and_then(|a| a + self) } -} -impl ops::Add> for &SignedAmount { - type Output = NumOpResult; - - fn add(self, rhs: NumOpResult) -> Self::Output { rhs.and_then(|a| a + self) } -} -impl ops::Add for NumOpResult { - type Output = NumOpResult; - - fn add(self, rhs: SignedAmount) -> Self::Output { rhs + self } -} -impl ops::Add<&SignedAmount> for NumOpResult { - type Output = NumOpResult; - - fn add(self, rhs: &SignedAmount) -> Self::Output { rhs + self } -} - -impl ops::Sub for SignedAmount { - type Output = NumOpResult; - - fn sub(self, rhs: SignedAmount) -> Self::Output { self.checked_sub(rhs).valid_or_error() } -} -crate::internal_macros::impl_sub_for_amount_references!(SignedAmount); - -impl ops::Sub> for SignedAmount { - type Output = NumOpResult; - - fn sub(self, rhs: NumOpResult) -> Self::Output { - match rhs { - R::Valid(amount) => amount - rhs, - R::Error(_) => rhs, - } + fn mul(self, rhs: i64) -> Self::Output { self.checked_mul(rhs).valid_or_error() } } -} -impl ops::Sub> for &SignedAmount { - type Output = NumOpResult; + impl ops::Div for SignedAmount { + type Output = NumOpResult; - fn sub(self, rhs: NumOpResult) -> Self::Output { - match rhs { - R::Valid(amount) => amount - rhs, - R::Error(_) => rhs, - } + fn div(self, rhs: i64) -> Self::Output { self.checked_div(rhs).valid_or_error() } } -} -impl ops::Sub for NumOpResult { - type Output = NumOpResult; + impl ops::Rem for SignedAmount { + type Output = NumOpResult; - fn sub(self, rhs: SignedAmount) -> Self::Output { - match self { - R::Valid(amount) => amount - rhs, - R::Error(_) => self, - } + fn rem(self, modulus: i64) -> Self::Output { self.checked_rem(modulus).valid_or_error() } } } -impl ops::Sub<&SignedAmount> 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; - - fn mul(self, rhs: i64) -> Self::Output { self.checked_mul(rhs).valid_or_error() } -} -impl ops::Mul<&i64> for SignedAmount { - type Output = NumOpResult; - - fn mul(self, rhs: &i64) -> Self::Output { self.mul(*rhs) } -} -impl ops::Mul for &SignedAmount { - type Output = NumOpResult; - - fn mul(self, rhs: i64) -> Self::Output { (*self).mul(rhs) } -} -impl ops::Mul<&i64> for &SignedAmount { - type Output = NumOpResult; - - fn mul(self, rhs: &i64) -> Self::Output { self.mul(*rhs) } -} - -impl ops::Div for SignedAmount { - type Output = NumOpResult; - - fn div(self, rhs: i64) -> Self::Output { self.checked_div(rhs).valid_or_error() } -} -impl ops::Div<&i64> for SignedAmount { - type Output = NumOpResult; - - fn div(self, rhs: &i64) -> Self::Output { self.div(*rhs) } -} -impl ops::Div for &SignedAmount { - type Output = NumOpResult; - - fn div(self, rhs: i64) -> Self::Output { (*self).div(rhs) } -} -impl ops::Div<&i64> for &SignedAmount { - type Output = NumOpResult; - - fn div(self, rhs: &i64) -> Self::Output { (*self).div(*rhs) } -} - -impl ops::Rem for SignedAmount { - type Output = NumOpResult; - - fn rem(self, modulus: i64) -> Self::Output { self.checked_rem(modulus).valid_or_error() } -} -impl ops::Rem<&i64> for SignedAmount { - type Output = NumOpResult; - - fn rem(self, modulus: &i64) -> Self::Output { self.rem(*modulus) } -} -impl ops::Rem for &SignedAmount { - type Output = NumOpResult; - - fn rem(self, modulus: i64) -> Self::Output { (*self).rem(modulus) } -} -impl ops::Rem<&i64> for &SignedAmount { - type Output = NumOpResult; - - fn rem(self, modulus: &i64) -> Self::Output { (*self).rem(*modulus) } -} impl ops::Neg for SignedAmount { type Output = Self; diff --git a/units/src/fee_rate/mod.rs b/units/src/fee_rate/mod.rs index 1579578cc..f3ec8fd7d 100644 --- a/units/src/fee_rate/mod.rs +++ b/units/src/fee_rate/mod.rs @@ -135,19 +135,19 @@ impl From for u64 { fn from(value: FeeRate) -> Self { value.to_sat_per_kwu() } } -impl ops::Add for FeeRate { - type Output = FeeRate; +crate::internal_macros::impl_op_for_references! { + impl ops::Add for FeeRate { + type Output = FeeRate; - fn add(self, rhs: FeeRate) -> Self::Output { FeeRate(self.0 + rhs.0) } + fn add(self, rhs: FeeRate) -> Self::Output { FeeRate(self.0 + rhs.0) } + } + + impl ops::Sub for FeeRate { + type Output = FeeRate; + + fn sub(self, rhs: FeeRate) -> Self::Output { FeeRate(self.0 - rhs.0) } + } } -crate::internal_macros::impl_add_for_references!(FeeRate); - -impl ops::Sub for FeeRate { - type Output = FeeRate; - - fn sub(self, rhs: FeeRate) -> Self::Output { FeeRate(self.0 - rhs.0) } -} -crate::internal_macros::impl_sub_for_references!(FeeRate); crate::internal_macros::impl_add_assign!(FeeRate); crate::internal_macros::impl_sub_assign!(FeeRate); diff --git a/units/src/internal_macros.rs b/units/src/internal_macros.rs index d5d0f14d1..668272aeb 100644 --- a/units/src/internal_macros.rs +++ b/units/src/internal_macros.rs @@ -57,66 +57,6 @@ macro_rules! impl_op_for_references { } pub(crate) use impl_op_for_references; -/// Implements `ops::Add` for various references. -/// -/// Requires `$ty` it implement `Add` e.g. 'impl Add for T'. Adds impls of: -/// -/// - Add for &T -/// - Add<&T> for T -/// - Add<&T> for &T -macro_rules! impl_add_for_references { - ($ty:ident) => { - impl core::ops::Add<$ty> for &$ty { - type Output = $ty; - - fn add(self, rhs: $ty) -> Self::Output { *self + rhs } - } - - impl core::ops::Add<&$ty> for $ty { - type Output = $ty; - - fn add(self, rhs: &$ty) -> Self::Output { self + *rhs } - } - - impl<'a> core::ops::Add<&'a $ty> for &$ty { - type Output = $ty; - - fn add(self, rhs: &'a $ty) -> Self::Output { *self + *rhs } - } - }; -} -pub(crate) use impl_add_for_references; - -/// Implements `ops::Add` for various amount references. -/// -/// Requires `$ty` it implement `Add` e.g. 'impl Add for T'. Adds impls of: -/// -/// - Add for &T -/// - Add<&T> for T -/// - Add<&T> for &T -macro_rules! impl_add_for_amount_references { - ($ty:ident) => { - impl core::ops::Add<$ty> for &$ty { - type Output = NumOpResult<$ty>; - - fn add(self, rhs: $ty) -> Self::Output { *self + rhs } - } - - impl core::ops::Add<&$ty> for $ty { - type Output = NumOpResult<$ty>; - - fn add(self, rhs: &$ty) -> Self::Output { self + *rhs } - } - - impl<'a> core::ops::Add<&'a $ty> for &$ty { - type Output = NumOpResult<$ty>; - - fn add(self, rhs: &'a $ty) -> Self::Output { *self + *rhs } - } - }; -} -pub(crate) use impl_add_for_amount_references; - /// Implement `ops::AddAssign` for `$ty` and `&$ty`. macro_rules! impl_add_assign { ($ty:ident) => { @@ -131,66 +71,6 @@ macro_rules! impl_add_assign { } pub(crate) use impl_add_assign; -/// Implement `ops::Sub` for various references. -/// -/// Requires `$ty` it implement `Sub` e.g. 'impl Sub for T'. Adds impls of: -/// -/// - Sub for &T -/// - Sub<&T> for T -/// - Sub<&T> for &T -macro_rules! impl_sub_for_references { - ($ty:ident) => { - impl core::ops::Sub<$ty> for &$ty { - type Output = $ty; - - fn sub(self, rhs: $ty) -> Self::Output { *self - rhs } - } - - impl core::ops::Sub<&$ty> for $ty { - type Output = $ty; - - fn sub(self, rhs: &$ty) -> Self::Output { self - *rhs } - } - - impl<'a> core::ops::Sub<&'a $ty> for &$ty { - type Output = $ty; - - fn sub(self, rhs: &'a $ty) -> Self::Output { *self - *rhs } - } - }; -} -pub(crate) use impl_sub_for_references; - -/// Implement `ops::Sub` for various amount references. -/// -/// Requires `$ty` it implement `Sub` e.g. 'impl Sub for T'. Adds impls of: -/// -/// - Sub for &T -/// - Sub<&T> for T -/// - Sub<&T> for &T -macro_rules! impl_sub_for_amount_references { - ($ty:ident) => { - impl core::ops::Sub<$ty> for &$ty { - type Output = NumOpResult<$ty>; - - fn sub(self, rhs: $ty) -> Self::Output { *self - rhs } - } - - impl core::ops::Sub<&$ty> for $ty { - type Output = NumOpResult<$ty>; - - fn sub(self, rhs: &$ty) -> Self::Output { self - *rhs } - } - - impl<'a> core::ops::Sub<&'a $ty> for &$ty { - type Output = NumOpResult<$ty>; - - fn sub(self, rhs: &'a $ty) -> Self::Output { *self - *rhs } - } - }; -} -pub(crate) use impl_sub_for_amount_references; - /// Implement `ops::SubAssign` for `$ty` and `&$ty`. macro_rules! impl_sub_assign { ($ty:ident) => { diff --git a/units/src/weight.rs b/units/src/weight.rs index a1ea4fa80..720dfd385 100644 --- a/units/src/weight.rs +++ b/units/src/weight.rs @@ -166,50 +166,46 @@ impl From for u64 { fn from(value: Weight) -> Self { value.to_wu() } } -impl ops::Add for Weight { - type Output = Weight; +crate::internal_macros::impl_op_for_references! { + impl ops::Add for Weight { + type Output = Weight; - fn add(self, rhs: Weight) -> Self::Output { Weight(self.0 + rhs.0) } + fn add(self, rhs: Weight) -> Self::Output { Weight(self.0 + rhs.0) } + } + impl ops::Sub for Weight { + type Output = Weight; + + fn sub(self, rhs: Weight) -> Self::Output { Weight(self.0 - rhs.0) } + } + + impl ops::Mul for Weight { + type Output = Weight; + + fn mul(self, rhs: u64) -> Self::Output { Weight(self.0 * rhs) } + } + impl ops::Mul for u64 { + type Output = Weight; + + fn mul(self, rhs: Weight) -> Self::Output { Weight(self * rhs.0) } + } + impl ops::Div for Weight { + type Output = Weight; + + fn div(self, rhs: u64) -> Self::Output { Weight(self.0 / rhs) } + } + impl ops::Div for Weight { + type Output = u64; + + fn div(self, rhs: Weight) -> Self::Output { self.to_wu() / rhs.to_wu() } + } } -crate::internal_macros::impl_add_for_references!(Weight); - -impl ops::Sub for Weight { - type Output = Weight; - - fn sub(self, rhs: Weight) -> Self::Output { Weight(self.0 - rhs.0) } -} -crate::internal_macros::impl_sub_for_references!(Weight); crate::internal_macros::impl_add_assign!(Weight); crate::internal_macros::impl_sub_assign!(Weight); -impl ops::Mul for Weight { - type Output = Weight; - - fn mul(self, rhs: u64) -> Self::Output { Weight(self.0 * rhs) } -} - -impl ops::Mul for u64 { - type Output = Weight; - - fn mul(self, rhs: Weight) -> Self::Output { Weight(self * rhs.0) } -} - impl ops::MulAssign for Weight { fn mul_assign(&mut self, rhs: u64) { self.0 *= rhs } } -impl ops::Div for Weight { - type Output = Weight; - - fn div(self, rhs: u64) -> Self::Output { Weight(self.0 / rhs) } -} - -impl ops::Div for Weight { - type Output = u64; - - fn div(self, rhs: Weight) -> Self::Output { self.to_wu() / rhs.to_wu() } -} - impl ops::DivAssign for Weight { fn div_assign(&mut self, rhs: u64) { self.0 /= rhs } }