From 6ce5385914269dcf968e1fb26efc012f2072ee16 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 11 Dec 2024 15:49:01 +1100 Subject: [PATCH] units: Add macros for Add and Sub Add macros for implementing `ops::Add` and `ops::Sub` for various combinations of references. Use the macro in `fee_rate`. These are internal macros so no need to protect `core` using `$crate::_export::_core`. --- units/src/fee_rate.rs | 38 ++------------------- units/src/internal_macros.rs | 65 ++++++++++++++++++++++++++++++++++++ units/src/lib.rs | 2 ++ 3 files changed, 69 insertions(+), 36 deletions(-) create mode 100644 units/src/internal_macros.rs diff --git a/units/src/fee_rate.rs b/units/src/fee_rate.rs index 65fd58e77..eb3995ad4 100644 --- a/units/src/fee_rate.rs +++ b/units/src/fee_rate.rs @@ -190,48 +190,14 @@ impl Add for FeeRate { fn add(self, rhs: FeeRate) -> Self::Output { FeeRate(self.0 + rhs.0) } } - -impl Add for &FeeRate { - type Output = FeeRate; - - fn add(self, rhs: FeeRate) -> Self::Output { FeeRate(self.0 + rhs.0) } -} - -impl Add<&FeeRate> for FeeRate { - type Output = FeeRate; - - fn add(self, rhs: &FeeRate) -> Self::Output { FeeRate(self.0 + rhs.0) } -} - -impl<'a> Add<&'a FeeRate> for &FeeRate { - type Output = FeeRate; - - fn add(self, rhs: &'a FeeRate) -> Self::Output { FeeRate(self.0 + rhs.0) } -} +crate::internal_macros::impl_add_for_references!(FeeRate); impl Sub for FeeRate { type Output = FeeRate; fn sub(self, rhs: FeeRate) -> Self::Output { FeeRate(self.0 - rhs.0) } } - -impl Sub for &FeeRate { - type Output = FeeRate; - - fn sub(self, rhs: FeeRate) -> Self::Output { FeeRate(self.0 - rhs.0) } -} - -impl Sub<&FeeRate> for FeeRate { - type Output = FeeRate; - - fn sub(self, rhs: &FeeRate) -> Self::Output { FeeRate(self.0 - rhs.0) } -} - -impl<'a> Sub<&'a FeeRate> for &FeeRate { - type Output = FeeRate; - - fn sub(self, rhs: &'a FeeRate) -> Self::Output { FeeRate(self.0 - rhs.0) } -} +crate::internal_macros::impl_sub_for_references!(FeeRate); /// Computes the ceiling so that the fee computation is conservative. impl Mul for Weight { diff --git a/units/src/internal_macros.rs b/units/src/internal_macros.rs new file mode 100644 index 000000000..bd4ce335b --- /dev/null +++ b/units/src/internal_macros.rs @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: CC0-1.0 + +//! Internal macros. +//! +//! Macros meant to be used inside the `bitcoin-units` library. + +/// 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; + +/// 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; diff --git a/units/src/lib.rs b/units/src/lib.rs index f97bc430f..ffcbfa3d7 100644 --- a/units/src/lib.rs +++ b/units/src/lib.rs @@ -23,6 +23,8 @@ extern crate alloc; #[cfg(feature = "std")] extern crate std; +mod internal_macros; + #[doc(hidden)] pub mod _export { /// A re-export of core::*