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