]> git.lizzy.rs Git - rust.git/blob - library/core/src/iter/adapters/copied.rs
Rollup merge of #102854 - semarie:openbsd-immutablestack, r=m-ou-se
[rust.git] / library / core / src / iter / adapters / copied.rs
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> {
22     pub(in crate::iter) fn new(it: I) -> Copied<I> {
23         Copied { it }
24     }
25 }
26
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)
29 }
30
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)
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
43     fn next(&mut self) -> Option<T> {
44         self.it.next().copied()
45     }
46
47     fn size_hint(&self) -> (usize, Option<usize>) {
48         self.it.size_hint()
49     }
50
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
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
67     fn nth(&mut self, n: usize) -> Option<T> {
68         self.it.nth(n).copied()
69     }
70
71     fn last(self) -> Option<T> {
72         self.it.last().copied()
73     }
74
75     fn count(self) -> usize {
76         self.it.count()
77     }
78
79     #[inline]
80     fn advance_by(&mut self, n: usize) -> Result<(), usize> {
81         self.it.advance_by(n)
82     }
83
84     unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T
85     where
86         Self: TrustedRandomAccessNoCoerce,
87     {
88         // SAFETY: the caller must uphold the contract for
89         // `Iterator::__iterator_get_unchecked`.
90         *unsafe { try_get_unchecked(&mut self.it, idx) }
91     }
92 }
93
94 #[stable(feature = "iter_copied", since = "1.36.0")]
95 impl<'a, I, T: 'a> DoubleEndedIterator for Copied<I>
96 where
97     I: DoubleEndedIterator<Item = &'a T>,
98     T: Copy,
99 {
100     fn next_back(&mut self) -> Option<T> {
101         self.it.next_back().copied()
102     }
103
104     fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
105     where
106         Self: Sized,
107         F: FnMut(B, Self::Item) -> R,
108         R: Try<Output = B>,
109     {
110         self.it.try_rfold(init, copy_try_fold(f))
111     }
112
113     fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
114     where
115         F: FnMut(Acc, Self::Item) -> Acc,
116     {
117         self.it.rfold(init, copy_fold(f))
118     }
119
120     #[inline]
121     fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
122         self.it.advance_back_by(n)
123     }
124 }
125
126 #[stable(feature = "iter_copied", since = "1.36.0")]
127 impl<'a, I, T: 'a> ExactSizeIterator for Copied<I>
128 where
129     I: ExactSizeIterator<Item = &'a T>,
130     T: Copy,
131 {
132     fn len(&self) -> usize {
133         self.it.len()
134     }
135
136     fn is_empty(&self) -> bool {
137         self.it.is_empty()
138     }
139 }
140
141 #[stable(feature = "iter_copied", since = "1.36.0")]
142 impl<'a, I, T: 'a> FusedIterator for Copied<I>
143 where
144     I: FusedIterator<Item = &'a T>,
145     T: Copy,
146 {
147 }
148
149 #[doc(hidden)]
150 #[unstable(feature = "trusted_random_access", issue = "none")]
151 unsafe impl<I> TrustedRandomAccess for Copied<I> where I: TrustedRandomAccess {}
152
153 #[doc(hidden)]
154 #[unstable(feature = "trusted_random_access", issue = "none")]
155 unsafe impl<I> TrustedRandomAccessNoCoerce for Copied<I>
156 where
157     I: TrustedRandomAccessNoCoerce,
158 {
159     const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
160 }
161
162 #[stable(feature = "iter_copied", since = "1.36.0")]
163 unsafe impl<'a, I, T: 'a> TrustedLen for Copied<I>
164 where
165     I: TrustedLen<Item = &'a T>,
166     T: Copy,
167 {
168 }