pusherator/
inspect.rs

1use super::{Pusherator, PusheratorBuild};
2
3pub struct Inspect<Next, Func> {
4    next: Next,
5    func: Func,
6}
7impl<Next, Func> Pusherator for Inspect<Next, Func>
8where
9    Next: Pusherator,
10    Func: FnMut(&Next::Item),
11{
12    type Item = Next::Item;
13
14    fn give(&mut self, item: Self::Item) {
15        (self.func)(&item);
16        self.next.give(item);
17    }
18}
19impl<Next, Func> Inspect<Next, Func>
20where
21    Next: Pusherator,
22    Func: FnMut(&Next::Item),
23{
24    pub fn new(func: Func, next: Next) -> Self {
25        Self { next, func }
26    }
27}
28
29pub struct InspectBuild<Prev, Func> {
30    prev: Prev,
31    func: Func,
32}
33impl<Prev, Func> InspectBuild<Prev, Func>
34where
35    Prev: PusheratorBuild,
36    Func: FnMut(&Prev::ItemOut),
37{
38    pub fn new(prev: Prev, func: Func) -> Self {
39        Self { prev, func }
40    }
41}
42impl<Prev, Func> PusheratorBuild for InspectBuild<Prev, Func>
43where
44    Prev: PusheratorBuild,
45    Func: FnMut(&Prev::ItemOut),
46{
47    type ItemOut = Prev::ItemOut;
48
49    type Output<Next: Pusherator<Item = Self::ItemOut>> = Prev::Output<Inspect<Next, Func>>;
50    fn push_to<Next>(self, next: Next) -> Self::Output<Next>
51    where
52        Next: Pusherator<Item = Self::ItemOut>,
53    {
54        self.prev.push_to(Inspect::new(self.func, next))
55    }
56}