/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.11.0/src/iter/copied.rs
Line | Count | Source |
1 | | use super::plumbing::*; |
2 | | use super::*; |
3 | | |
4 | | use std::iter; |
5 | | |
6 | | /// `Copied` is an iterator that copies the elements of an underlying iterator. |
7 | | /// |
8 | | /// This struct is created by the [`copied()`] method on [`ParallelIterator`] |
9 | | /// |
10 | | /// [`copied()`]: ParallelIterator::copied() |
11 | | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
12 | | #[derive(Debug, Clone)] |
13 | | pub struct Copied<I> { |
14 | | base: I, |
15 | | } |
16 | | |
17 | | impl<I> Copied<I> { |
18 | | /// Creates a new `Copied` iterator. |
19 | 0 | pub(super) fn new(base: I) -> Self { |
20 | 0 | Copied { base } |
21 | 0 | } |
22 | | } |
23 | | |
24 | | impl<'a, T, I> ParallelIterator for Copied<I> |
25 | | where |
26 | | I: ParallelIterator<Item = &'a T>, |
27 | | T: 'a + Copy + Send + Sync, |
28 | | { |
29 | | type Item = T; |
30 | | |
31 | 0 | fn drive_unindexed<C>(self, consumer: C) -> C::Result |
32 | 0 | where |
33 | 0 | C: UnindexedConsumer<Self::Item>, |
34 | | { |
35 | 0 | let consumer1 = CopiedConsumer::new(consumer); |
36 | 0 | self.base.drive_unindexed(consumer1) |
37 | 0 | } |
38 | | |
39 | 0 | fn opt_len(&self) -> Option<usize> { |
40 | 0 | self.base.opt_len() |
41 | 0 | } |
42 | | } |
43 | | |
44 | | impl<'a, T, I> IndexedParallelIterator for Copied<I> |
45 | | where |
46 | | I: IndexedParallelIterator<Item = &'a T>, |
47 | | T: 'a + Copy + Send + Sync, |
48 | | { |
49 | 0 | fn drive<C>(self, consumer: C) -> C::Result |
50 | 0 | where |
51 | 0 | C: Consumer<Self::Item>, |
52 | | { |
53 | 0 | let consumer1 = CopiedConsumer::new(consumer); |
54 | 0 | self.base.drive(consumer1) |
55 | 0 | } |
56 | | |
57 | 0 | fn len(&self) -> usize { |
58 | 0 | self.base.len() |
59 | 0 | } |
60 | | |
61 | 0 | fn with_producer<CB>(self, callback: CB) -> CB::Output |
62 | 0 | where |
63 | 0 | CB: ProducerCallback<Self::Item>, |
64 | | { |
65 | 0 | return self.base.with_producer(Callback { callback }); |
66 | | |
67 | | struct Callback<CB> { |
68 | | callback: CB, |
69 | | } |
70 | | |
71 | | impl<'a, T, CB> ProducerCallback<&'a T> for Callback<CB> |
72 | | where |
73 | | CB: ProducerCallback<T>, |
74 | | T: 'a + Copy + Send, |
75 | | { |
76 | | type Output = CB::Output; |
77 | | |
78 | 0 | fn callback<P>(self, base: P) -> CB::Output |
79 | 0 | where |
80 | 0 | P: Producer<Item = &'a T>, |
81 | | { |
82 | 0 | let producer = CopiedProducer { base }; |
83 | 0 | self.callback.callback(producer) |
84 | 0 | } |
85 | | } |
86 | 0 | } |
87 | | } |
88 | | |
89 | | // //////////////////////////////////////////////////////////////////////// |
90 | | |
91 | | struct CopiedProducer<P> { |
92 | | base: P, |
93 | | } |
94 | | |
95 | | impl<'a, T, P> Producer for CopiedProducer<P> |
96 | | where |
97 | | P: Producer<Item = &'a T>, |
98 | | T: 'a + Copy, |
99 | | { |
100 | | type Item = T; |
101 | | type IntoIter = iter::Copied<P::IntoIter>; |
102 | | |
103 | 0 | fn into_iter(self) -> Self::IntoIter { |
104 | 0 | self.base.into_iter().copied() |
105 | 0 | } |
106 | | |
107 | 0 | fn min_len(&self) -> usize { |
108 | 0 | self.base.min_len() |
109 | 0 | } |
110 | | |
111 | 0 | fn max_len(&self) -> usize { |
112 | 0 | self.base.max_len() |
113 | 0 | } |
114 | | |
115 | 0 | fn split_at(self, index: usize) -> (Self, Self) { |
116 | 0 | let (left, right) = self.base.split_at(index); |
117 | 0 | ( |
118 | 0 | CopiedProducer { base: left }, |
119 | 0 | CopiedProducer { base: right }, |
120 | 0 | ) |
121 | 0 | } |
122 | | |
123 | 0 | fn fold_with<F>(self, folder: F) -> F |
124 | 0 | where |
125 | 0 | F: Folder<Self::Item>, |
126 | | { |
127 | 0 | self.base.fold_with(CopiedFolder { base: folder }).base |
128 | 0 | } |
129 | | } |
130 | | |
131 | | // //////////////////////////////////////////////////////////////////////// |
132 | | // Consumer implementation |
133 | | |
134 | | struct CopiedConsumer<C> { |
135 | | base: C, |
136 | | } |
137 | | |
138 | | impl<C> CopiedConsumer<C> { |
139 | 0 | fn new(base: C) -> Self { |
140 | 0 | CopiedConsumer { base } |
141 | 0 | } |
142 | | } |
143 | | |
144 | | impl<'a, T, C> Consumer<&'a T> for CopiedConsumer<C> |
145 | | where |
146 | | C: Consumer<T>, |
147 | | T: 'a + Copy, |
148 | | { |
149 | | type Folder = CopiedFolder<C::Folder>; |
150 | | type Reducer = C::Reducer; |
151 | | type Result = C::Result; |
152 | | |
153 | 0 | fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) { |
154 | 0 | let (left, right, reducer) = self.base.split_at(index); |
155 | 0 | ( |
156 | 0 | CopiedConsumer::new(left), |
157 | 0 | CopiedConsumer::new(right), |
158 | 0 | reducer, |
159 | 0 | ) |
160 | 0 | } |
161 | | |
162 | 0 | fn into_folder(self) -> Self::Folder { |
163 | 0 | CopiedFolder { |
164 | 0 | base: self.base.into_folder(), |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | 0 | fn full(&self) -> bool { |
169 | 0 | self.base.full() |
170 | 0 | } |
171 | | } |
172 | | |
173 | | impl<'a, T, C> UnindexedConsumer<&'a T> for CopiedConsumer<C> |
174 | | where |
175 | | C: UnindexedConsumer<T>, |
176 | | T: 'a + Copy, |
177 | | { |
178 | 0 | fn split_off_left(&self) -> Self { |
179 | 0 | CopiedConsumer::new(self.base.split_off_left()) |
180 | 0 | } |
181 | | |
182 | 0 | fn to_reducer(&self) -> Self::Reducer { |
183 | 0 | self.base.to_reducer() |
184 | 0 | } |
185 | | } |
186 | | |
187 | | struct CopiedFolder<F> { |
188 | | base: F, |
189 | | } |
190 | | |
191 | | impl<'a, T, F> Folder<&'a T> for CopiedFolder<F> |
192 | | where |
193 | | F: Folder<T>, |
194 | | T: 'a + Copy, |
195 | | { |
196 | | type Result = F::Result; |
197 | | |
198 | 0 | fn consume(self, &item: &'a T) -> Self { |
199 | 0 | CopiedFolder { |
200 | 0 | base: self.base.consume(item), |
201 | 0 | } |
202 | 0 | } |
203 | | |
204 | 0 | fn consume_iter<I>(mut self, iter: I) -> Self |
205 | 0 | where |
206 | 0 | I: IntoIterator<Item = &'a T>, |
207 | | { |
208 | 0 | self.base = self.base.consume_iter(iter.into_iter().copied()); |
209 | 0 | self |
210 | 0 | } |
211 | | |
212 | 0 | fn complete(self) -> F::Result { |
213 | 0 | self.base.complete() |
214 | 0 | } |
215 | | |
216 | 0 | fn full(&self) -> bool { |
217 | 0 | self.base.full() |
218 | 0 | } |
219 | | } |