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
use crate::common::*;
use super::{get_global_runtime, BoxAny};
pub fn spawn<Fut>(fut: Fut) -> JoinHandle<Fut::Output>
where
Fut: 'static + Future + Send,
Fut::Output: 'static + Send,
{
let future = async move {
let output = get_global_runtime()
.spawn(
async move {
let output: BoxAny<'static> = Box::new(fut.await);
output
}
.boxed(),
)
.await;
let output =
BoxAny::<'static>::downcast::<Fut::Output>(output).expect("interal error: unable downcast Box");
*output
}
.boxed();
JoinHandle {
future,
_phantom: PhantomData,
}
}
pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R>
where
F: 'static + Send + FnOnce() -> R,
R: 'static + Send,
{
let future = async move {
let output = get_global_runtime()
.spawn_blocking({
let f: Box<dyn FnOnce() -> BoxAny<'static> + Send> = Box::new(move || {
let output: BoxAny<'static> = Box::new(f());
output
});
f
})
.await;
let output = BoxAny::<'static>::downcast::<R>(output).expect("interal error: unable downcast Box");
*output
}
.boxed();
JoinHandle {
future,
_phantom: PhantomData,
}
}
pub async fn sleep(dur: Duration) {
get_global_runtime().sleep(dur).await;
}
pub fn block_on<F>(future: F) -> F::Output
where
F: Future + Send,
F::Output: 'static + Send,
{
let output = get_global_runtime().block_on(
async move {
let output: BoxAny<'static> = Box::new(future.await);
output
}
.boxed(),
);
let output = BoxAny::<'static>::downcast::<F::Output>(output).expect("interal error: unable downcast Box");
*output
}
pub fn block_on_executor<F>(future: F) -> F::Output
where
F: 'static + Future + Send,
F::Output: 'static + Send,
{
let output = get_global_runtime().block_on(
async {
let output: BoxAny<'static> = Box::new(future.await);
output
}
.boxed(),
);
let output = BoxAny::<'static>::downcast::<F::Output>(output).expect("interal error: unable downcast Box");
*output
}
#[repr(transparent)]
#[pin_project]
pub struct JoinHandle<T> {
_phantom: PhantomData<T>,
#[pin]
future: BoxFuture<'static, T>,
}
impl<T> Future for JoinHandle<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().future.poll_unpin(cx)
}
}