use crate::iter::adapters::{ zip::try_get_unchecked, TrustedRandomAccess, TrustedRandomAccessNoCoerce, }; use crate::iter::{FusedIterator, TrustedLen}; use crate::ops::Try; /// An iterator that copies the elements of an underlying iterator. /// /// This `struct` is created by the [`copied`] method on [`Iterator`]. See its /// documentation for more. /// /// [`copied`]: Iterator::copied /// [`Iterator`]: trait.Iterator.html #[stable(feature = "iter_copied", since = "1.36.0")] #[must_use = "iterators are lazy and do nothing unless consumed"] #[derive(Clone, Debug)] pub struct Copied { it: I, } impl Copied { pub(in crate::iter) fn new(it: I) -> Copied { Copied { it } } } fn copy_fold(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, &T) -> Acc { move |acc, &elt| f(acc, elt) } fn copy_try_fold(mut f: impl FnMut(Acc, T) -> R) -> impl FnMut(Acc, &T) -> R { move |acc, &elt| f(acc, elt) } #[stable(feature = "iter_copied", since = "1.36.0")] impl<'a, I, T: 'a> Iterator for Copied where I: Iterator, T: Copy, { type Item = T; fn next(&mut self) -> Option { self.it.next().copied() } fn size_hint(&self) -> (usize, Option) { self.it.size_hint() } fn try_fold(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try, { self.it.try_fold(init, copy_try_fold(f)) } fn fold(self, init: Acc, f: F) -> Acc where F: FnMut(Acc, Self::Item) -> Acc, { self.it.fold(init, copy_fold(f)) } fn nth(&mut self, n: usize) -> Option { self.it.nth(n).copied() } fn last(self) -> Option { self.it.last().copied() } fn count(self) -> usize { self.it.count() } #[inline] fn advance_by(&mut self, n: usize) -> Result<(), usize> { self.it.advance_by(n) } unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T where Self: TrustedRandomAccessNoCoerce, { // SAFETY: the caller must uphold the contract for // `Iterator::__iterator_get_unchecked`. *unsafe { try_get_unchecked(&mut self.it, idx) } } } #[stable(feature = "iter_copied", since = "1.36.0")] impl<'a, I, T: 'a> DoubleEndedIterator for Copied where I: DoubleEndedIterator, T: Copy, { fn next_back(&mut self) -> Option { self.it.next_back().copied() } fn try_rfold(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try, { self.it.try_rfold(init, copy_try_fold(f)) } fn rfold(self, init: Acc, f: F) -> Acc where F: FnMut(Acc, Self::Item) -> Acc, { self.it.rfold(init, copy_fold(f)) } #[inline] fn advance_back_by(&mut self, n: usize) -> Result<(), usize> { self.it.advance_back_by(n) } } #[stable(feature = "iter_copied", since = "1.36.0")] impl<'a, I, T: 'a> ExactSizeIterator for Copied where I: ExactSizeIterator, T: Copy, { fn len(&self) -> usize { self.it.len() } fn is_empty(&self) -> bool { self.it.is_empty() } } #[stable(feature = "iter_copied", since = "1.36.0")] impl<'a, I, T: 'a> FusedIterator for Copied where I: FusedIterator, T: Copy, { } #[doc(hidden)] #[unstable(feature = "trusted_random_access", issue = "none")] unsafe impl TrustedRandomAccess for Copied where I: TrustedRandomAccess {} #[doc(hidden)] #[unstable(feature = "trusted_random_access", issue = "none")] unsafe impl TrustedRandomAccessNoCoerce for Copied where I: TrustedRandomAccessNoCoerce, { const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT; } #[stable(feature = "iter_copied", since = "1.36.0")] unsafe impl<'a, I, T: 'a> TrustedLen for Copied where I: TrustedLen, T: Copy, { }