Skip to content

Commit

Permalink
Implement iter::Sum for BlockInterval
Browse files Browse the repository at this point in the history
We support adding two intervals; no obvious reason not to support
summing an iterator of intervals.
  • Loading branch information
tcharding committed Nov 28, 2024
1 parent 0369e64 commit 433f709
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions units/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,23 @@ impl ops::SubAssign<BlockInterval> 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<I: Iterator<Item = Self>>(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<I>(iter: I) -> Self
where
I: Iterator<Item = &'a BlockInterval>,
{
let sum = iter.map(|interval| interval.0).sum();
BlockInterval::from_u32(sum)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 433f709

Please sign in to comment.