What changed, and why it matters
This commit simply moves existing Rust code from one file to another without changing what the code does. It is described by the project as an internal cleanup with no logic change, and the diff confirms that the implementations were copied verbatim.
No action required. This is a non-functional refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit relocates generic Add and Sub trait implementations for NumOpResult<T> from units/src/amount/result.rs to units/src/result.rs. The code is identical before and after the move, including the same bounds, match arms, and error handling. No new behavior is introduced, no signatures are altered, and no security-sensitive logic is modified.
Changed components
units/src/amount/result.rsunits/src/result.rsInspect captured patch +55 / −52
diff --git a/units/src/amount/result.rs b/units/src/amount/result.rs
index 01f8ab07..2f940fe4 100644
--- a/units/src/amount/result.rs
+++ b/units/src/amount/result.rs
@@ -187,57 +187,6 @@ crate::internal_macros::impl_op_for_references! {
fn rem(self, modulus: i64) -> Self::Output { self.and_then(|lhs| lhs % modulus) }
}
-
- impl<T> ops::Add<NumOpResult<T>> for NumOpResult<T>
- where
- (T: Copy + ops::Add<Output = NumOpResult<T>>)
- {
- type Output = NumOpResult<T>;
-
- fn add(self, rhs: Self) -> Self::Output {
- match (self, rhs) {
- (R::Valid(lhs), R::Valid(rhs)) => lhs + rhs,
- (_, _) => R::Error(NumOpError::while_doing(MathOp::Add)),
- }
- }
- }
-
- impl<T> ops::Add<T> for NumOpResult<T>
- where
- (T: Copy + ops::Add<NumOpResult<T>, Output = NumOpResult<T>>)
- {
- type Output = NumOpResult<T>;
-
- fn add(self, rhs: T) -> Self::Output { rhs + self }
- }
-
- impl<T> ops::Sub<NumOpResult<T>> for NumOpResult<T>
- where
- (T: Copy + ops::Sub<Output = NumOpResult<T>>)
- {
- type Output = NumOpResult<T>;
-
- fn sub(self, rhs: Self) -> Self::Output {
- match (self, rhs) {
- (R::Valid(lhs), R::Valid(rhs)) => lhs - rhs,
- (_, _) => R::Error(NumOpError::while_doing(MathOp::Sub)),
- }
- }
- }
-
- impl<T> ops::Sub<T> for NumOpResult<T>
- where
- (T: Copy + ops::Sub<Output = NumOpResult<T>>)
- {
- type Output = NumOpResult<T>;
-
- fn sub(self, rhs: T) -> Self::Output {
- match self {
- R::Valid(amount) => amount - rhs,
- R::Error(_) => self,
- }
- }
- }
}
impl_mul_assign!(NumOpResult<Amount>, u64);
diff --git a/units/src/result.rs b/units/src/result.rs
index b616e5bb..7b730d02 100644
--- a/units/src/result.rs
+++ b/units/src/result.rs
@@ -3,7 +3,7 @@
//! Provides a monodic type returned by mathematical operations (`core::ops`).
use core::convert::Infallible;
-use core::fmt;
+use core::{fmt, ops};
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
@@ -215,6 +215,60 @@ impl<T: fmt::Debug> NumOpResult<T> {
pub fn is_error(&self) -> bool { !self.is_valid() }
}
+// Implement Add/Sub on NumOpResults for all wrapped types that already implement Add/Sub on themselves
+crate::internal_macros::impl_op_for_references! {
+ impl<T> ops::Add<NumOpResult<T>> for NumOpResult<T>
+ where
+ (T: Copy + ops::Add<Output = NumOpResult<T>>)
+ {
+ type Output = NumOpResult<T>;
+
+ fn add(self, rhs: Self) -> Self::Output {
+ match (self, rhs) {
+ (R::Valid(lhs), R::Valid(rhs)) => lhs + rhs,
+ (_, _) => R::Error(NumOpError::while_doing(MathOp::Add)),
+ }
+ }
+ }
+
+ impl<T> ops::Add<T> for NumOpResult<T>
+ where
+ (T: Copy + ops::Add<NumOpResult<T>, Output = NumOpResult<T>>)
+ {
+ type Output = NumOpResult<T>;
+
+ fn add(self, rhs: T) -> Self::Output { rhs + self }
+ }
+
+ impl<T> ops::Sub<NumOpResult<T>> for NumOpResult<T>
+ where
+ (T: Copy + ops::Sub<Output = NumOpResult<T>>)
+ {
+ type Output = NumOpResult<T>;
+
+ fn sub(self, rhs: Self) -> Self::Output {
+ match (self, rhs) {
+ (R::Valid(lhs), R::Valid(rhs)) => lhs - rhs,
+ (_, _) => R::Error(NumOpError::while_doing(MathOp::Sub)),
+ }
+ }
+ }
+
+ impl<T> ops::Sub<T> for NumOpResult<T>
+ where
+ (T: Copy + ops::Sub<Output = NumOpResult<T>>)
+ {
+ type Output = NumOpResult<T>;
+
+ fn sub(self, rhs: T) -> Self::Output {
+ match self {
+ R::Valid(amount) => amount - rhs,
+ R::Error(_) => self,
+ }
+ }
+ }
+}
+
pub(crate) trait OptionExt<T> {
fn valid_or_error(self, op: MathOp) -> NumOpResult<T>;
}
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.