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
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures::stream::FuturesUnordered;
use futures::{Future, Stream};
use futures::future::{AbortHandle, Abortable};
use pin_project::*;
use crate::spawner::*;
#[pin_project(PinnedDrop)]
pub struct Scope<'a, T, Sp: Spawner<T> + Blocker> {
done: bool,
len: usize,
remaining: usize,
#[pin]
futs: FuturesUnordered<Sp::SpawnHandle>,
abort_handles: Vec<AbortHandle>,
_marker: PhantomData<fn(&'a ()) -> &'a ()>,
_spawn_marker: PhantomData<Sp>,
}
impl<'a, T: Send + 'static, Sp: Spawner<T> + Blocker> Scope<'a, T, Sp> {
pub unsafe fn create() -> Self {
Scope {
done: false,
len: 0,
remaining: 0,
futs: FuturesUnordered::new(),
abort_handles: vec![],
_marker: PhantomData,
_spawn_marker: PhantomData,
}
}
pub fn spawn<F: Future<Output = T> + Send + 'a>(&mut self, f: F) {
let handle = Sp::spawn(unsafe {
std::mem::transmute::<_, Pin<Box<dyn Future<Output = T> + Send>>>(
Box::pin(f) as Pin<Box<dyn Future<Output = T>>>
)
});
self.futs.push(handle);
self.len += 1;
self.remaining += 1;
}
#[inline]
pub fn spawn_cancellable<F: Future<Output = T> + Send + 'a, Fu: FnOnce() -> T + Send + 'a>(
&mut self,
f: F,
default: Fu,
) {
let (h, reg) = AbortHandle::new_pair();
self.abort_handles.push(h);
let fut = Abortable::new(f, reg);
self.spawn(async {
fut.await
.unwrap_or_else(|_| default())
})
}
pub fn spawn_blocking<F: FnOnce() -> T + Send + 'a>(&mut self, f: F)
where
Sp: FuncSpawner<T, SpawnHandle = <Sp as Spawner<T>>::SpawnHandle>,
{
let handle = Sp::spawn_func(unsafe {
std::mem::transmute::<_, Box<dyn FnOnce() -> T + Send>>(
Box::new(f) as Box<dyn FnOnce() -> T + Send>
)
});
self.futs.push(handle);
self.len += 1;
self.remaining += 1;
}
}
impl<'a, T, Sp: Spawner<T> + Blocker> Scope<'a, T, Sp> {
#[inline]
pub fn cancel(&mut self) {
for h in self.abort_handles.drain(..) {
h.abort();
}
}
#[inline]
pub fn len(&self) -> usize {
self.len
}
#[inline]
pub fn remaining(&self) -> usize {
self.remaining
}
pub async fn collect(&mut self) -> Vec<Sp::FutureOutput> {
let mut proc_outputs = Vec::with_capacity(self.remaining);
use futures::StreamExt;
while let Some(item) = self.next().await {
proc_outputs.push(item);
}
proc_outputs
}
}
impl<'a, T, Sp: Spawner<T> + Blocker> Stream for Scope<'a, T, Sp> {
type Item = Sp::FutureOutput;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let this = self.project();
let poll = this.futs.poll_next(cx);
if let Poll::Ready(None) = poll {
*this.done = true;
} else if poll.is_ready() {
*this.remaining -= 1;
}
poll
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
}
}
#[pinned_drop]
impl<'a, T, Sp: Spawner<T> + Blocker> PinnedDrop for Scope<'a, T, Sp> {
fn drop(mut self: Pin<&mut Self>) {
if !self.done {
<Sp as Blocker>::block_on(async {
self.cancel();
self.collect().await;
});
}
}
}