pusherator/
partition.rs

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