]> git.proxmox.com Git - rustc.git/blob - src/vendor/rayon/src/iter/inspect.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / vendor / rayon / src / iter / inspect.rs
1 use super::plumbing::*;
2 use super::*;
3
4 use std::fmt::{self, Debug};
5 use std::iter;
6
7
8 /// `Inspect` is an iterator that calls a function with a reference to each
9 /// element before yielding it.
10 ///
11 /// This struct is created by the [`inspect()`] method on [`ParallelIterator`]
12 ///
13 /// [`inspect()`]: trait.ParallelIterator.html#method.inspect
14 /// [`ParallelIterator`]: trait.ParallelIterator.html
15 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
16 #[derive(Clone)]
17 pub struct Inspect<I: ParallelIterator, F> {
18 base: I,
19 inspect_op: F,
20 }
21
22 impl<I: ParallelIterator + Debug, F> Debug for Inspect<I, F> {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 f.debug_struct("Inspect")
25 .field("base", &self.base)
26 .finish()
27 }
28 }
29
30 /// Create a new `Inspect` iterator.
31 ///
32 /// NB: a free fn because it is NOT part of the end-user API.
33 pub fn new<I, F>(base: I, inspect_op: F) -> Inspect<I, F>
34 where I: ParallelIterator
35 {
36 Inspect {
37 base: base,
38 inspect_op: inspect_op,
39 }
40 }
41
42 impl<I, F> ParallelIterator for Inspect<I, F>
43 where I: ParallelIterator,
44 F: Fn(&I::Item) + Sync + Send
45 {
46 type Item = I::Item;
47
48 fn drive_unindexed<C>(self, consumer: C) -> C::Result
49 where C: UnindexedConsumer<Self::Item>
50 {
51 let consumer1 = InspectConsumer::new(consumer, &self.inspect_op);
52 self.base.drive_unindexed(consumer1)
53 }
54
55 fn opt_len(&self) -> Option<usize> {
56 self.base.opt_len()
57 }
58 }
59
60 impl<I, F> IndexedParallelIterator for Inspect<I, F>
61 where I: IndexedParallelIterator,
62 F: Fn(&I::Item) + Sync + Send
63 {
64 fn drive<C>(self, consumer: C) -> C::Result
65 where C: Consumer<Self::Item>
66 {
67 let consumer1 = InspectConsumer::new(consumer, &self.inspect_op);
68 self.base.drive(consumer1)
69 }
70
71 fn len(&self) -> usize {
72 self.base.len()
73 }
74
75 fn with_producer<CB>(self, callback: CB) -> CB::Output
76 where CB: ProducerCallback<Self::Item>
77 {
78 return self.base
79 .with_producer(Callback {
80 callback: callback,
81 inspect_op: self.inspect_op,
82 });
83
84 struct Callback<CB, F> {
85 callback: CB,
86 inspect_op: F,
87 }
88
89 impl<T, F, CB> ProducerCallback<T> for Callback<CB, F>
90 where CB: ProducerCallback<T>,
91 F: Fn(&T) + Sync
92 {
93 type Output = CB::Output;
94
95 fn callback<P>(self, base: P) -> CB::Output
96 where P: Producer<Item = T>
97 {
98 let producer = InspectProducer {
99 base: base,
100 inspect_op: &self.inspect_op,
101 };
102 self.callback.callback(producer)
103 }
104 }
105 }
106 }
107
108 /// ////////////////////////////////////////////////////////////////////////
109
110 struct InspectProducer<'f, P, F: 'f> {
111 base: P,
112 inspect_op: &'f F,
113 }
114
115 impl<'f, P, F> Producer for InspectProducer<'f, P, F>
116 where P: Producer,
117 F: Fn(&P::Item) + Sync
118 {
119 type Item = P::Item;
120 type IntoIter = iter::Inspect<P::IntoIter, &'f F>;
121
122 fn into_iter(self) -> Self::IntoIter {
123 self.base.into_iter().inspect(self.inspect_op)
124 }
125
126 fn min_len(&self) -> usize {
127 self.base.min_len()
128 }
129
130 fn max_len(&self) -> usize {
131 self.base.max_len()
132 }
133
134 fn split_at(self, index: usize) -> (Self, Self) {
135 let (left, right) = self.base.split_at(index);
136 (InspectProducer {
137 base: left,
138 inspect_op: self.inspect_op,
139 },
140 InspectProducer {
141 base: right,
142 inspect_op: self.inspect_op,
143 })
144 }
145
146 fn fold_with<G>(self, folder: G) -> G
147 where G: Folder<Self::Item>
148 {
149 let folder1 = InspectFolder { base: folder, inspect_op: self.inspect_op };
150 self.base.fold_with(folder1).base
151 }
152 }
153
154
155 /// ////////////////////////////////////////////////////////////////////////
156 /// Consumer implementation
157
158 struct InspectConsumer<'f, C, F: 'f> {
159 base: C,
160 inspect_op: &'f F,
161 }
162
163 impl<'f, C, F> InspectConsumer<'f, C, F> {
164 fn new(base: C, inspect_op: &'f F) -> Self {
165 InspectConsumer {
166 base: base,
167 inspect_op: inspect_op,
168 }
169 }
170 }
171
172 impl<'f, T, C, F> Consumer<T> for InspectConsumer<'f, C, F>
173 where C: Consumer<T>,
174 F: Fn(&T) + Sync
175 {
176 type Folder = InspectFolder<'f, C::Folder, F>;
177 type Reducer = C::Reducer;
178 type Result = C::Result;
179
180 fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
181 let (left, right, reducer) = self.base.split_at(index);
182 (InspectConsumer::new(left, self.inspect_op),
183 InspectConsumer::new(right, self.inspect_op),
184 reducer)
185 }
186
187 fn into_folder(self) -> Self::Folder {
188 InspectFolder {
189 base: self.base.into_folder(),
190 inspect_op: self.inspect_op,
191 }
192 }
193
194 fn full(&self) -> bool {
195 self.base.full()
196 }
197 }
198
199 impl<'f, T, C, F> UnindexedConsumer<T> for InspectConsumer<'f, C, F>
200 where C: UnindexedConsumer<T>,
201 F: Fn(&T) + Sync
202 {
203 fn split_off_left(&self) -> Self {
204 InspectConsumer::new(self.base.split_off_left(), &self.inspect_op)
205 }
206
207 fn to_reducer(&self) -> Self::Reducer {
208 self.base.to_reducer()
209 }
210 }
211
212 struct InspectFolder<'f, C, F: 'f> {
213 base: C,
214 inspect_op: &'f F,
215 }
216
217 impl<'f, T, C, F> Folder<T> for InspectFolder<'f, C, F>
218 where C: Folder<T>,
219 F: Fn(&T)
220 {
221 type Result = C::Result;
222
223 fn consume(self, item: T) -> Self {
224 (self.inspect_op)(&item);
225 InspectFolder {
226 base: self.base.consume(item),
227 inspect_op: self.inspect_op,
228 }
229 }
230
231 fn complete(self) -> C::Result {
232 self.base.complete()
233 }
234
235 fn full(&self) -> bool {
236 self.base.full()
237 }
238 }