use crate::intrinsics;
use crate::iter::adapters::zip::try_get_unchecked;
use crate::iter::adapters::SourceIter;
use crate::iter::{
FusedIterator, TrustedFused, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce,
};
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 Fuse<I> {
iter: Option<I>,
}
impl<I> Fuse<I> {
pub(in crate::iter) fn new(iter: I) -> Fuse<I> {
Fuse { iter: Some(iter) }
}
pub(crate) fn into_inner(self) -> Option<I> {
self.iter
}
}
#[stable(feature = "fused", since = "1.26.0")]
impl<I> FusedIterator for Fuse<I> where I: Iterator {}
#[unstable(issue = "none", feature = "trusted_fused")]
unsafe impl<I> TrustedFused for Fuse<I> where I: TrustedFused {}
#[stable(feature = "rust1", since = "1.0.0")]
impl<I> Iterator for Fuse<I>
where
I: Iterator,
{
type Item = <I as Iterator>::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
FuseImpl::next(self)
}
#[inline]
fn nth(&mut self, n: usize) -> Option<I::Item> {
FuseImpl::nth(self, n)
}
#[inline]
fn last(self) -> Option<Self::Item> {
match self.iter {
Some(iter) => iter.last(),
None => None,
}
}
#[inline]
fn count(self) -> usize {
match self.iter {
Some(iter) => iter.count(),
None => 0,
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.iter {
Some(ref iter) => iter.size_hint(),
None => (0, Some(0)),
}
}
#[inline]
fn try_fold<Acc, Fold, R>(&mut self, acc: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Output = Acc>,
{
FuseImpl::try_fold(self, acc, fold)
}
#[inline]
fn fold<Acc, Fold>(self, mut acc: Acc, fold: Fold) -> Acc
where
Fold: FnMut(Acc, Self::Item) -> Acc,
{
if let Some(iter) = self.iter {
acc = iter.fold(acc, fold);
}
acc
}
#[inline]
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
{
FuseImpl::find(self, predicate)
}
#[inline]
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
where
Self: TrustedRandomAccessNoCoerce,
{
match self.iter {
Some(ref mut iter) => unsafe { try_get_unchecked(iter, idx) },
None => unsafe { intrinsics::unreachable() },
}
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<I> DoubleEndedIterator for Fuse<I>
where
I: DoubleEndedIterator,
{
#[inline]
fn next_back(&mut self) -> Option<<I as Iterator>::Item> {
FuseImpl::next_back(self)
}
#[inline]
fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item> {
FuseImpl::nth_back(self, n)
}
#[inline]
fn try_rfold<Acc, Fold, R>(&mut self, acc: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Output = Acc>,
{
FuseImpl::try_rfold(self, acc, fold)
}
#[inline]
fn rfold<Acc, Fold>(self, mut acc: Acc, fold: Fold) -> Acc
where
Fold: FnMut(Acc, Self::Item) -> Acc,
{
if let Some(iter) = self.iter {
acc = iter.rfold(acc, fold);
}
acc
}
#[inline]
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
{
FuseImpl::rfind(self, predicate)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<I> ExactSizeIterator for Fuse<I>
where
I: ExactSizeIterator,
{
fn len(&self) -> usize {
match self.iter {
Some(ref iter) => iter.len(),
None => 0,
}
}
fn is_empty(&self) -> bool {
match self.iter {
Some(ref iter) => iter.is_empty(),
None => true,
}
}
}
#[stable(feature = "default_iters", since = "1.70.0")]
impl<I: Default> Default for Fuse<I> {
fn default() -> Self {
Fuse { iter: Default::default() }
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<I> TrustedLen for Fuse<I> where I: TrustedLen {}
#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe impl<I> TrustedRandomAccess for Fuse<I> where I: TrustedRandomAccess {}
#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe impl<I> TrustedRandomAccessNoCoerce for Fuse<I>
where
I: TrustedRandomAccessNoCoerce,
{
const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
}
#[doc(hidden)]
trait FuseImpl<I> {
type Item;
fn next(&mut self) -> Option<Self::Item>;
fn nth(&mut self, n: usize) -> Option<Self::Item>;
fn try_fold<Acc, Fold, R>(&mut self, acc: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Output = Acc>;
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool;
fn next_back(&mut self) -> Option<Self::Item>
where
I: DoubleEndedIterator;
fn nth_back(&mut self, n: usize) -> Option<Self::Item>
where
I: DoubleEndedIterator;
fn try_rfold<Acc, Fold, R>(&mut self, acc: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Output = Acc>,
I: DoubleEndedIterator;
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
I: DoubleEndedIterator;
}
#[doc(hidden)]
impl<I> FuseImpl<I> for Fuse<I>
where
I: Iterator,
{
type Item = <I as Iterator>::Item;
#[inline]
default fn next(&mut self) -> Option<<I as Iterator>::Item> {
and_then_or_clear(&mut self.iter, Iterator::next)
}
#[inline]
default fn nth(&mut self, n: usize) -> Option<I::Item> {
and_then_or_clear(&mut self.iter, |iter| iter.nth(n))
}
#[inline]
default fn try_fold<Acc, Fold, R>(&mut self, mut acc: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Output = Acc>,
{
if let Some(ref mut iter) = self.iter {
acc = iter.try_fold(acc, fold)?;
self.iter = None;
}
try { acc }
}
#[inline]
default fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
{
and_then_or_clear(&mut self.iter, |iter| iter.find(predicate))
}
#[inline]
default fn next_back(&mut self) -> Option<<I as Iterator>::Item>
where
I: DoubleEndedIterator,
{
and_then_or_clear(&mut self.iter, |iter| iter.next_back())
}
#[inline]
default fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item>
where
I: DoubleEndedIterator,
{
and_then_or_clear(&mut self.iter, |iter| iter.nth_back(n))
}
#[inline]
default fn try_rfold<Acc, Fold, R>(&mut self, mut acc: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Output = Acc>,
I: DoubleEndedIterator,
{
if let Some(ref mut iter) = self.iter {
acc = iter.try_rfold(acc, fold)?;
self.iter = None;
}
try { acc }
}
#[inline]
default fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
I: DoubleEndedIterator,
{
and_then_or_clear(&mut self.iter, |iter| iter.rfind(predicate))
}
}
#[doc(hidden)]
impl<I> FuseImpl<I> for Fuse<I>
where
I: FusedIterator,
{
#[inline]
fn next(&mut self) -> Option<<I as Iterator>::Item> {
self.iter.as_mut()?.next()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<I::Item> {
self.iter.as_mut()?.nth(n)
}
#[inline]
fn try_fold<Acc, Fold, R>(&mut self, mut acc: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Output = Acc>,
{
if let Some(ref mut iter) = self.iter {
acc = iter.try_fold(acc, fold)?;
}
try { acc }
}
#[inline]
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
{
self.iter.as_mut()?.find(predicate)
}
#[inline]
fn next_back(&mut self) -> Option<<I as Iterator>::Item>
where
I: DoubleEndedIterator,
{
self.iter.as_mut()?.next_back()
}
#[inline]
fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item>
where
I: DoubleEndedIterator,
{
self.iter.as_mut()?.nth_back(n)
}
#[inline]
fn try_rfold<Acc, Fold, R>(&mut self, mut acc: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Output = Acc>,
I: DoubleEndedIterator,
{
if let Some(ref mut iter) = self.iter {
acc = iter.try_rfold(acc, fold)?;
}
try { acc }
}
#[inline]
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
I: DoubleEndedIterator,
{
self.iter.as_mut()?.rfind(predicate)
}
}
#[unstable(issue = "none", feature = "inplace_iteration")]
unsafe impl<I> SourceIter for Fuse<I>
where
I: SourceIter + TrustedFused,
{
type Source = I::Source;
#[inline]
unsafe fn as_inner(&mut self) -> &mut I::Source {
unsafe { SourceIter::as_inner(self.iter.as_mut().unwrap_unchecked()) }
}
}
#[inline]
fn and_then_or_clear<T, U>(opt: &mut Option<T>, f: impl FnOnce(&mut T) -> Option<U>) -> Option<U> {
let x = f(opt.as_mut()?);
if x.is_none() {
*opt = None;
}
x
}