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:
commit
e5b0f9cd92
128
units/src/fee.rs
128
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<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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
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 {
|
||||
|
|
Loading…
Reference in New Issue