diff --git a/units/src/fee.rs b/units/src/fee.rs index 43bad6231..16eb0e5df 100644 --- a/units/src/fee.rs +++ b/units/src/fee.rs @@ -27,17 +27,12 @@ use core::ops; use NumOpResult as R; -use crate::{Amount, FeeRate, MathOp, NumOpError as E, NumOpResult, Weight}; +use crate::{Amount, FeeRate, NumOpResult, Weight}; crate::internal_macros::impl_op_for_references! { impl ops::Mul for Weight { type Output = NumOpResult; - fn mul(self, rhs: FeeRate) -> Self::Output { - match rhs.mul_by_weight(self) { - Some(amount) => R::Valid(amount), - None => R::Error(E::while_doing(MathOp::Mul)), - } - } + fn mul(self, rhs: FeeRate) -> Self::Output { rhs.mul_by_weight(self) } } impl ops::Mul for NumOpResult { type Output = NumOpResult; @@ -72,12 +67,7 @@ crate::internal_macros::impl_op_for_references! { impl ops::Mul for FeeRate { type Output = NumOpResult; - fn mul(self, rhs: Weight) -> Self::Output { - match self.mul_by_weight(rhs) { - Some(amount) => R::Valid(amount), - None => R::Error(E::while_doing(MathOp::Mul)), - } - } + fn mul(self, rhs: Weight) -> Self::Output { self.mul_by_weight(rhs) } } impl ops::Mul for NumOpResult { type Output = NumOpResult; @@ -218,7 +208,7 @@ mod tests { assert_eq!(Amount::from_sat_u32(100), fee); let fee = FeeRate::from_sat_per_kwu(10).unwrap().mul_by_weight(Weight::MAX); - assert!(fee.is_none()); + assert!(fee.is_error()); let weight = Weight::from_vb(3).unwrap(); let fee_rate = FeeRate::from_sat_per_vb(3).unwrap(); diff --git a/units/src/fee_rate/mod.rs b/units/src/fee_rate/mod.rs index b19157cae..ddff10fee 100644 --- a/units/src/fee_rate/mod.rs +++ b/units/src/fee_rate/mod.rs @@ -196,8 +196,8 @@ impl FeeRate { pub const fn to_fee(self, weight: Weight) -> Amount { // No `unwrap_or()` in const context. match self.mul_by_weight(weight) { - Some(fee) => fee, - None => Amount::MAX, + NumOpResult::Valid(fee) => fee, + NumOpResult::Error(_) => Amount::MAX, } } @@ -207,7 +207,7 @@ impl FeeRate { /// This is equivalent to `Self::checked_mul_by_weight()`. #[must_use] #[deprecated(since = "TBD", note = "use `to_fee()` instead")] - pub fn fee_wu(self, weight: Weight) -> Option { self.mul_by_weight(weight) } + pub fn fee_wu(self, weight: Weight) -> Option { self.mul_by_weight(weight).ok() } /// Calculates the fee by multiplying this fee rate by weight, in virtual bytes, returning [`None`] /// if an overflow occurred. @@ -223,21 +223,18 @@ impl FeeRate { /// Computes the absolute fee amount for a given [`Weight`] at this fee rate. When the resulting /// fee is a non-integer amount, the amount is rounded up, ensuring that the transaction fee is /// enough instead of falling short if rounded down. - /// - /// Returns [`None`] if overflow occurred. - #[must_use] - pub const fn mul_by_weight(self, weight: Weight) -> Option { + pub const fn mul_by_weight(self, weight: Weight) -> NumOpResult { let wu = weight.to_wu(); if let Some(fee_kwu) = self.to_sat_per_kwu_floor().checked_mul(wu) { // Bump by 999 to do ceil division using kwu. if let Some(bump) = fee_kwu.checked_add(999) { let fee = bump / 1_000; if let Ok(fee_amount) = Amount::from_sat(fee) { - return Some(fee_amount); + return NumOpResult::Valid(fee_amount); } } } - None + NumOpResult::Error(E::while_doing(MathOp::Mul)) } } diff --git a/units/src/weight.rs b/units/src/weight.rs index 0589cb0f7..b0b93ce5a 100644 --- a/units/src/weight.rs +++ b/units/src/weight.rs @@ -10,7 +10,7 @@ use arbitrary::{Arbitrary, Unstructured}; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use crate::{Amount, CheckedSum, FeeRate}; +use crate::{Amount, CheckedSum, FeeRate, NumOpResult}; /// The factor that non-witness serialization data is multiplied by during weight calculation. pub const WITNESS_SCALE_FACTOR: usize = 4; @@ -168,10 +168,7 @@ impl Weight { /// Computes the absolute fee amount for a given [`FeeRate`] at this weight. When the resulting /// fee is a non-integer amount, the amount is rounded up, ensuring that the transaction fee is /// enough instead of falling short if rounded down. - /// - /// Returns [`None`] if overflow occurred. - #[must_use] - pub const fn mul_by_fee_rate(self, fee_rate: FeeRate) -> Option { + pub const fn mul_by_fee_rate(self, fee_rate: FeeRate) -> NumOpResult { fee_rate.mul_by_weight(self) } }