idsp/accu.rs
1use num_traits::ops::wrapping::WrappingAdd;
2
3/// Wrapping Accumulator
4#[derive(Copy, Clone, Default, PartialEq, PartialOrd, Debug)]
5pub struct Accu<T> {
6 state: T,
7 step: T,
8}
9
10impl<T> Accu<T> {
11 /// Create a new accumulator with given initial state and step.
12 pub fn new(state: T, step: T) -> Self {
13 Self { state, step }
14 }
15}
16
17impl<T> Iterator for Accu<T>
18where
19 T: WrappingAdd + Copy,
20{
21 type Item = T;
22 fn next(&mut self) -> Option<T> {
23 let s = self.state;
24 self.state = s.wrapping_add(&self.step);
25 Some(s)
26 }
27}