From 0361604bab6c6ef260410d0bd6e33ce24a41e775 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 14 Apr 2025 11:15:47 +1000 Subject: [PATCH] Add impls for NumOpResult div and mul We recently added div and mul for combinations of `Amount`, `FeeRate`, and `Weight`. When doing so we forgot to add variations for `NumOpResult`. --- units/src/fee.rs | 128 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/units/src/fee.rs b/units/src/fee.rs index c44bc8966..4bf0792a9 100644 --- a/units/src/fee.rs +++ b/units/src/fee.rs @@ -15,6 +15,8 @@ use core::ops; use crate::{Amount, FeeRate, MathOp, NumOpResult, OptionExt, Weight}; +use NumOpResult as R; + impl Amount { /// Checked weight ceiling division. /// @@ -168,6 +170,36 @@ crate::internal_macros::impl_op_for_references! { rhs.checked_mul_by_weight(self).valid_or_error(MathOp::Mul) } } + impl ops::Mul for NumOpResult { + type Output = NumOpResult; + fn mul(self, rhs: FeeRate) -> Self::Output { + match self { + R::Valid(lhs) => lhs * rhs, + R::Error(e) => NumOpResult::Error(e), + } + } + } + impl ops::Mul> for Weight { + type Output = NumOpResult; + fn mul(self, rhs: NumOpResult) -> Self::Output { + match rhs { + R::Valid(fee_rate) => self * fee_rate, + R::Error(e) => NumOpResult::Error(e), + } + } + } + impl ops::Mul> for NumOpResult { + type Output = NumOpResult; + fn mul(self, rhs: NumOpResult) -> Self::Output { + match self { + R::Valid(lhs) => { match rhs { + R::Valid(fee_rate) => lhs * fee_rate, + R::Error(e) => NumOpResult::Error(e), + }} + R::Error(e) => NumOpResult::Error(e), + } + } + } impl ops::Mul for FeeRate { type Output = NumOpResult; @@ -175,6 +207,36 @@ crate::internal_macros::impl_op_for_references! { self.checked_mul_by_weight(rhs).valid_or_error(MathOp::Mul) } } + impl ops::Mul for NumOpResult { + type Output = NumOpResult; + fn mul(self, rhs: Weight) -> Self::Output { + match self { + R::Valid(lhs) => lhs * rhs, + R::Error(e) => NumOpResult::Error(e), + } + } + } + impl ops::Mul> for FeeRate { + type Output = NumOpResult; + fn mul(self, rhs: NumOpResult) -> Self::Output { + match rhs { + R::Valid(weight) => self * weight, + R::Error(e) => NumOpResult::Error(e), + } + } + } + impl ops::Mul> for NumOpResult { + type Output = NumOpResult; + fn mul(self, rhs: NumOpResult) -> Self::Output { + match self { + R::Valid(lhs) => { match rhs { + R::Valid(weight) => lhs * weight, + R::Error(e) => NumOpResult::Error(e), + }} + R::Error(e) => NumOpResult::Error(e), + } + } + } impl ops::Div for Amount { type Output = NumOpResult; @@ -183,6 +245,39 @@ crate::internal_macros::impl_op_for_references! { self.checked_div_by_weight_floor(rhs).valid_or_error(MathOp::Div) } } + impl ops::Div for NumOpResult { + type Output = NumOpResult; + + fn div(self, rhs: Weight) -> Self::Output { + match self { + R::Valid(lhs) => lhs / rhs, + R::Error(e) => NumOpResult::Error(e), + } + } + } + impl ops::Div> for Amount { + type Output = NumOpResult; + + fn div(self, rhs: NumOpResult) -> Self::Output { + match rhs { + R::Valid(weight) => self / weight, + R::Error(e) => NumOpResult::Error(e), + } + } + } + impl ops::Div> for NumOpResult { + type Output = NumOpResult; + + fn div(self, rhs: NumOpResult) -> Self::Output { + match self { + R::Valid(lhs) => { match rhs { + R::Valid(weight) => lhs / weight, + R::Error(e) => NumOpResult::Error(e), + }} + R::Error(e) => NumOpResult::Error(e), + } + } + } impl ops::Div for Amount { type Output = NumOpResult; @@ -191,6 +286,39 @@ crate::internal_macros::impl_op_for_references! { self.checked_div_by_fee_rate_floor(rhs).valid_or_error(MathOp::Div) } } + impl ops::Div for NumOpResult { + type Output = NumOpResult; + + fn div(self, rhs: FeeRate) -> Self::Output { + match self { + R::Valid(lhs) => lhs / rhs, + R::Error(e) => NumOpResult::Error(e), + } + } + } + impl ops::Div> for Amount { + type Output = NumOpResult; + + fn div(self, rhs: NumOpResult) -> Self::Output { + match rhs { + R::Valid(fee_rate) => self / fee_rate, + R::Error(e) => NumOpResult::Error(e), + } + } + } + impl ops::Div> for NumOpResult { + type Output = NumOpResult; + + fn div(self, rhs: NumOpResult) -> Self::Output { + match self { + R::Valid(lhs) => { match rhs { + R::Valid(fee_rate) => lhs / fee_rate, + R::Error(e) => NumOpResult::Error(e), + }} + R::Error(e) => NumOpResult::Error(e), + } + } + } } impl Weight {