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
use std::pin::Pin;
use std::task::{self, Poll};
use futures_core::ready;
use pin_project_lite::pin_project;
use crate::actor::Actor;
use crate::fut::{ActorFuture, IntoActorFuture};
pin_project! {
#[project = ThenProj]
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub enum Then<A, B, Fn> {
First {
#[pin]
fut1: A,
data: Option<Fn>,
},
Second {
#[pin]
fut2: B
},
Empty,
}
}
pub(super) fn new<A, B, Fn, Act>(future: A, f: Fn) -> Then<A, B, Fn>
where
A: ActorFuture<Act>,
B: IntoActorFuture<Act>,
Act: Actor,
{
Then::First {
fut1: future,
data: Some(f),
}
}
impl<A, B, Fn, Act> ActorFuture<Act> for Then<A, B::Future, Fn>
where
A: ActorFuture<Act>,
B: IntoActorFuture<Act>,
Fn: FnOnce(A::Output, &mut Act, &mut Act::Context) -> B,
Act: Actor,
{
type Output = B::Output;
fn poll(
mut self: Pin<&mut Self>,
act: &mut Act,
ctx: &mut Act::Context,
task: &mut task::Context<'_>,
) -> Poll<B::Output> {
match self.as_mut().project() {
ThenProj::First { fut1, data } => {
let output = ready!(fut1.poll(act, ctx, task));
let data = data.take().unwrap();
let fut2 = data(output, act, ctx).into_future();
self.set(Then::Second { fut2 });
self.poll(act, ctx, task)
}
ThenProj::Second { fut2 } => {
let res = ready!(fut2.poll(act, ctx, task));
self.set(Then::Empty);
Poll::Ready(res)
}
ThenProj::Empty => panic!("ActorFuture polled after finish"),
}
}
}