units: Add assign macros

Add macros for implementing `ops::AddAssign` and `ops::SubAssign`. Use
them in `fee_rate`.

Refactor only, no logic changes.
This commit is contained in:
Tobin C. Harding 2024-12-11 16:19:50 +11:00
parent 20a79da344
commit c5fbc7e117
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 30 additions and 16 deletions

View File

@ -190,6 +190,7 @@ impl ops::Add for 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) }
} }
crate::internal_macros::impl_add_for_references!(FeeRate); crate::internal_macros::impl_add_for_references!(FeeRate);
crate::internal_macros::impl_add_assign!(FeeRate);
impl ops::Sub for FeeRate { impl ops::Sub for FeeRate {
type Output = 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) } 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_for_references!(FeeRate);
crate::internal_macros::impl_sub_assign!(FeeRate);
/// Computes the ceiling so that the fee computation is conservative. /// Computes the ceiling so that the fee computation is conservative.
impl ops::Mul<FeeRate> for Weight { impl ops::Mul<FeeRate> for Weight {
@ -223,22 +225,6 @@ impl ops::Div<Weight> for Amount {
fn div(self, rhs: Weight) -> Self::Output { FeeRate(self.to_sat() * 1000 / rhs.to_wu()) } 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 { impl core::iter::Sum for FeeRate {
fn sum<I>(iter: I) -> Self fn sum<I>(iter: I) -> Self
where where

View File

@ -34,6 +34,20 @@ macro_rules! impl_add_for_references {
} }
pub(crate) use 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. /// Implement `ops::Sub` for various references.
/// ///
/// Requires `$ty` it implement `Sub` e.g. 'impl Sub<T> for T'. Adds impls of: /// Requires `$ty` it implement `Sub` e.g. 'impl Sub<T> for T'. Adds impls of:
@ -63,3 +77,17 @@ macro_rules! impl_sub_for_references {
} }
} }
pub(crate) use 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;