units: Remove mention of amount in result module

We currently use the `NumOpResult` for operations involving more than
just amount types (e.g. `FeeRate`) however when the `result` module was
written we only used amount types.

To make the docs and code clearer use 'numeric type' instead of
'amount' in docs. And for local variables use `x` instead of `amount`.

This is docs and internal changes only.
This commit is contained in:
Tobin C. Harding 2025-04-07 10:55:09 +10:00
parent 263db9057b
commit f49efdf3f7
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 13 additions and 15 deletions

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: CC0-1.0 // SPDX-License-Identifier: CC0-1.0
//! Provides a monodic type used when mathematical operations (`core::ops`) return an amount type. //! Provides a monodic type returned by mathematical operations (`core::ops`).
use core::{fmt, ops}; use core::{fmt, ops};
@ -8,9 +8,7 @@ use NumOpResult as R;
use super::{Amount, SignedAmount}; use super::{Amount, SignedAmount};
/// Result of an operation on [`Amount`] or [`SignedAmount`]. /// Result of a mathematical operation on two numeric types.
///
/// The type parameter `T` should be normally `Amount` or `SignedAmount`.
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[must_use] #[must_use]
pub enum NumOpResult<T> { pub enum NumOpResult<T> {
@ -21,7 +19,7 @@ pub enum NumOpResult<T> {
} }
impl<T: fmt::Debug> NumOpResult<T> { impl<T: fmt::Debug> NumOpResult<T> {
/// Returns the contained valid amount, consuming `self`. /// Returns the contained valid numeric type, consuming `self`.
/// ///
/// # Panics /// # Panics
/// ///
@ -30,12 +28,12 @@ impl<T: fmt::Debug> NumOpResult<T> {
#[track_caller] #[track_caller]
pub fn expect(self, msg: &str) -> T { pub fn expect(self, msg: &str) -> T {
match self { match self {
R::Valid(amount) => amount, R::Valid(x) => x,
R::Error(_) => panic!("{}", msg), R::Error(_) => panic!("{}", msg),
} }
} }
/// Returns the contained valid amount, consuming `self`. /// Returns the contained valid numeric type, consuming `self`.
/// ///
/// # Panics /// # Panics
/// ///
@ -44,7 +42,7 @@ impl<T: fmt::Debug> NumOpResult<T> {
#[track_caller] #[track_caller]
pub fn unwrap(self) -> T { pub fn unwrap(self) -> T {
match self { match self {
R::Valid(amount) => amount, R::Valid(x) => x,
R::Error(e) => panic!("tried to unwrap an invalid numeric result: {:?}", e), R::Error(e) => panic!("tried to unwrap an invalid numeric result: {:?}", e),
} }
} }
@ -53,7 +51,7 @@ impl<T: fmt::Debug> NumOpResult<T> {
/// ///
/// # Panics /// # Panics
/// ///
/// Panics if the numeric result is a valid amount. /// Panics if the numeric result is valid.
#[inline] #[inline]
#[track_caller] #[track_caller]
pub fn unwrap_err(self) -> NumOpError { pub fn unwrap_err(self) -> NumOpError {
@ -67,7 +65,7 @@ impl<T: fmt::Debug> NumOpResult<T> {
#[inline] #[inline]
pub fn ok(self) -> Option<T> { pub fn ok(self) -> Option<T> {
match self { match self {
R::Valid(amount) => Some(amount), R::Valid(x) => Some(x),
R::Error(_) => None, R::Error(_) => None,
} }
} }
@ -77,7 +75,7 @@ impl<T: fmt::Debug> NumOpResult<T> {
#[allow(clippy::missing_errors_doc)] #[allow(clippy::missing_errors_doc)]
pub fn into_result(self) -> Result<T, NumOpError> { pub fn into_result(self) -> Result<T, NumOpError> {
match self { match self {
R::Valid(amount) => Ok(amount), R::Valid(x) => Ok(x),
R::Error(e) => Err(e), R::Error(e) => Err(e),
} }
} }
@ -89,12 +87,12 @@ impl<T: fmt::Debug> NumOpResult<T> {
F: FnOnce(T) -> NumOpResult<T>, F: FnOnce(T) -> NumOpResult<T>,
{ {
match self { match self {
R::Valid(amount) => op(amount), R::Valid(x) => op(x),
R::Error(e) => R::Error(e), R::Error(e) => R::Error(e),
} }
} }
/// Returns `true` if the numeric result is a valid amount. /// Returns `true` if the numeric result is valid.
#[inline] #[inline]
pub fn is_valid(&self) -> bool { pub fn is_valid(&self) -> bool {
match self { match self {
@ -103,7 +101,7 @@ impl<T: fmt::Debug> NumOpResult<T> {
} }
} }
/// Returns `true` if the numeric result is an invalid amount. /// Returns `true` if the numeric result is invalid.
#[inline] #[inline]
pub fn is_error(&self) -> bool { !self.is_valid() } pub fn is_error(&self) -> bool { !self.is_valid() }
} }
@ -410,7 +408,7 @@ pub struct NumOpError;
impl fmt::Display for NumOpError { impl fmt::Display for NumOpError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "a math operation on amounts gave an invalid numeric result") write!(f, "a math operation gave an invalid numeric result")
} }
} }