diff --git a/units/src/fee_rate.rs b/units/src/fee_rate.rs index 1c2daad41..6700434cd 100644 --- a/units/src/fee_rate.rs +++ b/units/src/fee_rate.rs @@ -190,6 +190,7 @@ impl ops::Add for FeeRate { fn add(self, rhs: FeeRate) -> Self::Output { FeeRate(self.0 + rhs.0) } } crate::internal_macros::impl_add_for_references!(FeeRate); +crate::internal_macros::impl_add_assign!(FeeRate); impl ops::Sub for FeeRate { type Output = FeeRate; @@ -197,6 +198,7 @@ impl ops::Sub for 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_sub_assign!(FeeRate); /// Computes the ceiling so that the fee computation is conservative. impl ops::Mul for Weight { @@ -223,22 +225,6 @@ impl ops::Div for Amount { fn div(self, rhs: Weight) -> Self::Output { FeeRate(self.to_sat() * 1000 / rhs.to_wu()) } } -impl ops::AddAssign for FeeRate { - fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0 } -} - -impl ops::AddAssign<&FeeRate> for FeeRate { - fn add_assign(&mut self, rhs: &FeeRate) { self.0 += rhs.0 } -} - -impl ops::SubAssign for FeeRate { - fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0 } -} - -impl ops::SubAssign<&FeeRate> for FeeRate { - fn sub_assign(&mut self, rhs: &FeeRate) { self.0 -= rhs.0 } -} - impl core::iter::Sum for FeeRate { fn sum(iter: I) -> Self where diff --git a/units/src/internal_macros.rs b/units/src/internal_macros.rs index bd4ce335b..03536b5ec 100644 --- a/units/src/internal_macros.rs +++ b/units/src/internal_macros.rs @@ -34,6 +34,20 @@ macro_rules! impl_add_for_references { } pub(crate) use impl_add_for_references; +/// Implement `ops::AddAssign` for `$ty` and `&$ty`. +macro_rules! impl_add_assign { + ($ty:ident) => { + impl core::ops::AddAssign<$ty> for $ty { + fn add_assign(&mut self, rhs: $ty) { *self = *self + rhs } + } + + impl core::ops::AddAssign<&$ty> for $ty { + fn add_assign(&mut self, rhs: &$ty) { *self = *self + *rhs } + } + } +} +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: @@ -63,3 +77,17 @@ macro_rules! impl_sub_for_references { } } pub(crate) use impl_sub_for_references; + +/// Implement `ops::SubAssign` for `$ty` and `&$ty`. +macro_rules! impl_sub_assign { + ($ty:ident) => { + impl core::ops::SubAssign<$ty> for $ty { + fn sub_assign(&mut self, rhs: $ty) { *self = *self - rhs } + } + + impl core::ops::SubAssign<&$ty> for $ty { + fn sub_assign(&mut self, rhs: &$ty) { *self = *self - *rhs } + } + } +} +pub(crate) use impl_sub_assign;