From f49efdf3f751e3992700841d0888a2993beb5956 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 7 Apr 2025 10:55:09 +1000 Subject: [PATCH] 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. --- units/src/amount/result.rs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/units/src/amount/result.rs b/units/src/amount/result.rs index ab06f207f..c36584b1c 100644 --- a/units/src/amount/result.rs +++ b/units/src/amount/result.rs @@ -1,6 +1,6 @@ // 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}; @@ -8,9 +8,7 @@ use NumOpResult as R; use super::{Amount, SignedAmount}; -/// Result of an operation on [`Amount`] or [`SignedAmount`]. -/// -/// The type parameter `T` should be normally `Amount` or `SignedAmount`. +/// Result of a mathematical operation on two numeric types. #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[must_use] pub enum NumOpResult { @@ -21,7 +19,7 @@ pub enum NumOpResult { } impl NumOpResult { - /// Returns the contained valid amount, consuming `self`. + /// Returns the contained valid numeric type, consuming `self`. /// /// # Panics /// @@ -30,12 +28,12 @@ impl NumOpResult { #[track_caller] pub fn expect(self, msg: &str) -> T { match self { - R::Valid(amount) => amount, + R::Valid(x) => x, R::Error(_) => panic!("{}", msg), } } - /// Returns the contained valid amount, consuming `self`. + /// Returns the contained valid numeric type, consuming `self`. /// /// # Panics /// @@ -44,7 +42,7 @@ impl NumOpResult { #[track_caller] pub fn unwrap(self) -> T { match self { - R::Valid(amount) => amount, + R::Valid(x) => x, R::Error(e) => panic!("tried to unwrap an invalid numeric result: {:?}", e), } } @@ -53,7 +51,7 @@ impl NumOpResult { /// /// # Panics /// - /// Panics if the numeric result is a valid amount. + /// Panics if the numeric result is valid. #[inline] #[track_caller] pub fn unwrap_err(self) -> NumOpError { @@ -67,7 +65,7 @@ impl NumOpResult { #[inline] pub fn ok(self) -> Option { match self { - R::Valid(amount) => Some(amount), + R::Valid(x) => Some(x), R::Error(_) => None, } } @@ -77,7 +75,7 @@ impl NumOpResult { #[allow(clippy::missing_errors_doc)] pub fn into_result(self) -> Result { match self { - R::Valid(amount) => Ok(amount), + R::Valid(x) => Ok(x), R::Error(e) => Err(e), } } @@ -89,12 +87,12 @@ impl NumOpResult { F: FnOnce(T) -> NumOpResult, { match self { - R::Valid(amount) => op(amount), + R::Valid(x) => op(x), 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] pub fn is_valid(&self) -> bool { match self { @@ -103,7 +101,7 @@ impl NumOpResult { } } - /// Returns `true` if the numeric result is an invalid amount. + /// Returns `true` if the numeric result is invalid. #[inline] pub fn is_error(&self) -> bool { !self.is_valid() } } @@ -410,7 +408,7 @@ pub struct NumOpError; impl fmt::Display for NumOpError { 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") } }