1 use crate::iter::adapters::{
2     zip::try_get_unchecked, TrustedRandomAccess, TrustedRandomAccessNoCoerce,
3 };
4 use crate::iter::{FusedIterator, TrustedLen};
5 use crate::ops::Try;
6 
7 /// An iterator that copies the elements of an underlying iterator.
8 ///
9 /// This `struct` is created by the [`copied`] method on [`Iterator`]. See its
10 /// documentation for more.
11 ///
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> {
18     it: I,
19 }
20 
21 impl<I> Copied<I> {
new(it: I) -> Copied<I>22     pub(in crate::iter) fn new(it: I) -> Copied<I> {
23         Copied { it }
24     }
25 }
26 
copy_fold<T: Copy, Acc>(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, &T) -> Acc27 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)
29 }
30 
copy_try_fold<T: Copy, Acc, R>(mut f: impl FnMut(Acc, T) -> R) -> impl FnMut(Acc, &T) -> R31 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)
33 }
34 
35 #[stable(feature = "iter_copied", since = "1.36.0")]
36 impl<'a, I, T: 'a> Iterator for Copied<I>
37 where
38     I: Iterator<Item = &'a T>,
39     T: Copy,
40 {
41     type Item = T;
42 
next(&mut self) -> Option<T>43     fn next(&mut self) -> Option<T> {
44         self.it.next().copied()
45     }
46 
size_hint(&self) -> (usize, Option<usize>)47     fn size_hint(&self) -> (usize, Option<usize>) {
48         self.it.size_hint()
49     }
50 
try_fold<B, F, R>(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B>,51     fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
52     where
53         Self: Sized,
54         F: FnMut(B, Self::Item) -> R,
55         R: Try<Output = B>,
56     {
57         self.it.try_fold(init, copy_try_fold(f))
58     }
59 
fold<Acc, F>(self, init: Acc, f: F) -> Acc where F: FnMut(Acc, Self::Item) -> Acc,60     fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
61     where
62         F: FnMut(Acc, Self::Item) -> Acc,
63     {
64         self.it.fold(init, copy_fold(f))
65     }
66 
nth(&mut self, n: usize) -> Option<T>67     fn nth(&mut self, n: usize) -> Option<T> {
68         self.it.nth(n).copied()
69     }
70 
last(self) -> Option<T>71     fn last(self) -> Option<T> {
72         self.it.last().copied()
73     }
74 
count(self) -> usize75     fn count(self) -> usize {
76         self.it.count()
77     }
78 
79     #[inline]
advance_by(&mut self, n: usize) -> Result<(), usize>80     fn advance_by(&mut self, n: usize) -> Result<(), usize> {
81         self.it.advance_by(n)
82     }
83 
84     #[doc(hidden)]
__iterator_get_unchecked(&mut self, idx: usize) -> T where Self: TrustedRandomAccessNoCoerce,85     unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T
86     where
87         Self: TrustedRandomAccessNoCoerce,
88     {
89         // SAFETY: the caller must uphold the contract for
90         // `Iterator::__iterator_get_unchecked`.
91         *unsafe { try_get_unchecked(&mut self.it, idx) }
92     }
93 }
94 
95 #[stable(feature = "iter_copied", since = "1.36.0")]
96 impl<'a, I, T: 'a> DoubleEndedIterator for Copied<I>
97 where
98     I: DoubleEndedIterator<Item = &'a T>,
99     T: Copy,
100 {
next_back(&mut self) -> Option<T>101     fn next_back(&mut self) -> Option<T> {
102         self.it.next_back().copied()
103     }
104 
try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B>,105     fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
106     where
107         Self: Sized,
108         F: FnMut(B, Self::Item) -> R,
109         R: Try<Output = B>,
110     {
111         self.it.try_rfold(init, copy_try_fold(f))
112     }
113 
rfold<Acc, F>(self, init: Acc, f: F) -> Acc where F: FnMut(Acc, Self::Item) -> Acc,114     fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
115     where
116         F: FnMut(Acc, Self::Item) -> Acc,
117     {
118         self.it.rfold(init, copy_fold(f))
119     }
120 
121     #[inline]
advance_back_by(&mut self, n: usize) -> Result<(), usize>122     fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
123         self.it.advance_back_by(n)
124     }
125 }
126 
127 #[stable(feature = "iter_copied", since = "1.36.0")]
128 impl<'a, I, T: 'a> ExactSizeIterator for Copied<I>
129 where
130     I: ExactSizeIterator<Item = &'a T>,
131     T: Copy,
132 {
len(&self) -> usize133     fn len(&self) -> usize {
134         self.it.len()
135     }
136 
is_empty(&self) -> bool137     fn is_empty(&self) -> bool {
138         self.it.is_empty()
139     }
140 }
141 
142 #[stable(feature = "iter_copied", since = "1.36.0")]
143 impl<'a, I, T: 'a> FusedIterator for Copied<I>
144 where
145     I: FusedIterator<Item = &'a T>,
146     T: Copy,
147 {
148 }
149 
150 #[doc(hidden)]
151 #[unstable(feature = "trusted_random_access", issue = "none")]
152 unsafe impl<I> TrustedRandomAccess for Copied<I> where I: TrustedRandomAccess {}
153 
154 #[doc(hidden)]
155 #[unstable(feature = "trusted_random_access", issue = "none")]
156 unsafe impl<I> TrustedRandomAccessNoCoerce for Copied<I>
157 where
158     I: TrustedRandomAccessNoCoerce,
159 {
160     const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
161 }
162 
163 #[stable(feature = "iter_copied", since = "1.36.0")]
164 unsafe impl<'a, I, T: 'a> TrustedLen for Copied<I>
165 where
166     I: TrustedLen<Item = &'a T>,
167     T: Copy,
168 {
169 }
170