Make Sum generic for Signed/Amount -> NumOpResult
What changed, and why it matters
This is a small Rust API ergonomics change. It makes the 'sum' operation more flexible so developers can add up plain Amount values directly into a result type, not just values that are already wrapped in that result type. There is no security-relevant change.
No security action required. Treat as a normal API/usability improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit generalizes the core::iter::Sum implementation for NumOpResult
Changed components
units/src/amount/result.rsInspect captured patch +6 / −6
diff --git a/units/src/amount/result.rs b/units/src/amount/result.rs
index c70ded04..7f8c5631 100644
--- a/units/src/amount/result.rs
+++ b/units/src/amount/result.rs
@@ -214,12 +214,12 @@ impl ops::Neg for SignedAmount {
}
}
-impl core::iter::Sum<Self> for NumOpResult<Amount> {
+impl<T: Into<Self>> core::iter::Sum<T> for NumOpResult<Amount> {
fn sum<I>(iter: I) -> Self
where
- I: Iterator<Item = Self>,
+ I: Iterator<Item = T>,
{
- iter.fold(Self::Valid(Amount::ZERO), |acc, amount| match (acc, amount) {
+ iter.fold(Self::Valid(Amount::ZERO), |acc, amount| match (acc, amount.into()) {
(Self::Valid(lhs), Self::Valid(rhs)) => lhs + rhs,
(_, _) => Self::Error(NumOpError::while_doing(MathOp::Add)),
})
@@ -237,12 +237,12 @@ impl<'a> core::iter::Sum<&'a Self> for NumOpResult<Amount> {
}
}
-impl core::iter::Sum<Self> for NumOpResult<SignedAmount> {
+impl<T: Into<Self>> core::iter::Sum<T> for NumOpResult<SignedAmount> {
fn sum<I>(iter: I) -> Self
where
- I: Iterator<Item = Self>,
+ I: Iterator<Item = T>,
{
- iter.fold(Self::Valid(SignedAmount::ZERO), |acc, amount| match (acc, amount) {
+ iter.fold(Self::Valid(SignedAmount::ZERO), |acc, amount| match (acc, amount.into()) {
(Self::Valid(lhs), Self::Valid(rhs)) => lhs + rhs,
(_, _) => Self::Error(NumOpError::while_doing(MathOp::Add)),
})
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.