gat_lending_iterator/adapters/
cycle.rs

1use crate::LendingIterator;
2use core::fmt;
3
4/// A lending iterator that repeats endlessly.
5///
6/// This `struct` is created by the [`cycle`] method on [`LendingIterator`]. See
7/// its documentation for more.
8///
9/// [`LendingIterator`]: crate::LendingIterator
10/// [`cycle`]: crate::LendingIterator::cycle
11#[derive(Clone)]
12#[must_use = "iterators are lazy and do nothing unless consumed"]
13pub struct Cycle<I> {
14    orig: I,
15    iter: I,
16    first_iteration: bool,
17}
18
19impl<I: Clone> Cycle<I> {
20    pub(crate) fn new(iter: I) -> Self {
21        Self {
22            orig: iter.clone(),
23            iter,
24            first_iteration: true,
25        }
26    }
27}
28
29impl<I: fmt::Debug> fmt::Debug for Cycle<I> {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        f.debug_struct("Cycle")
32            .field("iter", &self.iter)
33            .finish_non_exhaustive()
34    }
35}
36
37impl<I> LendingIterator for Cycle<I>
38where
39    I: Clone + LendingIterator,
40{
41    type Item<'a>
42        = I::Item<'a>
43    where
44        Self: 'a;
45
46    #[inline]
47    fn next(&mut self) -> Option<Self::Item<'_>> {
48        loop {
49            // SAFETY: see https://docs.rs/polonius-the-crab/0.3.1/polonius_the_crab/#the-arcanemagic
50            let self_ = unsafe { &mut *(self as *mut Self) };
51            match self_.iter.next() {
52                None => {
53                    if self.first_iteration {
54                        return None;
55                    }
56                    self.iter = self.orig.clone();
57                }
58                Some(item) => {
59                    self.first_iteration = false;
60                    return Some(item);
61                }
62            }
63        }
64    }
65
66    #[inline]
67    fn size_hint(&self) -> (usize, Option<usize>) {
68        match self.orig.size_hint() {
69            (0, Some(0)) => (0, Some(0)),
70            (0, _) => (0, None),
71            _ => (usize::MAX, None),
72        }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use crate::{LendingIterator, ToLendingIterator};
79
80    fn identity(x: i32) -> i32 {
81        x
82    }
83
84    #[test]
85    fn cycle_basic() {
86        let result: Vec<_> = (0..3)
87            .into_lending()
88            .cycle()
89            .take(10)
90            .map(identity)
91            .into_iter()
92            .collect();
93        assert_eq!(result, vec![0, 1, 2, 0, 1, 2, 0, 1, 2, 0]);
94    }
95
96    #[test]
97    fn cycle_empty() {
98        let result: Vec<_> = core::iter::empty::<i32>()
99            .into_lending()
100            .cycle()
101            .take(5)
102            .map(identity)
103            .into_iter()
104            .collect();
105        assert_eq!(result, Vec::<i32>::new());
106    }
107}