1 use crate::iter::adapters::{
2 zip::try_get_unchecked, TrustedRandomAccess, TrustedRandomAccessNoCoerce,
4 use crate::iter::{FusedIterator, TrustedLen};
7 /// An iterator that copies the elements of an underlying iterator.
9 /// This `struct` is created by the [`copied`] method on [`Iterator`]. See its
10 /// documentation for more.
12 /// [`copied`]: Iterator::copied
13 /// [`Iterator`]: trait.Iterator.html
14 #[stable(feature = "iter_copied", since = "1.36.0")]
15 #[must_use = "iterators are lazy and do nothing unless consumed"]
16 #[derive(Clone, Debug)]
17 pub struct Copied<I> {
22 pub(in crate::iter) fn new(it: I) -> Copied<I> {
27 fn copy_fold<T: Copy, Acc>(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, &T) -> Acc {
28 move |acc, &elt| f(acc, elt)
31 fn copy_try_fold<T: Copy, Acc, R>(mut f: impl FnMut(Acc, T) -> R) -> impl FnMut(Acc, &T) -> R {
32 move |acc, &elt| f(acc, elt)
35 #[stable(feature = "iter_copied", since = "1.36.0")]
36 impl<'a, I, T: 'a> Iterator for Copied<I>
38 I: Iterator<Item = &'a T>,
43 fn next(&mut self) -> Option<T> {
44 self.it.next().copied()
47 fn size_hint(&self) -> (usize, Option<usize>) {
51 fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
54 F: FnMut(B, Self::Item) -> R,
57 self.it.try_fold(init, copy_try_fold(f))
60 fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
62 F: FnMut(Acc, Self::Item) -> Acc,
64 self.it.fold(init, copy_fold(f))
67 fn nth(&mut self, n: usize) -> Option<T> {
68 self.it.nth(n).copied()
71 fn last(self) -> Option<T> {
72 self.it.last().copied()
75 fn count(self) -> usize {
80 fn advance_by(&mut self, n: usize) -> Result<(), usize> {
84 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T
86 Self: TrustedRandomAccessNoCoerce,
88 // SAFETY: the caller must uphold the contract for
89 // `Iterator::__iterator_get_unchecked`.
90 *unsafe { try_get_unchecked(&mut self.it, idx) }
94 #[stable(feature = "iter_copied", since = "1.36.0")]
95 impl<'a, I, T: 'a> DoubleEndedIterator for Copied<I>
97 I: DoubleEndedIterator<Item = &'a T>,
100 fn next_back(&mut self) -> Option<T> {
101 self.it.next_back().copied()
104 fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
107 F: FnMut(B, Self::Item) -> R,
110 self.it.try_rfold(init, copy_try_fold(f))
113 fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
115 F: FnMut(Acc, Self::Item) -> Acc,
117 self.it.rfold(init, copy_fold(f))
121 fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
122 self.it.advance_back_by(n)
126 #[stable(feature = "iter_copied", since = "1.36.0")]
127 impl<'a, I, T: 'a> ExactSizeIterator for Copied<I>
129 I: ExactSizeIterator<Item = &'a T>,
132 fn len(&self) -> usize {
136 fn is_empty(&self) -> bool {
141 #[stable(feature = "iter_copied", since = "1.36.0")]
142 impl<'a, I, T: 'a> FusedIterator for Copied<I>
144 I: FusedIterator<Item = &'a T>,
150 #[unstable(feature = "trusted_random_access", issue = "none")]
151 unsafe impl<I> TrustedRandomAccess for Copied<I> where I: TrustedRandomAccess {}
154 #[unstable(feature = "trusted_random_access", issue = "none")]
155 unsafe impl<I> TrustedRandomAccessNoCoerce for Copied<I>
157 I: TrustedRandomAccessNoCoerce,
159 const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
162 #[stable(feature = "iter_copied", since = "1.36.0")]
163 unsafe impl<'a, I, T: 'a> TrustedLen for Copied<I>
165 I: TrustedLen<Item = &'a T>,