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
#![allow(unsafe_code)]
use core::{
future::Future,
pin::Pin,
task::{Context, RawWaker, RawWakerVTable, Waker},
};
use crate::exec::Exec;
pub type Task<'a, T> = Pin<&'a mut (dyn Future<Output = T> + Unpin)>;
#[macro_export]
macro_rules! join {
($($future:ident),* $(,)?) => {{
use core::{task::{Poll, Context}, mem::MaybeUninit, pin::Pin, future::Future};
struct MaybeFuture<'a, T, F: Future<Output = T>>(Option<Pin<&'a mut F>>);
impl <T, F: Future<Output = T>> Future for MaybeFuture<'_, T, F> {
type Output = T;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
let mut this = self.as_mut();
if let Some(future) = this.0.as_mut() {
match future.as_mut().poll(cx) {
Poll::Ready(t) => {
this.0 = None;
Poll::Ready(t)
},
a => a,
}
} else {
Poll::Pending
}
}
}
let mut count = 0usize;
$(
let mut $future = MaybeFuture(Some(Pin::new(&mut $future)));
let mut $future = (Pin::new(&mut $future), MaybeUninit::uninit());
count += 1;
)*
for _ in 0..count {
struct __Pasts_Selector<'a, T> {
closure: &'a mut dyn FnMut(&mut Context<'_>) -> Poll<T>,
}
impl<'a, T> Future for __Pasts_Selector<'a, T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
(self.get_mut().closure)(cx)
}
}
__Pasts_Selector { closure: &mut |__pasts_cx: &mut Context<'_>| {
$(
match $future.0.as_mut().poll(__pasts_cx) {
Poll::Ready(pattern) => {
$future.1 = MaybeUninit::new(pattern);
return Poll::Ready(());
}
Poll::Pending => {}
}
)*
Poll::Pending
} }.await
}
unsafe {
($($future.1.assume_init()),*)
}
}};
}
#[macro_export]
macro_rules! task {
($(let $x:ident = $y:expr);* $(;)?) => { $(
let mut $x = $y;
#[allow(unused_mut, unused_qualifications)]
let mut $x = unsafe {
core::pin::Pin::<&mut dyn core::future::Future<Output = _>>
::new_unchecked(&mut $x)
};
)* };
}
#[inline]
pub(super) fn waker<F, T>(exec: &Exec, f: F) -> T
where
F: FnOnce(&mut Context<'_>) -> T,
{
let exec: *const Exec = exec;
const RWVT: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);
#[inline]
unsafe fn clone(data: *const ()) -> RawWaker {
RawWaker::new(data, &RWVT)
}
#[inline]
unsafe fn wake(data: *const ()) {
let exec: *const Exec = data.cast();
(*exec).wake();
}
#[inline]
unsafe fn drop(_: *const ()) {}
let waker = unsafe { Waker::from_raw(RawWaker::new(exec.cast(), &RWVT)) };
f(&mut Context::from_waker(&waker))
}
#[cfg(any(target_arch = "wasm32", not(feature = "std")))]
static mut EXEC: core::mem::MaybeUninit<Exec> =
core::mem::MaybeUninit::<Exec>::uninit();
#[cfg(any(target_arch = "wasm32", not(feature = "std")))]
pub(crate) fn exec() -> &'static mut Exec {
unsafe {
EXEC = core::mem::MaybeUninit::new(Exec::new());
&mut *EXEC.as_mut_ptr()
}
}