diff --git a/units/src/fee_rate.rs b/units/src/fee_rate.rs index 0eef2346e..804cab2e5 100644 --- a/units/src/fee_rate.rs +++ b/units/src/fee_rate.rs @@ -3,7 +3,7 @@ //! Implements `FeeRate` and assoctiated features. use core::fmt; -use core::ops::{Add, Div, Mul}; +use core::ops::{Add, Sub, Div, Mul}; #[cfg(feature = "arbitrary")] use arbitrary::{Arbitrary, Unstructured}; @@ -165,6 +165,36 @@ impl<'a, 'b> Add<&'a FeeRate> for &'b 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, other: FeeRate) -> ::Output { + FeeRate(self.0 - other.0) + } +} + +impl Sub<&FeeRate> for FeeRate { + type Output = FeeRate; + + fn sub(self, other: &FeeRate) -> ::Output { + FeeRate(self.0 - other.0) + } +} + +impl<'a, 'b> Sub<&'a FeeRate> for &'b FeeRate { + type Output = FeeRate; + + fn sub(self, other: &'a FeeRate) -> ::Output { + FeeRate(self.0 - other.0) + } +} + /// Computes the ceiling so that the fee computation is conservative. impl Mul for Weight { type Output = Amount; @@ -205,6 +235,19 @@ mod tests { assert!(&one + &two == three); } + #[test] + #[allow(clippy::op_ref)] + fn subtract() { + let one = FeeRate(1); + let two = FeeRate(2); + let three = FeeRate(3); + + assert!(three - two == one); + assert!(&three - two == one); + assert!(three - &two == one); + assert!(&three - &two == one); + } + #[test] fn fee_rate_const_test() { assert_eq!(0, FeeRate::ZERO.to_sat_per_kwu());