/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.11.0/src/iter/update.rs
Line | Count | Source |
1 | | use super::plumbing::*; |
2 | | use super::*; |
3 | | |
4 | | use std::fmt::{self, Debug}; |
5 | | |
6 | | /// `Update` is an iterator that mutates the elements of an |
7 | | /// underlying iterator before they are yielded. |
8 | | /// |
9 | | /// This struct is created by the [`update()`] method on [`ParallelIterator`] |
10 | | /// |
11 | | /// [`update()`]: ParallelIterator::update() |
12 | | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
13 | | #[derive(Clone)] |
14 | | pub struct Update<I, F> { |
15 | | base: I, |
16 | | update_op: F, |
17 | | } |
18 | | |
19 | | impl<I: Debug, F> Debug for Update<I, F> { |
20 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
21 | 0 | f.debug_struct("Update").field("base", &self.base).finish() |
22 | 0 | } |
23 | | } |
24 | | |
25 | | impl<I, F> Update<I, F> { |
26 | | /// Creates a new `Update` iterator. |
27 | 0 | pub(super) fn new(base: I, update_op: F) -> Self { |
28 | 0 | Update { base, update_op } |
29 | 0 | } |
30 | | } |
31 | | |
32 | | impl<I, F> ParallelIterator for Update<I, F> |
33 | | where |
34 | | I: ParallelIterator, |
35 | | F: Fn(&mut I::Item) + Send + Sync, |
36 | | { |
37 | | type Item = I::Item; |
38 | | |
39 | 0 | fn drive_unindexed<C>(self, consumer: C) -> C::Result |
40 | 0 | where |
41 | 0 | C: UnindexedConsumer<Self::Item>, |
42 | | { |
43 | 0 | let consumer1 = UpdateConsumer::new(consumer, &self.update_op); |
44 | 0 | self.base.drive_unindexed(consumer1) |
45 | 0 | } |
46 | | |
47 | 0 | fn opt_len(&self) -> Option<usize> { |
48 | 0 | self.base.opt_len() |
49 | 0 | } |
50 | | } |
51 | | |
52 | | impl<I, F> IndexedParallelIterator for Update<I, F> |
53 | | where |
54 | | I: IndexedParallelIterator, |
55 | | F: Fn(&mut I::Item) + Send + Sync, |
56 | | { |
57 | 0 | fn drive<C>(self, consumer: C) -> C::Result |
58 | 0 | where |
59 | 0 | C: Consumer<Self::Item>, |
60 | | { |
61 | 0 | let consumer1 = UpdateConsumer::new(consumer, &self.update_op); |
62 | 0 | self.base.drive(consumer1) |
63 | 0 | } |
64 | | |
65 | 0 | fn len(&self) -> usize { |
66 | 0 | self.base.len() |
67 | 0 | } |
68 | | |
69 | 0 | fn with_producer<CB>(self, callback: CB) -> CB::Output |
70 | 0 | where |
71 | 0 | CB: ProducerCallback<Self::Item>, |
72 | | { |
73 | 0 | return self.base.with_producer(Callback { |
74 | 0 | callback, |
75 | 0 | update_op: self.update_op, |
76 | 0 | }); |
77 | | |
78 | | struct Callback<CB, F> { |
79 | | callback: CB, |
80 | | update_op: F, |
81 | | } |
82 | | |
83 | | impl<T, F, CB> ProducerCallback<T> for Callback<CB, F> |
84 | | where |
85 | | CB: ProducerCallback<T>, |
86 | | F: Fn(&mut T) + Send + Sync, |
87 | | { |
88 | | type Output = CB::Output; |
89 | | |
90 | 0 | fn callback<P>(self, base: P) -> CB::Output |
91 | 0 | where |
92 | 0 | P: Producer<Item = T>, |
93 | | { |
94 | 0 | let producer = UpdateProducer { |
95 | 0 | base, |
96 | 0 | update_op: &self.update_op, |
97 | 0 | }; |
98 | 0 | self.callback.callback(producer) |
99 | 0 | } |
100 | | } |
101 | 0 | } |
102 | | } |
103 | | |
104 | | // //////////////////////////////////////////////////////////////////////// |
105 | | |
106 | | struct UpdateProducer<'f, P, F> { |
107 | | base: P, |
108 | | update_op: &'f F, |
109 | | } |
110 | | |
111 | | impl<'f, P, F> Producer for UpdateProducer<'f, P, F> |
112 | | where |
113 | | P: Producer, |
114 | | F: Fn(&mut P::Item) + Send + Sync, |
115 | | { |
116 | | type Item = P::Item; |
117 | | type IntoIter = UpdateSeq<P::IntoIter, &'f F>; |
118 | | |
119 | 0 | fn into_iter(self) -> Self::IntoIter { |
120 | 0 | UpdateSeq { |
121 | 0 | base: self.base.into_iter(), |
122 | 0 | update_op: self.update_op, |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | 0 | fn min_len(&self) -> usize { |
127 | 0 | self.base.min_len() |
128 | 0 | } |
129 | 0 | fn max_len(&self) -> usize { |
130 | 0 | self.base.max_len() |
131 | 0 | } |
132 | | |
133 | 0 | fn split_at(self, index: usize) -> (Self, Self) { |
134 | 0 | let (left, right) = self.base.split_at(index); |
135 | 0 | ( |
136 | 0 | UpdateProducer { |
137 | 0 | base: left, |
138 | 0 | update_op: self.update_op, |
139 | 0 | }, |
140 | 0 | UpdateProducer { |
141 | 0 | base: right, |
142 | 0 | update_op: self.update_op, |
143 | 0 | }, |
144 | 0 | ) |
145 | 0 | } |
146 | | |
147 | 0 | fn fold_with<G>(self, folder: G) -> G |
148 | 0 | where |
149 | 0 | G: Folder<Self::Item>, |
150 | | { |
151 | 0 | let folder1 = UpdateFolder { |
152 | 0 | base: folder, |
153 | 0 | update_op: self.update_op, |
154 | 0 | }; |
155 | 0 | self.base.fold_with(folder1).base |
156 | 0 | } |
157 | | } |
158 | | |
159 | | // //////////////////////////////////////////////////////////////////////// |
160 | | // Consumer implementation |
161 | | |
162 | | struct UpdateConsumer<'f, C, F> { |
163 | | base: C, |
164 | | update_op: &'f F, |
165 | | } |
166 | | |
167 | | impl<'f, C, F> UpdateConsumer<'f, C, F> { |
168 | 0 | fn new(base: C, update_op: &'f F) -> Self { |
169 | 0 | UpdateConsumer { base, update_op } |
170 | 0 | } |
171 | | } |
172 | | |
173 | | impl<'f, T, C, F> Consumer<T> for UpdateConsumer<'f, C, F> |
174 | | where |
175 | | C: Consumer<T>, |
176 | | F: Fn(&mut T) + Send + Sync, |
177 | | { |
178 | | type Folder = UpdateFolder<'f, C::Folder, F>; |
179 | | type Reducer = C::Reducer; |
180 | | type Result = C::Result; |
181 | | |
182 | 0 | fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) { |
183 | 0 | let (left, right, reducer) = self.base.split_at(index); |
184 | 0 | ( |
185 | 0 | UpdateConsumer::new(left, self.update_op), |
186 | 0 | UpdateConsumer::new(right, self.update_op), |
187 | 0 | reducer, |
188 | 0 | ) |
189 | 0 | } |
190 | | |
191 | 0 | fn into_folder(self) -> Self::Folder { |
192 | 0 | UpdateFolder { |
193 | 0 | base: self.base.into_folder(), |
194 | 0 | update_op: self.update_op, |
195 | 0 | } |
196 | 0 | } |
197 | | |
198 | 0 | fn full(&self) -> bool { |
199 | 0 | self.base.full() |
200 | 0 | } |
201 | | } |
202 | | |
203 | | impl<'f, T, C, F> UnindexedConsumer<T> for UpdateConsumer<'f, C, F> |
204 | | where |
205 | | C: UnindexedConsumer<T>, |
206 | | F: Fn(&mut T) + Send + Sync, |
207 | | { |
208 | 0 | fn split_off_left(&self) -> Self { |
209 | 0 | UpdateConsumer::new(self.base.split_off_left(), self.update_op) |
210 | 0 | } |
211 | | |
212 | 0 | fn to_reducer(&self) -> Self::Reducer { |
213 | 0 | self.base.to_reducer() |
214 | 0 | } |
215 | | } |
216 | | |
217 | | struct UpdateFolder<'f, C, F> { |
218 | | base: C, |
219 | | update_op: &'f F, |
220 | | } |
221 | | |
222 | 0 | fn apply<T>(update_op: impl Fn(&mut T)) -> impl Fn(T) -> T { |
223 | 0 | move |mut item| { |
224 | 0 | update_op(&mut item); |
225 | 0 | item |
226 | 0 | } |
227 | 0 | } |
228 | | |
229 | | impl<'f, T, C, F> Folder<T> for UpdateFolder<'f, C, F> |
230 | | where |
231 | | C: Folder<T>, |
232 | | F: Fn(&mut T), |
233 | | { |
234 | | type Result = C::Result; |
235 | | |
236 | 0 | fn consume(self, mut item: T) -> Self { |
237 | 0 | (self.update_op)(&mut item); |
238 | | |
239 | 0 | UpdateFolder { |
240 | 0 | base: self.base.consume(item), |
241 | 0 | update_op: self.update_op, |
242 | 0 | } |
243 | 0 | } |
244 | | |
245 | 0 | fn consume_iter<I>(mut self, iter: I) -> Self |
246 | 0 | where |
247 | 0 | I: IntoIterator<Item = T>, |
248 | | { |
249 | 0 | let update_op = self.update_op; |
250 | 0 | self.base = self |
251 | 0 | .base |
252 | 0 | .consume_iter(iter.into_iter().map(apply(update_op))); |
253 | 0 | self |
254 | 0 | } |
255 | | |
256 | 0 | fn complete(self) -> C::Result { |
257 | 0 | self.base.complete() |
258 | 0 | } |
259 | | |
260 | 0 | fn full(&self) -> bool { |
261 | 0 | self.base.full() |
262 | 0 | } |
263 | | } |
264 | | |
265 | | /// Standard Update adaptor, based on `itertools::adaptors::Update` |
266 | | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
267 | | #[derive(Debug, Clone)] |
268 | | struct UpdateSeq<I, F> { |
269 | | base: I, |
270 | | update_op: F, |
271 | | } |
272 | | |
273 | | impl<I, F> Iterator for UpdateSeq<I, F> |
274 | | where |
275 | | I: Iterator, |
276 | | F: Fn(&mut I::Item), |
277 | | { |
278 | | type Item = I::Item; |
279 | | |
280 | 0 | fn next(&mut self) -> Option<Self::Item> { |
281 | 0 | let mut v = self.base.next()?; |
282 | 0 | (self.update_op)(&mut v); |
283 | 0 | Some(v) |
284 | 0 | } |
285 | | |
286 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
287 | 0 | self.base.size_hint() |
288 | 0 | } |
289 | | |
290 | 0 | fn fold<Acc, G>(self, init: Acc, g: G) -> Acc |
291 | 0 | where |
292 | 0 | G: FnMut(Acc, Self::Item) -> Acc, |
293 | | { |
294 | 0 | self.base.map(apply(self.update_op)).fold(init, g) |
295 | 0 | } |
296 | | |
297 | | // if possible, re-use inner iterator specializations in collect |
298 | 0 | fn collect<C>(self) -> C |
299 | 0 | where |
300 | 0 | C: ::std::iter::FromIterator<Self::Item>, |
301 | | { |
302 | 0 | self.base.map(apply(self.update_op)).collect() |
303 | 0 | } |
304 | | } |
305 | | |
306 | | impl<I, F> ExactSizeIterator for UpdateSeq<I, F> |
307 | | where |
308 | | I: ExactSizeIterator, |
309 | | F: Fn(&mut I::Item), |
310 | | { |
311 | | } |
312 | | |
313 | | impl<I, F> DoubleEndedIterator for UpdateSeq<I, F> |
314 | | where |
315 | | I: DoubleEndedIterator, |
316 | | F: Fn(&mut I::Item), |
317 | | { |
318 | 0 | fn next_back(&mut self) -> Option<Self::Item> { |
319 | 0 | let mut v = self.base.next_back()?; |
320 | 0 | (self.update_op)(&mut v); |
321 | 0 | Some(v) |
322 | 0 | } |
323 | | } |