Implement iter::Sum for BlockInterval

We support adding two intervals; no obvious reason not to support
summing an iterator of intervals.
This commit is contained in:
Tobin C. Harding 2024-11-28 15:56:07 +11:00
parent 0369e64b56
commit 433f70939c
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 17 additions and 0 deletions

View File

@ -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::*;