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`.
This commit is contained in:
Tobin C. Harding 2024-12-11 15:49:01 +11:00
parent b31ca72b33
commit 6ce5385914
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
3 changed files with 69 additions and 36 deletions

View File

@ -190,48 +190,14 @@ impl Add for 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 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<FeeRate> 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<FeeRate> for Weight {

View File

@ -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<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;
/// 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;

View File

@ -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::*