citer/
lib.rs

1pub struct CIter<'a, T> {
2  idx: usize,
3  pub li: &'a [T],
4  ed: usize,
5}
6
7impl<'a, T> CIter<'a, T> {
8  pub fn pos(&self) -> usize {
9    if self.idx == 0 { 0 } else { self.idx - 1 }
10  }
11
12  pub fn new(li: &'a [T], pos: usize) -> Self {
13    CIter {
14      li,
15      idx: pos,
16      ed: 0,
17    }
18  }
19
20  #[cfg(feature = "rand")]
21  pub fn rand(li: &'a [T]) -> Self {
22    use rand::Rng;
23    let mut rng = rand::rng();
24    let n: usize = rng.random_range(0..li.len());
25    Self::new(li, n)
26  }
27}
28
29impl<'a, T> Iterator for CIter<'a, T> {
30  type Item = (usize, &'a T);
31
32  fn next(&mut self) -> Option<Self::Item> {
33    let len = self.li.len();
34    if self.ed < len {
35      let idx = self.idx % len;
36      let r = Some((idx, &self.li[idx]));
37      self.ed += 1;
38      self.idx += 1;
39      r
40    } else {
41      None
42    }
43  }
44}