use core::ops;
pub(crate) struct Indexer<const N: usize> {
offset: usize,
}
impl<const N: usize> Indexer<N> {
pub(crate) fn new() -> Self {
Self { offset: 0 }
}
pub(crate) fn iter(&mut self) -> IndexIter<N> {
let offset = self.offset;
if N > 0 {
self.offset = (self.offset + 1).wrapping_rem(N);
}
IndexIter {
iter: (0..N),
offset,
}
}
}
pub(crate) struct IndexIter<const N: usize> {
iter: ops::Range<usize>,
offset: usize,
}
impl<const N: usize> Iterator for IndexIter<N> {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|pos| (pos + self.offset).wrapping_rem(N))
}
}