Merge rust-bitcoin/rust-bitcoin#4337: Add impls for `NumOpResult` div and mul

0361604bab Add impls for NumOpResult div and mul (Tobin C. Harding)

Pull request description:

  We recently added div and mul for combinations of `Amount`, `FeeRate`, and `Weight`. When doing so we forgot to add variations for `NumOpResult`.

ACKs for top commit:
  apoelstra:
    ACK 0361604bab6c6ef260410d0bd6e33ce24a41e775; successfully ran local tests

Tree-SHA512: 6d262b9079b8a670f32d58d49e3c7e9a79d5d795a4c9f37f6bc2213879649d41900e95f515d8685c3870c935358bcb25567b2f6f332301e1ad88188056047b7b
This commit is contained in:
merge-script 2025-04-15 13:20:09 +00:00
commit e5b0f9cd92
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 128 additions and 0 deletions

View File

@ -15,6 +15,8 @@ use core::ops;
use crate::{Amount, FeeRate, MathOp, NumOpResult, OptionExt, Weight}; use crate::{Amount, FeeRate, MathOp, NumOpResult, OptionExt, Weight};
use NumOpResult as R;
impl Amount { impl Amount {
/// Checked weight ceiling division. /// 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) rhs.checked_mul_by_weight(self).valid_or_error(MathOp::Mul)
} }
} }
impl ops::Mul<FeeRate> for NumOpResult<Weight> {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: FeeRate) -> Self::Output {
match self {
R::Valid(lhs) => lhs * rhs,
R::Error(e) => NumOpResult::Error(e),
}
}
}
impl ops::Mul<NumOpResult<FeeRate>> for Weight {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: NumOpResult<FeeRate>) -> Self::Output {
match rhs {
R::Valid(fee_rate) => self * fee_rate,
R::Error(e) => NumOpResult::Error(e),
}
}
}
impl ops::Mul<NumOpResult<FeeRate>> for NumOpResult<Weight> {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: NumOpResult<FeeRate>) -> 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<Weight> for FeeRate { impl ops::Mul<Weight> for FeeRate {
type Output = NumOpResult<Amount>; type Output = NumOpResult<Amount>;
@ -175,6 +207,36 @@ crate::internal_macros::impl_op_for_references! {
self.checked_mul_by_weight(rhs).valid_or_error(MathOp::Mul) self.checked_mul_by_weight(rhs).valid_or_error(MathOp::Mul)
} }
} }
impl ops::Mul<Weight> for NumOpResult<FeeRate> {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: Weight) -> Self::Output {
match self {
R::Valid(lhs) => lhs * rhs,
R::Error(e) => NumOpResult::Error(e),
}
}
}
impl ops::Mul<NumOpResult<Weight>> for FeeRate {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: NumOpResult<Weight>) -> Self::Output {
match rhs {
R::Valid(weight) => self * weight,
R::Error(e) => NumOpResult::Error(e),
}
}
}
impl ops::Mul<NumOpResult<Weight>> for NumOpResult<FeeRate> {
type Output = NumOpResult<Amount>;
fn mul(self, rhs: NumOpResult<Weight>) -> 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<Weight> for Amount { impl ops::Div<Weight> for Amount {
type Output = NumOpResult<FeeRate>; type Output = NumOpResult<FeeRate>;
@ -183,6 +245,39 @@ crate::internal_macros::impl_op_for_references! {
self.checked_div_by_weight_floor(rhs).valid_or_error(MathOp::Div) self.checked_div_by_weight_floor(rhs).valid_or_error(MathOp::Div)
} }
} }
impl ops::Div<Weight> for NumOpResult<Amount> {
type Output = NumOpResult<FeeRate>;
fn div(self, rhs: Weight) -> Self::Output {
match self {
R::Valid(lhs) => lhs / rhs,
R::Error(e) => NumOpResult::Error(e),
}
}
}
impl ops::Div<NumOpResult<Weight>> for Amount {
type Output = NumOpResult<FeeRate>;
fn div(self, rhs: NumOpResult<Weight>) -> Self::Output {
match rhs {
R::Valid(weight) => self / weight,
R::Error(e) => NumOpResult::Error(e),
}
}
}
impl ops::Div<NumOpResult<Weight>> for NumOpResult<Amount> {
type Output = NumOpResult<FeeRate>;
fn div(self, rhs: NumOpResult<Weight>) -> 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<FeeRate> for Amount { impl ops::Div<FeeRate> for Amount {
type Output = NumOpResult<Weight>; type Output = NumOpResult<Weight>;
@ -191,6 +286,39 @@ crate::internal_macros::impl_op_for_references! {
self.checked_div_by_fee_rate_floor(rhs).valid_or_error(MathOp::Div) self.checked_div_by_fee_rate_floor(rhs).valid_or_error(MathOp::Div)
} }
} }
impl ops::Div<FeeRate> for NumOpResult<Amount> {
type Output = NumOpResult<Weight>;
fn div(self, rhs: FeeRate) -> Self::Output {
match self {
R::Valid(lhs) => lhs / rhs,
R::Error(e) => NumOpResult::Error(e),
}
}
}
impl ops::Div<NumOpResult<FeeRate>> for Amount {
type Output = NumOpResult<Weight>;
fn div(self, rhs: NumOpResult<FeeRate>) -> Self::Output {
match rhs {
R::Valid(fee_rate) => self / fee_rate,
R::Error(e) => NumOpResult::Error(e),
}
}
}
impl ops::Div<NumOpResult<FeeRate>> for NumOpResult<Amount> {
type Output = NumOpResult<Weight>;
fn div(self, rhs: NumOpResult<FeeRate>) -> 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 { impl Weight {