1 use crate::iter::adapters::{
2 zip::try_get_unchecked, TrustedRandomAccess, TrustedRandomAccessNoCoerce,
4 use crate::iter::{FusedIterator, TrustedLen};
5 use crate::mem::MaybeUninit;
6 use crate::mem::SizedTypeProperties;
8 use crate::{array, ptr};
10 /// An iterator that copies the elements of an underlying iterator.
12 /// This `struct` is created by the [`copied`] method on [`Iterator`]. See its
13 /// documentation for more.
15 /// [`copied`]: Iterator::copied
16 /// [`Iterator`]: trait.Iterator.html
17 #[stable(feature = "iter_copied", since = "1.36.0")]
18 #[must_use = "iterators are lazy and do nothing unless consumed"]
19 #[derive(Clone, Debug)]
20 pub struct Copied<I> {
25 pub(in crate::iter) fn new(it: I) -> Copied<I> {
30 fn copy_fold<T: Copy, Acc>(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, &T) -> Acc {
31 move |acc, &elt| f(acc, elt)
34 fn copy_try_fold<T: Copy, Acc, R>(mut f: impl FnMut(Acc, T) -> R) -> impl FnMut(Acc, &T) -> R {
35 move |acc, &elt| f(acc, elt)
38 #[stable(feature = "iter_copied", since = "1.36.0")]
39 impl<'a, I, T: 'a> Iterator for Copied<I>
41 I: Iterator<Item = &'a T>,
46 fn next(&mut self) -> Option<T> {
47 self.it.next().copied()
50 fn next_chunk<const N: usize>(
52 ) -> Result<[Self::Item; N], array::IntoIter<Self::Item, N>>
56 <I as SpecNextChunk<'_, N, T>>::spec_next_chunk(&mut self.it)
59 fn size_hint(&self) -> (usize, Option<usize>) {
63 fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
66 F: FnMut(B, Self::Item) -> R,
69 self.it.try_fold(init, copy_try_fold(f))
72 fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
74 F: FnMut(Acc, Self::Item) -> Acc,
76 self.it.fold(init, copy_fold(f))
79 fn nth(&mut self, n: usize) -> Option<T> {
80 self.it.nth(n).copied()
83 fn last(self) -> Option<T> {
84 self.it.last().copied()
87 fn count(self) -> usize {
92 fn advance_by(&mut self, n: usize) -> Result<(), usize> {
96 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T
98 Self: TrustedRandomAccessNoCoerce,
100 // SAFETY: the caller must uphold the contract for
101 // `Iterator::__iterator_get_unchecked`.
102 *unsafe { try_get_unchecked(&mut self.it, idx) }
106 #[stable(feature = "iter_copied", since = "1.36.0")]
107 impl<'a, I, T: 'a> DoubleEndedIterator for Copied<I>
109 I: DoubleEndedIterator<Item = &'a T>,
112 fn next_back(&mut self) -> Option<T> {
113 self.it.next_back().copied()
116 fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
119 F: FnMut(B, Self::Item) -> R,
122 self.it.try_rfold(init, copy_try_fold(f))
125 fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
127 F: FnMut(Acc, Self::Item) -> Acc,
129 self.it.rfold(init, copy_fold(f))
133 fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
134 self.it.advance_back_by(n)
138 #[stable(feature = "iter_copied", since = "1.36.0")]
139 impl<'a, I, T: 'a> ExactSizeIterator for Copied<I>
141 I: ExactSizeIterator<Item = &'a T>,
144 fn len(&self) -> usize {
148 fn is_empty(&self) -> bool {
153 #[stable(feature = "iter_copied", since = "1.36.0")]
154 impl<'a, I, T: 'a> FusedIterator for Copied<I>
156 I: FusedIterator<Item = &'a T>,
162 #[unstable(feature = "trusted_random_access", issue = "none")]
163 unsafe impl<I> TrustedRandomAccess for Copied<I> where I: TrustedRandomAccess {}
166 #[unstable(feature = "trusted_random_access", issue = "none")]
167 unsafe impl<I> TrustedRandomAccessNoCoerce for Copied<I>
169 I: TrustedRandomAccessNoCoerce,
171 const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
174 #[stable(feature = "iter_copied", since = "1.36.0")]
175 unsafe impl<'a, I, T: 'a> TrustedLen for Copied<I>
177 I: TrustedLen<Item = &'a T>,
182 trait SpecNextChunk<'a, const N: usize, T: 'a>: Iterator<Item = &'a T>
186 fn spec_next_chunk(&mut self) -> Result<[T; N], array::IntoIter<T, N>>;
189 impl<'a, const N: usize, I, T: 'a> SpecNextChunk<'a, N, T> for I
191 I: Iterator<Item = &'a T>,
194 default fn spec_next_chunk(&mut self) -> Result<[T; N], array::IntoIter<T, N>> {
195 array::iter_next_chunk(&mut self.map(|e| *e))
199 impl<'a, const N: usize, T: 'a> SpecNextChunk<'a, N, T> for crate::slice::Iter<'a, T>
203 fn spec_next_chunk(&mut self) -> Result<[T; N], array::IntoIter<T, N>> {
204 let mut raw_array = MaybeUninit::uninit_array();
206 let len = self.len();
210 let _ = self.advance_by(len);
211 // SAFETY: ZSTs can be conjured ex nihilo; only the amount has to be correct
212 return Err(unsafe { array::IntoIter::new_unchecked(raw_array, 0..len) });
215 let _ = self.advance_by(N);
217 return Ok(unsafe { MaybeUninit::array_assume_init(raw_array) });
221 // SAFETY: `len` indicates that this many elements are available and we just checked that
222 // it fits into the array.
224 ptr::copy_nonoverlapping(
225 self.as_ref().as_ptr(),
226 raw_array.as_mut_ptr() as *mut T,
229 let _ = self.advance_by(len);
230 return Err(array::IntoIter::new_unchecked(raw_array, 0..len));
234 // SAFETY: `len` is larger than the array size. Copy a fixed amount here to fully initialize
237 ptr::copy_nonoverlapping(self.as_ref().as_ptr(), raw_array.as_mut_ptr() as *mut T, N);
238 let _ = self.advance_by(N);
239 Ok(MaybeUninit::array_assume_init(raw_array))