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
#[doc(no_inline)]
pub use {
futures::{sync::oneshot::SpawnHandle, Async, Future, Poll},
tokio::executor::{spawn, DefaultExecutor, Executor, Spawn, SpawnError},
tokio_threadpool::{blocking as poll_blocking, BlockingError},
};
#[inline]
pub fn spawn_with_handle<F>(future: F) -> SpawnHandle<F::Item, F::Error>
where
F: Future + Send + 'static,
F::Item: Send + 'static,
F::Error: Send + 'static,
{
futures::sync::oneshot::spawn(future, &DefaultExecutor::current())
}
pub fn blocking<T>(op: impl FnOnce() -> T) -> impl Future<Item = T, Error = BlockingError> {
let mut op = Some(op);
futures::future::poll_fn(move || {
poll_blocking(|| {
let op = op.take().expect("The future has already polled");
op()
})
})
}
#[inline]
pub fn spawn_fn<T>(op: impl FnOnce() -> T + Send + 'static) -> SpawnHandle<T, BlockingError>
where
T: Send + 'static,
{
spawn_with_handle(blocking(op))
}