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
use crate::{prelude::*, Notifier};
pub trait Stateful<S, T>: Unpin {
fn state(&mut self) -> &mut S;
fn poll(&mut self, _: &mut Exec<'_>) -> Poll<Poll<T>> {
Pending
}
}
#[derive(Debug)]
pub struct Never<'a, S>(&'a mut S);
impl<S, T> Stateful<S, T> for Never<'_, S> {
fn state(&mut self) -> &mut S {
self.0
}
}
#[doc = include_str!("doc/slices.rs")]
#[doc = include_str!("doc/tasks.rs")]
#[derive(Debug)]
pub struct Join<S: Unpin, T, F: Stateful<S, T>> {
other: F,
_phantom: core::marker::PhantomData<(S, T)>,
}
impl<'a, S: Unpin, T> Join<S, T, Never<'a, S>> {
pub fn new(state: &'a mut S) -> Self {
let other = Never(state);
let _phantom = core::marker::PhantomData;
Join { other, _phantom }
}
}
impl<S: Unpin, T, F: Stateful<S, T>> Join<S, T, F> {
pub fn on<N: Notifier + Unpin + ?Sized>(
self,
noti: impl for<'a> FnMut(&'a mut S) -> &'a mut N + Unpin,
then: fn(&mut S, N::Event) -> Poll<T>,
) -> Join<S, T, impl Stateful<S, T>> {
let other = self.other;
let _phantom = core::marker::PhantomData;
let other = Joiner { other, noti, then };
Join { other, _phantom }
}
}
impl<S: Unpin, T: Unpin, F: Stateful<S, T>> Future for Join<S, T, F> {
type Output = T;
#[inline]
fn poll(mut self: Pin<&mut Self>, e: &mut Exec<'_>) -> Poll<T> {
while let Ready(output) = Pin::new(&mut self.other).poll(e) {
if let Ready(output) = output {
return Ready(output);
}
}
Pending
}
}
struct Joiner<S, T, E, F: Stateful<S, T>, P> {
other: F,
noti: P,
then: fn(&mut S, E) -> Poll<T>,
}
impl<S, T, E, F, N, P> Stateful<S, T> for Joiner<S, T, E, F, P>
where
F: Stateful<S, T>,
N: Notifier<Event = E> + Unpin + ?Sized,
P: for<'a> FnMut(&'a mut S) -> &'a mut N + Unpin,
{
#[inline]
fn state(&mut self) -> &mut S {
self.other.state()
}
#[inline]
fn poll(&mut self, e: &mut Exec<'_>) -> Poll<Poll<T>> {
let state = self.other.state();
let poll = Pin::new((self.noti)(state)).poll_next(e);
if let Ready(out) = poll.map(|x| (self.then)(state, x)) {
Ready(out)
} else {
self.other.poll(e)
}
}
}