From 433f70939c3ecc10702ab6502e3f9bcd94dab739 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 28 Nov 2024 15:56:07 +1100 Subject: [PATCH] Implement iter::Sum for BlockInterval We support adding two intervals; no obvious reason not to support summing an iterator of intervals. --- units/src/block.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/units/src/block.rs b/units/src/block.rs index 7b407e33cf..aabb4c2ccb 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -231,6 +231,23 @@ impl ops::SubAssign for BlockInterval { fn sub_assign(&mut self, rhs: BlockInterval) { self.0 = self.to_u32() - rhs.to_u32(); } } +impl core::iter::Sum for BlockInterval { + fn sum>(iter: I) -> Self { + let sum = iter.map(|interval| interval.0).sum(); + BlockInterval::from_u32(sum) + } +} + +impl<'a> core::iter::Sum<&'a BlockInterval> for BlockInterval { + fn sum(iter: I) -> Self + where + I: Iterator, + { + let sum = iter.map(|interval| interval.0).sum(); + BlockInterval::from_u32(sum) + } +} + #[cfg(test)] mod tests { use super::*;