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