pusherator/
filter.rs

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