pusherator/
pivot.rs

1use super::{Pusherator, PusheratorBuild};
2
3pub struct Pivot<I, P>
4where
5    I: Iterator,
6    P: Pusherator<Item = I::Item>,
7{
8    pull: I,
9    push: P,
10}
11impl<I, P> Pivot<I, P>
12where
13    I: Iterator,
14    P: Pusherator<Item = I::Item>,
15{
16    pub fn new(pull: I, push: P) -> Self {
17        Self { pull, push }
18    }
19
20    pub fn step(&mut self) -> bool {
21        if let Some(v) = self.pull.next() {
22            self.push.give(v);
23            true
24        } else {
25            false
26        }
27    }
28
29    pub fn run(mut self) {
30        for v in self.pull.by_ref() {
31            self.push.give(v);
32        }
33    }
34}
35
36pub struct PivotBuild<I>
37where
38    I: Iterator,
39{
40    pull: I,
41}
42impl<I> PivotBuild<I>
43where
44    I: Iterator,
45{
46    pub fn new(pull: I) -> Self {
47        Self { pull }
48    }
49}
50impl<I> PusheratorBuild for PivotBuild<I>
51where
52    I: Iterator,
53{
54    type ItemOut = I::Item;
55
56    type Output<O: Pusherator<Item = Self::ItemOut>> = Pivot<I, O>;
57    fn push_to<O>(self, input: O) -> Self::Output<O>
58    where
59        O: Pusherator<Item = Self::ItemOut>,
60    {
61        Pivot {
62            pull: self.pull,
63            push: input,
64        }
65    }
66}