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
use core::{
    ops::ControlFlow,
    pin::Pin,
    task::{Context, Poll},
};

use futures::Future;
use pin_project::pin_project;

use crate::effect::{Effective, ErasedEffective, Ss};

use super::{map::Map, Async, BoxOrReady};

#[pin_project(project = ThenStateProj, project_replace = ThenStateReplace)]
enum ThenState<'a, Fut, F, V>
where
    V: Effective<'a>,
{
    Incomplete {
        #[pin]
        future: Fut,
        f: F,
    },
    Completed {
        #[pin]
        effective: V::IntoFuture,
    },
    Temp,
}

#[pin_project]
pub struct Then<'a, Fut, F, V>
where
    V: Effective<'a>,
{
    #[pin]
    state: ThenState<'a, Fut, F, V>,
}

impl<'a, Fut, F, V> Then<'a, Fut, F, V>
where
    V: Effective<'a>,
{
    pub(super) fn new(future: Fut, f: F) -> Self {
        Self {
            state: ThenState::Incomplete { future, f },
        }
    }
}

impl<'a, Fut, F, V> Future for Then<'a, Fut, F, V>
where
    Fut: Future,
    F: FnOnce(Fut::Output) -> V,
    V: Effective<'a>,
{
    type Output = V::Output;

    #[inline(always)]
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut this = self.project();
        match this.state.as_mut().project() {
            ThenStateProj::Incomplete { future, .. } => {
                let output = match future.poll(cx) {
                    Poll::Ready(value) => value,
                    Poll::Pending => return Poll::Pending,
                };

                match this.state.as_mut().project_replace(ThenState::Temp) {
                    ThenStateReplace::Incomplete { f, .. } => {
                        let effective = f(output).into_future();

                        this.state
                            .as_mut()
                            .project_replace(ThenState::Completed { effective });

                        match this.state.project() {
                            ThenStateProj::Completed { effective } => effective.poll(cx),
                            _ => unreachable!(),
                        }
                    }
                    _ => unreachable!(),
                }
            }
            ThenStateProj::Completed { effective } => effective.poll(cx),
            ThenStateProj::Temp => unreachable!(),
        }
    }
}

impl<'c, 'lt: 'c, Fut0: Ss + 'lt, V0: Ss + 'lt, F0: Ss + 'c> Effective<'c>
    for Then<'lt, Fut0, F0, V0>
where
    F0: FnOnce(Fut0::Output) -> V0,
    V0: Effective<'lt>,
    Fut0: Future,
    Fut0::Output: Ss + 'lt,
{
    fn into_erased<B>(self) -> ErasedEffective<'c, Self::Output, Self::Effect, B> {
        BoxOrReady::Boxed(Box::pin(self))
    }

    type Effect = Async;

    type Output = V0::Output;

    type IntoFuture = Self;

    fn into_future(self) -> Self::IntoFuture {
        self
    }

    type Loop<'ctx: 'a, 'a, T: Ss + 'a, F: Ss + 'a>
        = BoxOrReady<'a, (V0::Output, T)>
    where
        F: for<'b> FnMut(&'b mut Self::Output) -> ErasedEffective<'b, ControlFlow<T>, Self::Effect>,
        'c: 'a;

    fn r#loop<'ctx: 'a, 'a, T: Ss + 'a, F: Ss + 'a>(self, mut cb: F) -> Self::Loop<'ctx, 'a, T, F>
    where
        F: for<'b> FnMut(&'b mut Self::Output) -> ErasedEffective<'b, ControlFlow<T>, Self::Effect>,
        'c: 'a,
    {
        BoxOrReady::Boxed(Box::pin(async move {
            let mut this = self.into_future().await;

            loop {
                if let ControlFlow::Break(value) = cb(&mut this).into_future().await {
                    return (this, value);
                }
            }
        }))
    }

    type Map<'a, T: Ss + 'a, F: Ss + 'a>
        = Map<Self, F>
    where
        F: FnOnce(Self::Output) -> T,
        'c: 'a;

    fn map<'a, T: Ss + 'a, F: Ss + 'a>(self, cb: F) -> Self::Map<'a, T, F>
    where
        F: FnOnce(Self::Output) -> T,
        'c: 'a,
    {
        Map::new(self, cb)
    }

    type Then<'a, T: Ss + 'a, V: Ss + 'a, F: Ss + 'a>
        = Then<'a, Self, F, V>
    where
        F: FnOnce(Self::Output) -> V,
        V: Effective<'a, Output = T, Effect = Self::Effect>,
        'c: 'a;

    fn then<'a, T: Ss + 'a, V: Ss + 'a, F: Ss + 'a>(self, cb: F) -> Self::Then<'a, T, V, F>
    where
        F: FnOnce(Self::Output) -> V,
        V: Effective<'a, Output = T, Effect = Self::Effect>,
        'c: 'a,
    {
        Then::new(self, cb)
    }

    type AsCtx<'ctx: 'a, 'a, T: Ss + 'a, F: Ss + 'a>
        = BoxOrReady<'a, (Self::Output, T)>
    where
        F: for<'b> FnOnce(&'b mut Self::Output) -> ErasedEffective<'b, T, Self::Effect>,
        'c: 'a;

    fn as_ctx<'ctx: 'a, 'a, T: Ss + 'a, F: Ss + 'a>(self, cb: F) -> Self::AsCtx<'ctx, 'a, T, F>
    where
        F: for<'b> FnOnce(&'b mut Self::Output) -> ErasedEffective<'b, T, Self::Effect>,
        'c: 'a,
    {
        BoxOrReady::Boxed(Box::pin(async {
            let mut this = self.await;

            let result = cb(&mut this).into_future().await;

            (this, result)
        }))
    }
}