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
61
62
63
64
65
66
67
use std::marker::PhantomData;

use super::{Pusherator, PusheratorBuild};

pub struct FilterMap<Next, Func, In> {
    next: Next,
    func: Func,
    _marker: PhantomData<fn(In)>,
}
impl<Next, Func, In> Pusherator for FilterMap<Next, Func, In>
where
    Next: Pusherator,
    Func: FnMut(In) -> Option<Next::Item>,
{
    type Item = In;
    fn give(&mut self, item: Self::Item) {
        if let Some(item) = (self.func)(item) {
            self.next.give(item);
        }
    }
}
impl<Next, Func, In> FilterMap<Next, Func, In>
where
    Next: Pusherator,
    Func: FnMut(In) -> Option<Next::Item>,
{
    pub fn new(func: Func, next: Next) -> Self {
        Self {
            next,
            func,
            _marker: PhantomData,
        }
    }
}

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

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