diff --git a/units/src/amount/signed.rs b/units/src/amount/signed.rs index b90fdd5e71..841b270714 100644 --- a/units/src/amount/signed.rs +++ b/units/src/amount/signed.rs @@ -442,6 +442,16 @@ impl core::iter::Sum for SignedAmount { } } +impl<'a> core::iter::Sum<&'a SignedAmount> for SignedAmount { + fn sum(iter: I) -> Self + where + I: Iterator, + { + let sats: i64 = iter.map(|amt| amt.0).sum(); + SignedAmount::from_sat(sats) + } +} + #[cfg(feature = "arbitrary")] impl<'a> Arbitrary<'a> for SignedAmount { fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result { diff --git a/units/src/amount/tests.rs b/units/src/amount/tests.rs index e695a443bd..00d54d7268 100644 --- a/units/src/amount/tests.rs +++ b/units/src/amount/tests.rs @@ -825,8 +825,8 @@ fn serde_as_sat_opt() { #[test] fn sum_amounts() { - assert_eq!(Amount::from_sat(0), [].into_iter().sum::()); - assert_eq!(SignedAmount::from_sat(0), [].into_iter().sum::()); + assert_eq!(Amount::from_sat(0), [].iter().sum::()); + assert_eq!(SignedAmount::from_sat(0), [].iter().sum::()); let amounts = [Amount::from_sat(42), Amount::from_sat(1337), Amount::from_sat(21)]; let sum = amounts.into_iter().sum::(); diff --git a/units/src/amount/unsigned.rs b/units/src/amount/unsigned.rs index a682623853..143c94e57f 100644 --- a/units/src/amount/unsigned.rs +++ b/units/src/amount/unsigned.rs @@ -432,6 +432,16 @@ impl core::iter::Sum for Amount { } } +impl<'a> core::iter::Sum<&'a Amount> for Amount { + fn sum(iter: I) -> Self + where + I: Iterator, + { + let sats: u64 = iter.map(|amt| amt.0).sum(); + Amount::from_sat(sats) + } +} + #[cfg(feature = "arbitrary")] impl<'a> Arbitrary<'a> for Amount { fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result {