core/iter/adapters/
cloned.rs1use core::num::NonZero;
2
3use crate::iter::adapters::zip::try_get_unchecked;
4use crate::iter::adapters::{SourceIter, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
5use crate::iter::{FusedIterator, InPlaceIterable, TrustedLen};
6use crate::ops::Try;
7
8#[stable(feature = "iter_cloned", since = "1.1.0")]
16#[must_use = "iterators are lazy and do nothing unless consumed"]
17#[derive(Clone, Debug)]
18#[ferrocene::prevalidated]
19pub struct Cloned<I> {
20 it: I,
21}
22
23impl<I> Cloned<I> {
24 #[ferrocene::prevalidated]
25 pub(in crate::iter) const fn new(it: I) -> Cloned<I> {
26 Cloned { it }
27 }
28}
29
30#[ferrocene::prevalidated]
31#[inline]
32fn clone_try_fold<T: Clone, Acc, R>(mut f: impl FnMut(Acc, T) -> R) -> impl FnMut(Acc, &T) -> R {
33 move |acc, elt| f(acc, elt.clone())
34}
35
36#[stable(feature = "iter_cloned", since = "1.1.0")]
37impl<'a, I, T: 'a> Iterator for Cloned<I>
38where
39 I: Iterator<Item = &'a T>,
40 T: Clone,
41{
42 type Item = T;
43
44 #[ferrocene::prevalidated]
45 fn next(&mut self) -> Option<T> {
46 self.it.next().cloned()
47 }
48
49 #[ferrocene::prevalidated]
50 fn size_hint(&self) -> (usize, Option<usize>) {
51 self.it.size_hint()
52 }
53
54 #[ferrocene::prevalidated]
55 #[inline]
56 fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
57 where
58 Self: Sized,
59 F: FnMut(B, Self::Item) -> R,
60 R: Try<Output = B>,
61 {
62 self.it.try_fold(init, clone_try_fold(f))
63 }
64
65 #[ferrocene::prevalidated]
66 #[inline]
67 fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
68 where
69 F: FnMut(Acc, Self::Item) -> Acc,
70 {
71 self.it.map(T::clone).fold(init, f)
72 }
73
74 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T
75 where
76 Self: TrustedRandomAccessNoCoerce,
77 {
78 unsafe { try_get_unchecked(&mut self.it, idx).clone() }
81 }
82}
83
84#[stable(feature = "iter_cloned", since = "1.1.0")]
85impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
86where
87 I: DoubleEndedIterator<Item = &'a T>,
88 T: Clone,
89{
90 #[ferrocene::prevalidated]
91 fn next_back(&mut self) -> Option<T> {
92 self.it.next_back().cloned()
93 }
94
95 #[ferrocene::prevalidated]
96 #[inline]
97 fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
98 where
99 Self: Sized,
100 F: FnMut(B, Self::Item) -> R,
101 R: Try<Output = B>,
102 {
103 self.it.try_rfold(init, clone_try_fold(f))
104 }
105
106 #[ferrocene::prevalidated]
107 #[inline]
108 fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
109 where
110 F: FnMut(Acc, Self::Item) -> Acc,
111 {
112 self.it.map(T::clone).rfold(init, f)
113 }
114}
115
116#[stable(feature = "iter_cloned", since = "1.1.0")]
117impl<'a, I, T: 'a> ExactSizeIterator for Cloned<I>
118where
119 I: ExactSizeIterator<Item = &'a T>,
120 T: Clone,
121{
122 fn len(&self) -> usize {
123 self.it.len()
124 }
125
126 fn is_empty(&self) -> bool {
127 self.it.is_empty()
128 }
129}
130
131#[stable(feature = "fused", since = "1.26.0")]
132impl<'a, I, T: 'a> FusedIterator for Cloned<I>
133where
134 I: FusedIterator<Item = &'a T>,
135 T: Clone,
136{
137}
138
139#[doc(hidden)]
140#[unstable(feature = "trusted_random_access", issue = "none")]
141unsafe impl<I> TrustedRandomAccess for Cloned<I> where I: TrustedRandomAccess {}
142
143#[doc(hidden)]
144#[unstable(feature = "trusted_random_access", issue = "none")]
145unsafe impl<I> TrustedRandomAccessNoCoerce for Cloned<I>
146where
147 I: TrustedRandomAccessNoCoerce,
148{
149 const MAY_HAVE_SIDE_EFFECT: bool = true;
150}
151
152#[unstable(feature = "trusted_len", issue = "37572")]
153unsafe impl<'a, I, T: 'a> TrustedLen for Cloned<I>
154where
155 I: TrustedLen<Item = &'a T>,
156 T: Clone,
157{
158}
159
160#[stable(feature = "default_iters", since = "1.70.0")]
161impl<I: Default> Default for Cloned<I> {
162 fn default() -> Self {
170 Self::new(Default::default())
171 }
172}
173
174#[unstable(issue = "none", feature = "inplace_iteration")]
175unsafe impl<I> SourceIter for Cloned<I>
176where
177 I: SourceIter,
178{
179 type Source = I::Source;
180
181 #[inline]
182 unsafe fn as_inner(&mut self) -> &mut I::Source {
183 unsafe { SourceIter::as_inner(&mut self.it) }
185 }
186}
187
188#[unstable(issue = "none", feature = "inplace_iteration")]
189unsafe impl<I: InPlaceIterable> InPlaceIterable for Cloned<I> {
190 const EXPAND_BY: Option<NonZero<usize>> = I::EXPAND_BY;
191 const MERGE_BY: Option<NonZero<usize>> = I::MERGE_BY;
192}