non_empty_iter/
cycle.rs

1//! Repeating non-empty iterators endlessly.
2
3use core::iter;
4
5use crate::non_empty::NonEmptyIterator;
6
7/// Represents non-empty iterators that repeat endlessly.
8///
9/// This `struct` is created by the [`cycle`] method on [`NonEmptyIterator`].
10/// See its documentation for more.
11///
12/// [`cycle`]: NonEmptyIterator::cycle
13pub struct Cycle<I: NonEmptyIterator>
14where
15    I::IntoIter: Clone,
16{
17    non_empty: I,
18}
19
20impl<I: NonEmptyIterator> Cycle<I>
21where
22    I::IntoIter: Clone,
23{
24    /// Constructs [`Self`].
25    pub const fn new(non_empty: I) -> Self {
26        Self { non_empty }
27    }
28}
29
30impl<I: NonEmptyIterator> IntoIterator for Cycle<I>
31where
32    I::IntoIter: Clone,
33{
34    type Item = I::Item;
35
36    type IntoIter = iter::Cycle<I::IntoIter>;
37
38    fn into_iter(self) -> Self::IntoIter {
39        self.non_empty.into_iter().cycle()
40    }
41}
42
43unsafe impl<I: NonEmptyIterator> NonEmptyIterator for Cycle<I> where I::IntoIter: Clone {}