use crate::iter::FusedIterator;
use crate::num::NonZero;
use crate::ops::Try;
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Cycle<I> {
orig: I,
iter: I,
}
impl<I: Clone> Cycle<I> {
pub(in crate::iter) fn new(iter: I) -> Cycle<I> {
Cycle { orig: iter.clone(), iter }
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<I> Iterator for Cycle<I>
where
I: Clone + Iterator,
{
type Item = <I as Iterator>::Item;
#[inline]
fn next(&mut self) -> Option<<I as Iterator>::Item> {
match self.iter.next() {
None => {
self.iter = self.orig.clone();
self.iter.next()
}
y => y,
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.orig.size_hint() {
sz @ (0, Some(0)) => sz,
(0, _) => (0, None),
_ => (usize::MAX, None),
}
}
#[inline]
fn try_fold<Acc, F, R>(&mut self, mut acc: Acc, mut f: F) -> R
where
F: FnMut(Acc, Self::Item) -> R,
R: Try<Output = Acc>,
{
acc = self.iter.try_fold(acc, &mut f)?;
self.iter = self.orig.clone();
let mut is_empty = true;
acc = self.iter.try_fold(acc, |acc, x| {
is_empty = false;
f(acc, x)
})?;
if is_empty {
return try { acc };
}
loop {
self.iter = self.orig.clone();
acc = self.iter.try_fold(acc, &mut f)?;
}
}
#[inline]
#[rustc_inherit_overflow_checks]
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
let mut n = match self.iter.advance_by(n) {
Ok(()) => return Ok(()),
Err(rem) => rem.get(),
};
while n > 0 {
self.iter = self.orig.clone();
n = match self.iter.advance_by(n) {
Ok(()) => return Ok(()),
e @ Err(rem) if rem.get() == n => return e,
Err(rem) => rem.get(),
};
}
NonZero::new(n).map_or(Ok(()), Err)
}
}
#[stable(feature = "fused", since = "1.26.0")]
impl<I> FusedIterator for Cycle<I> where I: Clone + Iterator {}