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
use std::marker::PhantomData;

use super::{Pusherator, PusheratorBuild};

pub struct Flatten<Next, In> {
    next: Next,
    _marker: PhantomData<fn(In)>,
}
impl<Next, In> Pusherator for Flatten<Next, In>
where
    Next: Pusherator<Item = In::Item>,
    In: IntoIterator,
{
    type Item = In;
    fn give(&mut self, item: Self::Item) {
        for x in item {
            self.next.give(x);
        }
    }
}
impl<Next, In> Flatten<Next, In>
where
    Next: Pusherator<Item = In::Item>,
    In: IntoIterator,
{
    pub fn new(next: Next) -> Self {
        Self {
            next,
            _marker: PhantomData,
        }
    }
}

pub struct FlattenBuild<Prev>
where
    Prev: PusheratorBuild,
    Prev::ItemOut: IntoIterator,
{
    prev: Prev,
}
impl<Prev> FlattenBuild<Prev>
where
    Prev: PusheratorBuild,
    Prev::ItemOut: IntoIterator,
{
    pub fn new(prev: Prev) -> Self {
        Self { prev }
    }
}
impl<Prev> PusheratorBuild for FlattenBuild<Prev>
where
    Prev: PusheratorBuild,
    Prev::ItemOut: IntoIterator,
{
    type ItemOut = <Prev::ItemOut as IntoIterator>::Item;

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