units: replace a gazillion more macro calls with new macro

Looks like a large diff but if you run

    git show --color-moved-ws=allow-indentation-change

you will see that it's 100% moves (though moves of code into the
reference macro). May be easier to just look at src/amount/result.rs
after this; it's pretty short now.
This commit is contained in:
Andrew Poelstra 2025-02-18 16:24:05 +00:00
parent 0dc7f6cebd
commit ad9564895b
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 127 additions and 403 deletions

View File

@ -135,270 +135,118 @@ crate::internal_macros::impl_op_for_references! {
fn add(self, rhs: NumOpResult<Amount>) -> Self::Output { rhs.and_then(|a| a + self) }
}
}
crate::internal_macros::impl_op_for_references! {
impl ops::Sub<Amount> for Amount {
type Output = NumOpResult<Amount>;
fn sub(self, rhs: Amount) -> Self::Output { self.checked_sub(rhs).valid_or_error() }
}
}
impl ops::Sub<NumOpResult<Amount>> for Amount {
type Output = NumOpResult<Amount>;
impl ops::Sub<NumOpResult<Amount>> for Amount {
type Output = NumOpResult<Amount>;
fn sub(self, rhs: NumOpResult<Amount>) -> Self::Output {
match rhs {
R::Valid(amount) => self - amount,
R::Error(_) => rhs,
fn sub(self, rhs: NumOpResult<Amount>) -> Self::Output {
match rhs {
R::Valid(amount) => self - amount,
R::Error(_) => rhs,
}
}
}
}
impl ops::Sub<NumOpResult<Amount>> for &Amount {
type Output = NumOpResult<Amount>;
fn sub(self, rhs: NumOpResult<Amount>) -> Self::Output {
match rhs {
R::Valid(amount) => self - amount,
R::Error(_) => rhs,
impl ops::Mul<u64> for Amount {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: u64) -> Self::Output { self.checked_mul(rhs).valid_or_error() }
}
impl ops::Div<u64> for Amount {
type Output = NumOpResult<Amount>;
fn div(self, rhs: u64) -> Self::Output { self.checked_div(rhs).valid_or_error() }
}
impl ops::Rem<u64> for Amount {
type Output = NumOpResult<Amount>;
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<Amount> for NumOpResult<Amount> {
type Output = NumOpResult<Amount>;
fn add(self, rhs: Amount) -> Self::Output { rhs + self }
}
impl ops::Sub<Amount> for NumOpResult<Amount> {
type Output = NumOpResult<Amount>;
fn sub(self, rhs: Amount) -> Self::Output {
match self {
R::Valid(amount) => amount - rhs,
R::Error(_) => self,
}
}
}
}
impl ops::Sub<Amount> for NumOpResult<Amount> {
type Output = NumOpResult<Amount>;
fn sub(self, rhs: Amount) -> Self::Output {
match self {
R::Valid(amount) => amount - rhs,
R::Error(_) => self,
impl ops::Add<SignedAmount> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn add(self, rhs: SignedAmount) -> Self::Output { self.checked_add(rhs).valid_or_error() }
}
impl ops::Add<NumOpResult<SignedAmount>> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn add(self, rhs: NumOpResult<SignedAmount>) -> Self::Output { rhs.and_then(|a| a + self) }
}
impl ops::Sub<SignedAmount> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn sub(self, rhs: SignedAmount) -> Self::Output { self.checked_sub(rhs).valid_or_error() }
}
impl ops::Sub<NumOpResult<SignedAmount>> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn sub(self, rhs: NumOpResult<SignedAmount>) -> Self::Output {
match rhs {
R::Valid(amount) => amount - rhs,
R::Error(_) => rhs,
}
}
}
}
impl ops::Sub<&Amount> for NumOpResult<Amount> {
type Output = NumOpResult<Amount>;
fn sub(self, rhs: &Amount) -> Self::Output {
match self {
R::Valid(amount) => amount - (*rhs),
R::Error(_) => self,
impl ops::Add<SignedAmount> for NumOpResult<SignedAmount> {
type Output = NumOpResult<SignedAmount>;
fn add(self, rhs: SignedAmount) -> Self::Output { rhs + self }
}
impl ops::Sub<SignedAmount> for NumOpResult<SignedAmount> {
type Output = NumOpResult<SignedAmount>;
fn sub(self, rhs: SignedAmount) -> Self::Output {
match self {
R::Valid(amount) => amount - rhs,
R::Error(_) => self,
}
}
}
}
impl ops::Mul<u64> for Amount {
type Output = NumOpResult<Amount>;
impl ops::Mul<i64> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn mul(self, rhs: u64) -> Self::Output { self.checked_mul(rhs).valid_or_error() }
}
impl ops::Mul<&u64> for Amount {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: &u64) -> Self::Output { self.mul(*rhs) }
}
impl ops::Mul<u64> for &Amount {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: u64) -> Self::Output { (*self).mul(rhs) }
}
impl ops::Mul<&u64> for &Amount {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: &u64) -> Self::Output { self.mul(*rhs) }
}
impl ops::Div<u64> for Amount {
type Output = NumOpResult<Amount>;
fn div(self, rhs: u64) -> Self::Output { self.checked_div(rhs).valid_or_error() }
}
impl ops::Div<&u64> for Amount {
type Output = NumOpResult<Amount>;
fn div(self, rhs: &u64) -> Self::Output { self.div(*rhs) }
}
impl ops::Div<u64> for &Amount {
type Output = NumOpResult<Amount>;
fn div(self, rhs: u64) -> Self::Output { (*self).div(rhs) }
}
impl ops::Div<&u64> for &Amount {
type Output = NumOpResult<Amount>;
fn div(self, rhs: &u64) -> Self::Output { (*self).div(*rhs) }
}
impl ops::Rem<u64> for Amount {
type Output = NumOpResult<Amount>;
fn rem(self, modulus: u64) -> Self::Output { self.checked_rem(modulus).valid_or_error() }
}
impl ops::Rem<&u64> for Amount {
type Output = NumOpResult<Amount>;
fn rem(self, modulus: &u64) -> Self::Output { self.rem(*modulus) }
}
impl ops::Rem<u64> for &Amount {
type Output = NumOpResult<Amount>;
fn rem(self, modulus: u64) -> Self::Output { (*self).rem(modulus) }
}
impl ops::Rem<&u64> for &Amount {
type Output = NumOpResult<Amount>;
fn rem(self, modulus: &u64) -> Self::Output { (*self).rem(*modulus) }
}
// FIXME these two should be covered by generic impls below
impl ops::Add<Amount> for NumOpResult<Amount> {
type Output = NumOpResult<Amount>;
fn add(self, rhs: Amount) -> Self::Output { rhs + self }
}
impl ops::Add<&Amount> for NumOpResult<Amount> {
type Output = NumOpResult<Amount>;
fn add(self, rhs: &Amount) -> Self::Output { rhs + self }
}
impl ops::Add<SignedAmount> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
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<NumOpResult<SignedAmount>> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn add(self, rhs: NumOpResult<SignedAmount>) -> Self::Output { rhs.and_then(|a| a + self) }
}
impl ops::Add<NumOpResult<SignedAmount>> for &SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn add(self, rhs: NumOpResult<SignedAmount>) -> Self::Output { rhs.and_then(|a| a + self) }
}
impl ops::Add<SignedAmount> for NumOpResult<SignedAmount> {
type Output = NumOpResult<SignedAmount>;
fn add(self, rhs: SignedAmount) -> Self::Output { rhs + self }
}
impl ops::Add<&SignedAmount> for NumOpResult<SignedAmount> {
type Output = NumOpResult<SignedAmount>;
fn add(self, rhs: &SignedAmount) -> Self::Output { rhs + self }
}
impl ops::Sub<SignedAmount> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
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<NumOpResult<SignedAmount>> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn sub(self, rhs: NumOpResult<SignedAmount>) -> 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<NumOpResult<SignedAmount>> for &SignedAmount {
type Output = NumOpResult<SignedAmount>;
impl ops::Div<i64> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn sub(self, rhs: NumOpResult<SignedAmount>) -> 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<SignedAmount> for NumOpResult<SignedAmount> {
type Output = NumOpResult<SignedAmount>;
impl ops::Rem<i64> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
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<SignedAmount> {
type Output = NumOpResult<SignedAmount>;
fn sub(self, rhs: &SignedAmount) -> Self::Output {
match self {
R::Valid(amount) => amount - *rhs,
R::Error(_) => self,
}
}
}
impl ops::Mul<i64> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn mul(self, rhs: i64) -> Self::Output { self.checked_mul(rhs).valid_or_error() }
}
impl ops::Mul<&i64> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn mul(self, rhs: &i64) -> Self::Output { self.mul(*rhs) }
}
impl ops::Mul<i64> for &SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn mul(self, rhs: i64) -> Self::Output { (*self).mul(rhs) }
}
impl ops::Mul<&i64> for &SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn mul(self, rhs: &i64) -> Self::Output { self.mul(*rhs) }
}
impl ops::Div<i64> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn div(self, rhs: i64) -> Self::Output { self.checked_div(rhs).valid_or_error() }
}
impl ops::Div<&i64> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn div(self, rhs: &i64) -> Self::Output { self.div(*rhs) }
}
impl ops::Div<i64> for &SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn div(self, rhs: i64) -> Self::Output { (*self).div(rhs) }
}
impl ops::Div<&i64> for &SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn div(self, rhs: &i64) -> Self::Output { (*self).div(*rhs) }
}
impl ops::Rem<i64> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn rem(self, modulus: i64) -> Self::Output { self.checked_rem(modulus).valid_or_error() }
}
impl ops::Rem<&i64> for SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn rem(self, modulus: &i64) -> Self::Output { self.rem(*modulus) }
}
impl ops::Rem<i64> for &SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn rem(self, modulus: i64) -> Self::Output { (*self).rem(modulus) }
}
impl ops::Rem<&i64> for &SignedAmount {
type Output = NumOpResult<SignedAmount>;
fn rem(self, modulus: &i64) -> Self::Output { (*self).rem(*modulus) }
}
impl ops::Neg for SignedAmount {
type Output = Self;

View File

@ -135,19 +135,19 @@ impl From<FeeRate> for u64 {
fn from(value: FeeRate) -> Self { value.to_sat_per_kwu() }
}
impl ops::Add<FeeRate> for FeeRate {
type Output = FeeRate;
crate::internal_macros::impl_op_for_references! {
impl ops::Add<FeeRate> 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<FeeRate> 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<FeeRate> 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);

View File

@ -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<T> for T'. Adds impls of:
///
/// - Add<T> 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<T> for T'. Adds impls of:
///
/// - Add<T> 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<T> for T'. Adds impls of:
///
/// - Sub<T> 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<T> for T'. Adds impls of:
///
/// - Sub<T> 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) => {

View File

@ -166,50 +166,46 @@ impl From<Weight> for u64 {
fn from(value: Weight) -> Self { value.to_wu() }
}
impl ops::Add<Weight> for Weight {
type Output = Weight;
crate::internal_macros::impl_op_for_references! {
impl ops::Add<Weight> 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<Weight> for Weight {
type Output = Weight;
fn sub(self, rhs: Weight) -> Self::Output { Weight(self.0 - rhs.0) }
}
impl ops::Mul<u64> for Weight {
type Output = Weight;
fn mul(self, rhs: u64) -> Self::Output { Weight(self.0 * rhs) }
}
impl ops::Mul<Weight> for u64 {
type Output = Weight;
fn mul(self, rhs: Weight) -> Self::Output { Weight(self * rhs.0) }
}
impl ops::Div<u64> for Weight {
type Output = Weight;
fn div(self, rhs: u64) -> Self::Output { Weight(self.0 / rhs) }
}
impl ops::Div<Weight> 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<Weight> 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<u64> for Weight {
type Output = Weight;
fn mul(self, rhs: u64) -> Self::Output { Weight(self.0 * rhs) }
}
impl ops::Mul<Weight> for u64 {
type Output = Weight;
fn mul(self, rhs: Weight) -> Self::Output { Weight(self * rhs.0) }
}
impl ops::MulAssign<u64> for Weight {
fn mul_assign(&mut self, rhs: u64) { self.0 *= rhs }
}
impl ops::Div<u64> for Weight {
type Output = Weight;
fn div(self, rhs: u64) -> Self::Output { Weight(self.0 / rhs) }
}
impl ops::Div<Weight> for Weight {
type Output = u64;
fn div(self, rhs: Weight) -> Self::Output { self.to_wu() / rhs.to_wu() }
}
impl ops::DivAssign<u64> for Weight {
fn div_assign(&mut self, rhs: u64) { self.0 /= rhs }
}