Implement FeeRate checked_sub

This commit is contained in:
yancy 2024-09-21 09:33:58 -05:00
parent 212a751929
commit cb2146d5fa
1 changed files with 14 additions and 0 deletions

View File

@ -101,6 +101,11 @@ impl FeeRate {
/// Computes `self + rhs` returning [`None`] if overflow occured.
pub fn checked_add(self, rhs: u64) -> Option<Self> { self.0.checked_add(rhs).map(Self) }
/// Checked subtraction.
///
/// Computes `self - rhs` returning [`None`] if overflow occured.
pub fn checked_sub(self, rhs: u64) -> Option<Self> { self.0.checked_sub(rhs).map(Self) }
/// Calculates the fee by multiplying this fee rate by weight, in weight units, returning [`None`]
/// if an overflow occurred.
///
@ -300,6 +305,15 @@ mod tests {
assert!(f.is_none());
}
#[test]
fn checked_sub() {
let f = FeeRate(2).checked_sub(1).unwrap();
assert_eq!(FeeRate(1), f);
let f = FeeRate::ZERO.checked_sub(1);
assert!(f.is_none());
}
#[test]
fn fee_rate_const_test() {
assert_eq!(0, FeeRate::ZERO.to_sat_per_kwu());