3 adapters::zip::try_get_unchecked, adapters::TrustedRandomAccess, FusedIterator, TrustedLen,
8 /// An iterator that copies the elements of an underlying iterator.
10 /// This `struct` is created by the [`copied`] method on [`Iterator`]. See its
11 /// documentation for more.
13 /// [`copied`]: Iterator::copied
14 /// [`Iterator`]: trait.Iterator.html
15 #[stable(feature = "iter_copied", since = "1.36.0")]
16 #[must_use = "iterators are lazy and do nothing unless consumed"]
17 #[derive(Clone, Debug)]
18 pub struct Copied<I> {
23 pub(in crate::iter) fn new(it: I) -> Copied<I> {
28 fn copy_fold<T: Copy, Acc>(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, &T) -> Acc {
29 move |acc, &elt| f(acc, elt)
32 fn copy_try_fold<T: Copy, Acc, R>(mut f: impl FnMut(Acc, T) -> R) -> impl FnMut(Acc, &T) -> R {
33 move |acc, &elt| f(acc, elt)
36 #[stable(feature = "iter_copied", since = "1.36.0")]
37 impl<'a, I, T: 'a> Iterator for Copied<I>
39 I: Iterator<Item = &'a T>,
44 fn next(&mut self) -> Option<T> {
45 self.it.next().copied()
48 fn size_hint(&self) -> (usize, Option<usize>) {
52 fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
55 F: FnMut(B, Self::Item) -> R,
58 self.it.try_fold(init, copy_try_fold(f))
61 fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
63 F: FnMut(Acc, Self::Item) -> Acc,
65 self.it.fold(init, copy_fold(f))
68 fn nth(&mut self, n: usize) -> Option<T> {
69 self.it.nth(n).copied()
72 fn last(self) -> Option<T> {
73 self.it.last().copied()
76 fn count(self) -> usize {
80 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T
82 Self: TrustedRandomAccess,
84 // SAFETY: the caller must uphold the contract for
85 // `Iterator::__iterator_get_unchecked`.
86 *unsafe { try_get_unchecked(&mut self.it, idx) }
90 #[stable(feature = "iter_copied", since = "1.36.0")]
91 impl<'a, I, T: 'a> DoubleEndedIterator for Copied<I>
93 I: DoubleEndedIterator<Item = &'a T>,
96 fn next_back(&mut self) -> Option<T> {
97 self.it.next_back().copied()
100 fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
103 F: FnMut(B, Self::Item) -> R,
106 self.it.try_rfold(init, copy_try_fold(f))
109 fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
111 F: FnMut(Acc, Self::Item) -> Acc,
113 self.it.rfold(init, copy_fold(f))
117 #[stable(feature = "iter_copied", since = "1.36.0")]
118 impl<'a, I, T: 'a> ExactSizeIterator for Copied<I>
120 I: ExactSizeIterator<Item = &'a T>,
123 fn len(&self) -> usize {
127 fn is_empty(&self) -> bool {
132 #[stable(feature = "iter_copied", since = "1.36.0")]
133 impl<'a, I, T: 'a> FusedIterator for Copied<I>
135 I: FusedIterator<Item = &'a T>,
141 #[unstable(feature = "trusted_random_access", issue = "none")]
142 unsafe impl<I> TrustedRandomAccess for Copied<I>
144 I: TrustedRandomAccess,
147 fn may_have_side_effect() -> bool {
148 I::may_have_side_effect()
152 #[stable(feature = "iter_copied", since = "1.36.0")]
153 unsafe impl<'a, I, T: 'a> TrustedLen for Copied<I>
155 I: TrustedLen<Item = &'a T>,