From 86359fe364dc6044c0e5210d5a5aeee78ac688a2 Mon Sep 17 00:00:00 2001 From: yancy Date: Sat, 21 Sep 2024 09:21:42 -0500 Subject: [PATCH] Implement FeeRate addition --- units/src/fee_rate.rs | 45 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/units/src/fee_rate.rs b/units/src/fee_rate.rs index 626fd34d3..0eef2346e 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::{Div, Mul}; +use core::ops::{Add, Div, Mul}; #[cfg(feature = "arbitrary")] use arbitrary::{Arbitrary, Unstructured}; @@ -135,6 +135,36 @@ impl From for u64 { fn from(value: FeeRate) -> Self { value.to_sat_per_kwu() } } +impl Add for FeeRate { + type Output = FeeRate; + + fn add(self, rhs: FeeRate) -> Self::Output { FeeRate(self.0 + rhs.0) } +} + +impl Add for &FeeRate { + type Output = FeeRate; + + fn add(self, other: FeeRate) -> ::Output { + FeeRate(self.0 + other.0) + } +} + +impl Add<&FeeRate> for FeeRate { + type Output = FeeRate; + + fn add(self, other: &FeeRate) -> ::Output { + FeeRate(self.0 + other.0) + } +} + +impl<'a, 'b> Add<&'a FeeRate> for &'b FeeRate { + type Output = FeeRate; + + fn add(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; @@ -162,6 +192,19 @@ crate::impl_parse_str_from_int_infallible!(FeeRate, u64, from_sat_per_kwu); mod tests { use super::*; + #[test] + #[allow(clippy::op_ref)] + fn addition() { + let one = FeeRate(1); + let two = FeeRate(2); + let three = FeeRate(3); + + assert!(one + two == three); + assert!(&one + two == three); + assert!(one + &two == three); + assert!(&one + &two == three); + } + #[test] fn fee_rate_const_test() { assert_eq!(0, FeeRate::ZERO.to_sat_per_kwu());