1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use super::{Pusherator, PusheratorBuild};

pub struct Filter<Next, Func> {
    next: Next,
    func: Func,
}
impl<Next, Func> Pusherator for Filter<Next, Func>
where
    Next: Pusherator,
    Func: FnMut(&Next::Item) -> bool,
{
    type Item = Next::Item;
    fn give(&mut self, item: Self::Item) {
        if (self.func)(&item) {
            self.next.give(item);
        }
    }
}
impl<Next, Func> Filter<Next, Func>
where
    Next: Pusherator,
    Func: FnMut(&Next::Item) -> bool,
{
    pub fn new(func: Func, next: Next) -> Self {
        Self { next, func }
    }
}

pub struct FilterBuild<Prev, Func>
where
    Prev: PusheratorBuild,
    Func: FnMut(&Prev::ItemOut) -> bool,
{
    prev: Prev,
    func: Func,
}
impl<Prev, Func> FilterBuild<Prev, Func>
where
    Prev: PusheratorBuild,
    Func: FnMut(&Prev::ItemOut) -> bool,
{
    pub fn new(prev: Prev, func: Func) -> Self {
        Self { prev, func }
    }
}
impl<Prev, Func> PusheratorBuild for FilterBuild<Prev, Func>
where
    Prev: PusheratorBuild,
    Func: FnMut(&Prev::ItemOut) -> bool,
{
    type ItemOut = Prev::ItemOut;

    type Output<Next: Pusherator<Item = Self::ItemOut>> = Prev::Output<Filter<Next, Func>>;
    fn push_to<Next>(self, input: Next) -> Self::Output<Next>
    where
        Next: Pusherator<Item = Self::ItemOut>,
    {
        self.prev.push_to(Filter::new(self.func, input))
    }
}