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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use crate::common::*;

pub type BoxFutureFactory<'a, In, Out> = Box<dyn 'a + FnMut(In) -> BoxFuture<'static, Out> + Send>;

pub use future_factory_::*;
mod future_factory_ {
    use super::*;

    pub trait FutureFactory<In>
    where
        In: 'static + Send,
        Self::Fut: 'static + Send + Future,
        <Self::Fut as Future>::Output: 'static + Send,
    {
        type Fut;

        fn generate(&mut self, input: In) -> Self::Fut;

        fn boxed<'a>(mut self) -> BoxFutureFactory<'a, In, <Self::Fut as Future>::Output>
        where
            Self: 'a + Sized + Send,
        {
            Box::new(move |input: In| self.generate(input).boxed())
        }

        fn compose<G>(self, other: G) -> ComposeFutureFactory<In, Self, G>
        where
            Self: Sized,
            G: 'static + Send + Clone + FutureFactory<<Self::Fut as Future>::Output>,
            <G::Fut as Future>::Output: Send,
        {
            ComposeFutureFactory::new(self, other)
        }
    }

    impl<F, In, Fut> FutureFactory<In> for F
    where
        F: FnMut(In) -> Fut,
        In: 'static + Send,
        Fut: 'static + Send + Future,
        Fut::Output: 'static + Send,
    {
        type Fut = Fut;

        fn generate(&mut self, input: In) -> Self::Fut {
            self(input)
        }
    }
}

pub use compose_future_factory::*;
mod compose_future_factory {
    use super::*;

    pub struct ComposeFutureFactory<In, F, G>
    where
        F: FutureFactory<In>,
        G: FutureFactory<<F::Fut as Future>::Output>,
        In: 'static + Send,
        <F::Fut as Future>::Output: 'static + Send,
        <G::Fut as Future>::Output: 'static + Send,
        F::Fut: 'static + Send + Future,
        G::Fut: 'static + Send + Future,
    {
        f: F,
        g: G,
        _phantom: PhantomData<In>,
    }

    impl<In, F, G> ComposeFutureFactory<In, F, G>
    where
        F: FutureFactory<In>,
        G: FutureFactory<<F::Fut as Future>::Output>,
        In: 'static + Send,
        <F::Fut as Future>::Output: 'static + Send,
        <G::Fut as Future>::Output: 'static + Send,
        F::Fut: 'static + Send + Future,
        G::Fut: 'static + Send + Future,
    {
        pub fn new(f: F, g: G) -> Self {
            Self {
                f,
                g,
                _phantom: PhantomData,
            }
        }
    }

    impl<In, F, G> FutureFactory<In> for ComposeFutureFactory<In, F, G>
    where
        F: FutureFactory<In>,
        G: 'static + Send + Clone + FutureFactory<<F::Fut as Future>::Output>,
        In: 'static + Send,
        <F::Fut as Future>::Output: 'static + Send,
        <G::Fut as Future>::Output: 'static + Send,
        F::Fut: 'static + Send + Future,
        G::Fut: 'static + Send + Future,
    {
        type Fut = FutFacChain<F::Fut, G>;

        fn generate(&mut self, input: In) -> FutFacChain<F::Fut, G> {
            FutFacChain::new(self.f.generate(input), self.g.clone())
        }
    }

    impl<In, F, G> Clone for ComposeFutureFactory<In, F, G>
    where
        F: Clone + FutureFactory<In>,
        G: Send + Clone + FutureFactory<<F::Fut as Future>::Output>,
        In: 'static + Send,
        <F::Fut as Future>::Output: 'static + Send,
        <G::Fut as Future>::Output: 'static + Send,
        F::Fut: 'static + Send + Future,
        G::Fut: 'static + Send + Future,
    {
        fn clone(&self) -> Self {
            Self {
                f: self.f.clone(),
                g: self.g.clone(),
                _phantom: PhantomData,
            }
        }
    }
}

pub use fut_fac_chain::*;
mod fut_fac_chain {
    use super::*;

    #[pin_project]
    pub struct FutFacChain<Fut, Fac>
    where
        Fut::Output: 'static + Send,
        <Fac::Fut as Future>::Output: 'static + Send,
        Fut: Future,
        Fac::Fut: 'static + Send + Future,
        Fac: FutureFactory<Fut::Output>,
    {
        #[pin]
        ffut: Option<Fut>,
        #[pin]
        gfut: Option<Fac::Fut>,
        fac: Fac,
    }

    impl<Fut, Fac> FutFacChain<Fut, Fac>
    where
        Fut::Output: 'static + Send,
        <Fac::Fut as Future>::Output: 'static + Send,
        Fut: Future,
        Fac::Fut: 'static + Send + Future,
        Fac: FutureFactory<Fut::Output>,
    {
        pub fn new(fut: Fut, fac: Fac) -> Self {
            Self {
                ffut: Some(fut),
                gfut: None,
                fac,
            }
        }
    }

    impl<Fut, Fac> Future for FutFacChain<Fut, Fac>
    where
        Fut::Output: 'static + Send,
        <Fac::Fut as Future>::Output: 'static + Send,
        Fut: Future,
        Fac::Fut: 'static + Send + Future,
        Fac: FutureFactory<Fut::Output>,
    {
        type Output = <Fac::Fut as Future>::Output;

        fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
            let mut this = self.project();

            Ready(loop {
                if let Some(ffut) = this.ffut.as_mut().as_pin_mut() {
                    let fout = ready!(ffut.poll(cx));
                    this.ffut.set(None);
                    let gfut = this.fac.generate(fout);
                    this.gfut.set(Some(gfut))
                } else if let Some(gfut) = this.gfut.as_mut().as_pin_mut() {
                    let gout = ready!(gfut.poll(cx));
                    this.gfut.set(None);
                    break gout;
                } else {
                    unreachable!()
                }
            })
        }
    }
}