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
185
186
use futures_core::future::Future;
use futures_core::task::{self, Poll};
use futures_channel::oneshot::{channel, Sender, Receiver};
use futures_util::future::FutureExt;
use futures_util::task::ContextExt;
use std::thread;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::AtomicBool;
use std::mem::PinMut;
use std::boxed::Box;
use std::marker::Unpin;
#[derive(Debug)]
pub struct Spawn<F> {
future: Option<F>
}
impl<F> Spawn<F> {
unsafe_unpinned!(future: Option<F>);
}
pub fn spawn<F>(future: F) -> Spawn<F>
where F: Future<Output = ()> + 'static + Send
{
Spawn { future: Some(future) }
}
impl<F: Future<Output = ()> + Send + 'static> Future for Spawn<F> {
type Output = ();
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<()> {
cx.spawn(self.future().take().unwrap());
Poll::Ready(())
}
}
#[derive(Debug)]
pub struct SpawnWithHandle<F> {
future: Option<F>
}
impl<F> SpawnWithHandle<F> {
unsafe_unpinned!(future: Option<F>);
}
pub fn spawn_with_handle<F>(f: F) -> SpawnWithHandle<F>
where F: Future + 'static + Send, F::Output: Send
{
SpawnWithHandle { future: Some(f) }
}
impl<F> Future for SpawnWithHandle<F>
where F: Future + Send + 'static,
F::Output: Send,
{
type Output = JoinHandle<F::Output>;
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
let (tx, rx) = channel();
let keep_running_flag = Arc::new(AtomicBool::new(false));
let sender = MySender {
future: AssertUnwindSafe(self.future().take().unwrap()).catch_unwind(),
tx: Some(tx),
keep_running_flag: keep_running_flag.clone(),
};
cx.spawn(sender);
Poll::Ready(JoinHandle {
inner: rx ,
keep_running_flag: keep_running_flag.clone()
})
}
}
struct MySender<F, T> {
future: F,
tx: Option<Sender<T>>,
keep_running_flag: Arc<AtomicBool>,
}
impl<F: Unpin, T> Unpin for MySender<F, T> {}
impl<F: Future, T> MySender<F, T> {
unsafe_pinned!(future: F);
unsafe_unpinned!(tx: Option<Sender<T>>);
unsafe_unpinned!(keep_running_flag: Arc<AtomicBool>);
}
#[must_use]
#[derive(Debug)]
pub struct JoinHandle<T> {
inner: Receiver<thread::Result<T>>,
keep_running_flag: Arc<AtomicBool>,
}
impl<T> JoinHandle<T> {
pub fn forget(self) {
self.keep_running_flag.store(true, Ordering::SeqCst);
}
}
impl<T: Send + 'static> Future for JoinHandle<T> {
type Output = T;
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<T> {
match self.inner.poll_unpin(cx) {
Poll::Ready(Ok(Ok(output))) => Poll::Ready(output),
Poll::Ready(Ok(Err(e))) => panic::resume_unwind(e),
Poll::Ready(Err(e)) => panic::resume_unwind(Box::new(e)),
Poll::Pending => Poll::Pending,
}
}
}
impl<F: Future> Future for MySender<F, F::Output> {
type Output = ();
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<()> {
if let Poll::Ready(_) = self.tx().as_mut().unwrap().poll_cancel(cx) {
if !self.keep_running_flag().load(Ordering::SeqCst) {
return Poll::Ready(())
}
}
let output = match self.future().poll(cx) {
Poll::Ready(output) => output,
Poll::Pending => return Poll::Pending,
};
drop(self.tx().take().unwrap().send(output));
Poll::Ready(())
}
}