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:
parent
b31ca72b33
commit
6ce5385914
|
@ -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 {
|
||||
|
|
|
@ -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;
|
|
@ -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::*
|
||||
|
|
Loading…
Reference in New Issue