1use std::{
2 fmt::{self, Debug},
3 iter,
4};
5
6use super::{plumbing::*, *};
7
8#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
16#[derive(Clone)]
17pub struct Inspect<I: ParallelIterator, F> {
18 base: I,
19 inspect_op: F,
20}
21
22impl<I: ParallelIterator + Debug, F> Debug for Inspect<I, F> {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 f.debug_struct("Inspect").field("base", &self.base).finish()
25 }
26}
27
28impl<I, F> Inspect<I, F>
29where
30 I: ParallelIterator,
31{
32 pub(super) fn new(base: I, inspect_op: F) -> Self {
34 Inspect { base, inspect_op }
35 }
36}
37
38impl<I, F> ParallelIterator for Inspect<I, F>
39where
40 I: ParallelIterator,
41 F: Fn(&I::Item) + Sync + Send,
42{
43 type Item = I::Item;
44
45 fn drive_unindexed<C>(self, consumer: C) -> C::Result
46 where
47 C: UnindexedConsumer<Self::Item>,
48 {
49 let consumer1 = InspectConsumer::new(consumer, &self.inspect_op);
50 self.base.drive_unindexed(consumer1)
51 }
52
53 fn opt_len(&self) -> Option<usize> {
54 self.base.opt_len()
55 }
56}
57
58impl<I, F> IndexedParallelIterator for Inspect<I, F>
59where
60 I: IndexedParallelIterator,
61 F: Fn(&I::Item) + Sync + Send,
62{
63 fn drive<C>(self, consumer: C) -> C::Result
64 where
65 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
77 CB: ProducerCallback<Self::Item>,
78 {
79 return self.base.with_producer(Callback {
80 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
91 CB: ProducerCallback<T>,
92 F: Fn(&T) + Sync,
93 {
94 type Output = CB::Output;
95
96 fn callback<P>(self, base: P) -> CB::Output
97 where
98 P: Producer<Item = T>,
99 {
100 let producer = InspectProducer {
101 base,
102 inspect_op: &self.inspect_op,
103 };
104 self.callback.callback(producer)
105 }
106 }
107 }
108}
109
110struct InspectProducer<'f, P, F> {
113 base: P,
114 inspect_op: &'f F,
115}
116
117impl<'f, P, F> Producer for InspectProducer<'f, P, F>
118where
119 P: Producer,
120 F: Fn(&P::Item) + Sync,
121{
122 type IntoIter = iter::Inspect<P::IntoIter, &'f F>;
123 type Item = P::Item;
124
125 fn into_iter(self) -> Self::IntoIter {
126 self.base.into_iter().inspect(self.inspect_op)
127 }
128
129 fn min_len(&self) -> usize {
130 self.base.min_len()
131 }
132
133 fn max_len(&self) -> usize {
134 self.base.max_len()
135 }
136
137 fn split_at(self, index: usize) -> (Self, Self) {
138 let (left, right) = self.base.split_at(index);
139 (
140 InspectProducer {
141 base: left,
142 inspect_op: self.inspect_op,
143 },
144 InspectProducer {
145 base: right,
146 inspect_op: self.inspect_op,
147 },
148 )
149 }
150
151 fn fold_with<G>(self, folder: G) -> G
152 where
153 G: Folder<Self::Item>,
154 {
155 let folder1 = InspectFolder {
156 base: folder,
157 inspect_op: self.inspect_op,
158 };
159 self.base.fold_with(folder1).base
160 }
161}
162
163struct InspectConsumer<'f, C, F> {
167 base: C,
168 inspect_op: &'f F,
169}
170
171impl<'f, C, F> InspectConsumer<'f, C, F> {
172 fn new(base: C, inspect_op: &'f F) -> Self {
173 InspectConsumer { base, inspect_op }
174 }
175}
176
177impl<'f, T, C, F> Consumer<T> for InspectConsumer<'f, C, F>
178where
179 C: Consumer<T>,
180 F: Fn(&T) + Sync,
181{
182 type Folder = InspectFolder<'f, C::Folder, F>;
183 type Reducer = C::Reducer;
184 type Result = C::Result;
185
186 fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
187 let (left, right, reducer) = self.base.split_at(index);
188 (
189 InspectConsumer::new(left, self.inspect_op),
190 InspectConsumer::new(right, self.inspect_op),
191 reducer,
192 )
193 }
194
195 fn into_folder(self) -> Self::Folder {
196 InspectFolder {
197 base: self.base.into_folder(),
198 inspect_op: self.inspect_op,
199 }
200 }
201
202 fn full(&self) -> bool {
203 self.base.full()
204 }
205}
206
207impl<'f, T, C, F> UnindexedConsumer<T> for InspectConsumer<'f, C, F>
208where
209 C: UnindexedConsumer<T>,
210 F: Fn(&T) + Sync,
211{
212 fn split_off_left(&self) -> Self {
213 InspectConsumer::new(self.base.split_off_left(), self.inspect_op)
214 }
215
216 fn to_reducer(&self) -> Self::Reducer {
217 self.base.to_reducer()
218 }
219}
220
221struct InspectFolder<'f, C, F> {
222 base: C,
223 inspect_op: &'f F,
224}
225
226impl<'f, T, C, F> Folder<T> for InspectFolder<'f, C, F>
227where
228 C: Folder<T>,
229 F: Fn(&T),
230{
231 type Result = C::Result;
232
233 fn consume(self, item: T) -> Self {
234 (self.inspect_op)(&item);
235 InspectFolder {
236 base: self.base.consume(item),
237 inspect_op: self.inspect_op,
238 }
239 }
240
241 fn consume_iter<I>(mut self, iter: I) -> Self
242 where
243 I: IntoIterator<Item = T>,
244 {
245 self.base = self
246 .base
247 .consume_iter(iter.into_iter().inspect(self.inspect_op));
248 self
249 }
250
251 fn complete(self) -> C::Result {
252 self.base.complete()
253 }
254
255 fn full(&self) -> bool {
256 self.base.full()
257 }
258}