Skip to main content

bounded_counter/
lib.rs

1use num_traits::ops::wrapping::WrappingAdd;
2use num_traits::{bounds::UpperBounded, One};
3use std::ops::{AddAssign, RemAssign};
4
5/// # Bounded Counter
6/// This is a generic type that provides an incremental counter with a type specified upper bound.
7/// It increments every time it is iterated until it reaches the upper bound, at which point it resets back to 0.
8#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
9#[cfg_attr(feature = "constructor", derive(derive_more::Constructor))]
10#[cfg_attr(feature = "deref_mut", derive(derive_more::DerefMut))]
11#[cfg_attr(feature = "deref", derive(derive_more::Deref))]
12pub struct BoundedCounter<I>(pub I);
13
14impl<I> Iterator for BoundedCounter<I>
15where
16    I: Copy + RemAssign<I> + UpperBounded + AddAssign<I> + One,
17{
18    type Item = I;
19
20    fn next(&mut self) -> Option<Self::Item> {
21        let i = self.0;
22        self.0 %= I::max_value();
23        self.0 += I::one();
24        Some(i)
25    }
26}
27
28/// # Wrapped Counter
29/// This counter increments on every iteration and wraps on overflow.
30/// For unsigned types this wraps to 0 after the max value; for signed types it wraps to the min value.
31#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
32#[cfg_attr(feature = "constructor", derive(derive_more::Constructor))]
33#[cfg_attr(feature = "deref_mut", derive(derive_more::DerefMut))]
34#[cfg_attr(feature = "deref", derive(derive_more::Deref))]
35pub struct WrappedCounter<I>(pub I);
36
37impl<I> Iterator for WrappedCounter<I>
38where
39    I: Copy + One + WrappingAdd,
40{
41    type Item = I;
42
43    fn next(&mut self) -> Option<Self::Item> {
44        let i = self.0;
45        self.0 = self.0.wrapping_add(&I::one());
46        Some(i)
47    }
48}