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
use futures::stream::FuturesUnordered;
use futures::future::{LocalFutureObj, FutureObj};
use futures::StreamExt;
use std::future::{Future};
use std::task::{Poll, Context};
use crate::waker::{AlwaysWake, waker_ref};
use futures::task::{Spawn, SpawnError};
pub fn poll_fn<T, F: FnMut(&mut Context<'_>) -> T>(mut f: F) -> T {
let waker = waker_ref(&AlwaysWake::INSTANCE);
let mut cx = Context::from_waker(&waker);
f(&mut cx)
}
pub fn block_fn<T, F: FnMut(&mut Context<'_>) -> Poll<T>>(mut f: F) -> T {
let waker = waker_ref(&AlwaysWake::INSTANCE);
let mut cx = Context::from_waker(&waker);
loop {
if let Poll::Ready(t) = f(&mut cx) {
return t;
}
}
}
pub fn poll_on<F: Future>(f: F) -> Poll<F::Output> {
futures::pin_mut!(f);
poll_fn(|cx| f.as_mut().poll(cx))
}
pub fn block_on<F: Future>(f: F) -> F::Output {
futures::pin_mut!(f);
block_fn(|cx| f.as_mut().poll(cx))
}
#[derive(Debug)]
pub struct LocalPool<'a, Ret = ()> {
pool: FuturesUnordered<LocalFutureObj<'a, Ret>>,
rx: crossbeam::channel::Receiver<FutureObj<'a, Ret>>,
tx: crossbeam::channel::Sender<FutureObj<'a, Ret>>,
}
#[derive(Clone)]
pub struct Spawner<'a, Ret> {
tx: crossbeam::channel::Sender<FutureObj<'a, Ret>>,
}
impl<'a, Ret> Spawner<'a, Ret> {
pub fn spawn<F>(&self, f: F) -> Result<(), SpawnError>
where F: Into<FutureObj<'a, Ret>> {
self.tx.send(f.into()).map_err(|_| SpawnError::shutdown())
}
}
impl Spawn for Spawner<'static, ()> {
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
Self::spawn(self, future)
}
}
impl<'a, Ret> LocalPool<'a, Ret> {
pub fn new() -> Self {
let (tx, rx) = crossbeam::channel::unbounded();
Self { pool: FuturesUnordered::new(), rx, tx }
}
pub fn spawner(&self) -> Spawner<'a, Ret> {
Spawner {
tx: self.tx.clone()
}
}
pub fn spawn<F>(&mut self, f: F)
where F: Into<LocalFutureObj<'a, Ret>> {
self.pool.push(f.into())
}
pub fn run(&mut self) -> Vec<Ret> {
let mut results = Vec::new();
loop {
let ret = self.poll_once();
match ret {
Poll::Pending => break,
Poll::Ready(None) => break,
Poll::Ready(Some(r)) => { results.push(r); }
}
}
results
}
pub fn try_run_one(&mut self) -> Poll<Ret> {
let ret = self.poll_once();
match ret {
Poll::Ready(Some(ret)) => {
Poll::Ready(ret)
}
Poll::Ready(None) => {
Poll::Pending
}
Poll::Pending => {
Poll::Pending
}
}
}
pub fn poll_once(&mut self) -> Poll<Option<Ret>> {
poll_fn(|cx| {
while let Ok(fut) = self.rx.try_recv() {
self.pool.push(LocalFutureObj::from(fut))
}
self.pool.poll_next_unpin(cx)
})
}
}
impl<'a, Ret> Default for LocalPool<'a, Ret> {
fn default() -> Self {
Self::new()
}
}