diff --git a/units/src/amount/mod.rs b/units/src/amount/mod.rs index 8928cea9d..6fb09fbcf 100644 --- a/units/src/amount/mod.rs +++ b/units/src/amount/mod.rs @@ -35,11 +35,9 @@ pub use self::{ OutOfRangeError, ParseAmountError, ParseDenominationError, ParseError, PossiblyConfusingDenominationError, TooPreciseError, UnknownDenominationError, }, - result::{NumOpError, NumOpResult}, signed::SignedAmount, unsigned::Amount, }; -pub(crate) use self::result::OptionExt; /// A set of denominations in which amounts can be expressed. /// diff --git a/units/src/amount/result.rs b/units/src/amount/result.rs index c36584b1c..a0ad26954 100644 --- a/units/src/amount/result.rs +++ b/units/src/amount/result.rs @@ -2,109 +2,12 @@ //! Provides a monodic type returned by mathematical operations (`core::ops`). -use core::{fmt, ops}; +use core::ops; use NumOpResult as R; use super::{Amount, SignedAmount}; - -/// Result of a mathematical operation on two numeric types. -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -#[must_use] -pub enum NumOpResult { - /// Result of a successful mathematical operation. - Valid(T), - /// Result of an unsuccessful mathematical operation. - Error(NumOpError), -} - -impl NumOpResult { - /// Returns the contained valid numeric type, consuming `self`. - /// - /// # Panics - /// - /// Panics with `msg` if the numeric result is an `Error`. - #[inline] - #[track_caller] - pub fn expect(self, msg: &str) -> T { - match self { - R::Valid(x) => x, - R::Error(_) => panic!("{}", msg), - } - } - - /// Returns the contained valid numeric type, consuming `self`. - /// - /// # Panics - /// - /// Panics if the numeric result is an `Error`. - #[inline] - #[track_caller] - pub fn unwrap(self) -> T { - match self { - R::Valid(x) => x, - R::Error(e) => panic!("tried to unwrap an invalid numeric result: {:?}", e), - } - } - - /// Returns the contained error, consuming `self`. - /// - /// # Panics - /// - /// Panics if the numeric result is valid. - #[inline] - #[track_caller] - pub fn unwrap_err(self) -> NumOpError { - match self { - R::Error(e) => e, - R::Valid(a) => panic!("tried to unwrap a valid numeric result: {:?}", a), - } - } - - /// Converts this `NumOpResult` to an `Option`. - #[inline] - pub fn ok(self) -> Option { - match self { - R::Valid(x) => Some(x), - R::Error(_) => None, - } - } - - /// Converts this `NumOpResult` to a `Result`. - #[inline] - #[allow(clippy::missing_errors_doc)] - pub fn into_result(self) -> Result { - match self { - R::Valid(x) => Ok(x), - R::Error(e) => Err(e), - } - } - - /// Calls `op` if the numeric result is `Valid`, otherwise returns the `Error` value of `self`. - #[inline] - pub fn and_then(self, op: F) -> NumOpResult - where - F: FnOnce(T) -> NumOpResult, - { - match self { - R::Valid(x) => op(x), - R::Error(e) => R::Error(e), - } - } - - /// Returns `true` if the numeric result is valid. - #[inline] - pub fn is_valid(&self) -> bool { - match self { - R::Valid(_) => true, - R::Error(_) => false, - } - } - - /// Returns `true` if the numeric result is invalid. - #[inline] - pub fn is_error(&self) -> bool { !self.is_valid() } -} +use crate::{NumOpError, NumOpResult, OptionExt}; impl From for NumOpResult { fn from(a: Amount) -> Self { Self::Valid(a) } @@ -376,41 +279,3 @@ impl<'a> core::iter::Sum<&'a NumOpResult> for NumOpResult { - fn valid_or_error(self) -> NumOpResult; -} - -impl OptionExt for Option { - #[inline] - fn valid_or_error(self) -> NumOpResult { - match self { - Some(amount) => R::Valid(amount), - None => R::Error(NumOpError {}), - } - } -} - -impl OptionExt for Option { - #[inline] - fn valid_or_error(self) -> NumOpResult { - match self { - Some(amount) => R::Valid(amount), - None => R::Error(NumOpError {}), - } - } -} - -/// An error occurred while doing a mathematical operation. -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -#[non_exhaustive] -pub struct NumOpError; - -impl fmt::Display for NumOpError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "a math operation gave an invalid numeric result") - } -} - -#[cfg(feature = "std")] -impl std::error::Error for NumOpError {} diff --git a/units/src/amount/tests.rs b/units/src/amount/tests.rs index f8aba133f..2dbefee14 100644 --- a/units/src/amount/tests.rs +++ b/units/src/amount/tests.rs @@ -13,6 +13,7 @@ use std::panic; use ::serde::{Deserialize, Serialize}; use super::*; +use crate::NumOpResult; #[cfg(feature = "alloc")] use crate::{FeeRate, Weight}; diff --git a/units/src/fee.rs b/units/src/fee.rs index 21d223c9c..5d277111e 100644 --- a/units/src/fee.rs +++ b/units/src/fee.rs @@ -13,8 +13,7 @@ use core::ops; -use crate::amount::{NumOpResult, OptionExt}; -use crate::{Amount, FeeRate, Weight}; +use crate::{Amount, FeeRate, NumOpResult, OptionExt, Weight}; impl Amount { /// Checked weight ceiling division. diff --git a/units/src/lib.rs b/units/src/lib.rs index 4a166ebb2..9d44f5ad8 100644 --- a/units/src/lib.rs +++ b/units/src/lib.rs @@ -20,6 +20,7 @@ extern crate std; mod fee; mod internal_macros; +mod result; #[doc(hidden)] pub mod _export { @@ -43,6 +44,8 @@ pub use self::{ amount::{Amount, SignedAmount}, block::{BlockHeight, BlockInterval}, fee_rate::FeeRate, + result::{NumOpError, NumOpResult}, time::BlockTime, weight::Weight }; +pub(crate) use self::result::OptionExt; diff --git a/units/src/result.rs b/units/src/result.rs new file mode 100644 index 000000000..afe268875 --- /dev/null +++ b/units/src/result.rs @@ -0,0 +1,145 @@ +// SPDX-License-Identifier: CC0-1.0 + +//! Provides a monodic type returned by mathematical operations (`core::ops`). + +use core::fmt; + +use NumOpResult as R; + +use crate::{Amount, SignedAmount}; + +/// Result of a mathematical operation on two numeric types. +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[must_use] +pub enum NumOpResult { + /// Result of a successful mathematical operation. + Valid(T), + /// Result of an unsuccessful mathematical operation. + Error(NumOpError), +} + +impl NumOpResult { + /// Returns the contained valid numeric type, consuming `self`. + /// + /// # Panics + /// + /// Panics with `msg` if the numeric result is an `Error`. + #[inline] + #[track_caller] + pub fn expect(self, msg: &str) -> T { + match self { + R::Valid(x) => x, + R::Error(_) => panic!("{}", msg), + } + } + + /// Returns the contained valid numeric type, consuming `self`. + /// + /// # Panics + /// + /// Panics if the numeric result is an `Error`. + #[inline] + #[track_caller] + pub fn unwrap(self) -> T { + match self { + R::Valid(x) => x, + R::Error(e) => panic!("tried to unwrap an invalid numeric result: {:?}", e), + } + } + + /// Returns the contained error, consuming `self`. + /// + /// # Panics + /// + /// Panics if the numeric result is valid. + #[inline] + #[track_caller] + pub fn unwrap_err(self) -> NumOpError { + match self { + R::Error(e) => e, + R::Valid(a) => panic!("tried to unwrap a valid numeric result: {:?}", a), + } + } + + /// Converts this `NumOpResult` to an `Option`. + #[inline] + pub fn ok(self) -> Option { + match self { + R::Valid(x) => Some(x), + R::Error(_) => None, + } + } + + /// Converts this `NumOpResult` to a `Result`. + #[inline] + #[allow(clippy::missing_errors_doc)] + pub fn into_result(self) -> Result { + match self { + R::Valid(x) => Ok(x), + R::Error(e) => Err(e), + } + } + + /// Calls `op` if the numeric result is `Valid`, otherwise returns the `Error` value of `self`. + #[inline] + pub fn and_then(self, op: F) -> NumOpResult + where + F: FnOnce(T) -> NumOpResult, + { + match self { + R::Valid(x) => op(x), + R::Error(e) => R::Error(e), + } + } + + /// Returns `true` if the numeric result is valid. + #[inline] + pub fn is_valid(&self) -> bool { + match self { + R::Valid(_) => true, + R::Error(_) => false, + } + } + + /// Returns `true` if the numeric result is invalid. + #[inline] + pub fn is_error(&self) -> bool { !self.is_valid() } +} + +pub(crate) trait OptionExt { + fn valid_or_error(self) -> NumOpResult; +} + +impl OptionExt for Option { + #[inline] + fn valid_or_error(self) -> NumOpResult { + match self { + Some(amount) => R::Valid(amount), + None => R::Error(NumOpError {}), + } + } +} + +impl OptionExt for Option { + #[inline] + fn valid_or_error(self) -> NumOpResult { + match self { + Some(amount) => R::Valid(amount), + None => R::Error(NumOpError {}), + } + } +} + +/// An error occurred while doing a mathematical operation. +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[non_exhaustive] +pub struct NumOpError; + +impl fmt::Display for NumOpError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "a math operation gave an invalid numeric result") + } +} + +#[cfg(feature = "std")] +impl std::error::Error for NumOpError {}