1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use num_traits::ops::wrapping::WrappingAdd;

/// Wrapping Accumulator
#[derive(Copy, Clone, Default, PartialEq, Eq, Debug)]
pub struct Accu<T> {
    state: T,
    step: T,
}

impl<T> Accu<T> {
    /// Create a new accumulator with given initial state and step.
    pub fn new(state: T, step: T) -> Self {
        Self { state, step }
    }
}

impl<T> Iterator for Accu<T>
where
    T: WrappingAdd + Copy,
{
    type Item = T;
    fn next(&mut self) -> Option<T> {
        let s = self.state;
        self.state = s.wrapping_add(&self.step);
        Some(s)
    }
}