units: Move result module errors to error submodule
What changed, and why it matters
This commit is a simple internal code reorganization. It moves the definition of an error type (NumOpError) from one place in a file to a new 'error' submodule within the same file, then re-exports it so existing code can still use it the same way. There is no change to how the code behaves, no bug fix, and no security relevance.
No action needed; this is a non-security refactoring change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors units/src/result.rs by moving the NumOpError struct and its impl blocks into a new pub mod error submodule and adding a #[doc(no_inline)] re-export at the top of the file. The struct’s field visibility changes from a private tuple field Self(op) to pub(super) MathOp, but the constructors and public API remain identical. No logic, behavior, or security boundary changes.
Changed components
units/src/result.rsInspect captured patch +44 / −32
diff --git a/units/src/result.rs b/units/src/result.rs
index 0362c48b..6f6971f7 100644
--- a/units/src/result.rs
+++ b/units/src/result.rs
@@ -11,6 +11,10 @@ use NumOpResult as R;
use crate::{Amount, FeeRate, SignedAmount, Weight};
+#[rustfmt::skip] // Keep public re-exports separate.
+#[doc(no_inline)]
+pub use self::error::NumOpError;
+
/// Result of a mathematical operation on two numeric types.
///
/// In order to prevent overflow we provide a custom result type that is similar to the normal
@@ -326,38 +330,6 @@ macro_rules! impl_opt_ext {
}
impl_opt_ext!(Amount, SignedAmount, u64, i64, FeeRate, Weight);
-/// Error returned when a mathematical operation fails.
-#[derive(Debug, Copy, Clone, PartialEq, Eq)]
-#[non_exhaustive]
-pub struct NumOpError(MathOp);
-
-impl From<Infallible> for NumOpError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
-impl NumOpError {
- /// Constructs a [`NumOpError`] caused by `op`.
- pub(crate) const fn while_doing(op: MathOp) -> Self { Self(op) }
-
- /// Returns `true` if this operation error'ed due to overflow.
- pub fn is_overflow(self) -> bool { self.0.is_overflow() }
-
- /// Returns `true` if this operation error'ed due to division by zero.
- pub fn is_div_by_zero(self) -> bool { self.0.is_div_by_zero() }
-
- /// Returns the [`MathOp`] that caused this error.
- pub fn operation(self) -> MathOp { self.0 }
-}
-
-impl fmt::Display for NumOpError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "math operation '{}' gave an invalid numeric result", self.operation())
- }
-}
-
-#[cfg(feature = "std")]
-impl std::error::Error for NumOpError {}
-
/// The math operation that caused the error.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
@@ -416,6 +388,46 @@ impl fmt::Display for MathOp {
}
}
+/// Error types for mathematical operations.
+pub mod error {
+ use core::convert::Infallible;
+ use core::fmt;
+
+ use super::MathOp;
+
+ /// Error returned when a mathematical operation fails.
+ #[derive(Debug, Copy, Clone, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub struct NumOpError(pub(super) MathOp);
+
+ impl From<Infallible> for NumOpError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ impl NumOpError {
+ /// Constructs a [`NumOpError`] caused by `op`.
+ pub(crate) const fn while_doing(op: MathOp) -> Self { Self(op) }
+
+ /// Returns `true` if this operation error'ed due to overflow.
+ pub fn is_overflow(self) -> bool { self.0.is_overflow() }
+
+ /// Returns `true` if this operation error'ed due to division by zero.
+ pub fn is_div_by_zero(self) -> bool { self.0.is_div_by_zero() }
+
+ /// Returns the [`MathOp`] that caused this error.
+ pub fn operation(self) -> MathOp { self.0 }
+ }
+
+ impl fmt::Display for NumOpError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "math operation '{}' gave an invalid numeric result", self.operation())
+ }
+ }
+
+ #[cfg(feature = "std")]
+ impl std::error::Error for NumOpError {}
+}
+
#[cfg(feature = "arbitrary")]
impl<'a, T: Arbitrary<'a>> Arbitrary<'a> for NumOpResult<T> {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.