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
use super::Runtime;
use futures_01::future::Future as Future01;
use futures_util::{compat::Future01CompatExt, FutureExt};
use std::future::Future;
use tokio_executor_01 as executor_01;
#[derive(Debug)]
#[cfg_attr(docsrs, doc(cfg(feature = "rt-current-thread")))]
pub struct TaskExecutor {
_p: (),
}
impl TaskExecutor {
pub fn current() -> TaskExecutor {
TaskExecutor { _p: () }
}
pub fn spawn_local(
&mut self,
future: impl Future01<Item = (), Error = ()> + 'static,
) -> Result<(), executor_01::SpawnError> {
self.spawn_local_std(future.compat().map(|_| ()))
}
pub fn spawn_local_std(
&mut self,
future: impl Future<Output = ()> + 'static,
) -> Result<(), executor_01::SpawnError> {
if let Some(idle) = Runtime::reserve_idle() {
tokio_02::task::spawn_local(idle.with(future));
Ok(())
} else {
Err(executor_01::SpawnError::shutdown())
}
}
pub fn spawn_handle<T: 'static, E: 'static>(
&mut self,
future: impl Future01<Item = T, Error = E> + 'static,
) -> tokio_02::task::JoinHandle<Result<T, E>> {
self.spawn_handle_std(future.compat())
}
pub fn spawn_handle_std<T: 'static>(
&mut self,
future: impl Future<Output = T> + 'static,
) -> tokio_02::task::JoinHandle<T> {
tokio_02::task::spawn_local(future)
}
}
impl executor_01::Executor for TaskExecutor {
fn spawn(
&mut self,
future: Box<dyn Future01<Item = (), Error = ()> + Send>,
) -> Result<(), executor_01::SpawnError> {
self.spawn_local(future)
}
fn status(&self) -> Result<(), executor_01::SpawnError> {
if Runtime::is_current() {
Ok(())
} else {
Err(executor_01::SpawnError::shutdown())
}
}
}
impl<F> executor_01::TypedExecutor<F> for TaskExecutor
where
F: Future01<Item = (), Error = ()> + 'static,
{
fn spawn(&mut self, future: F) -> Result<(), executor_01::SpawnError> {
self.spawn_local(Box::new(future))
}
fn status(&self) -> Result<(), executor_01::SpawnError> {
executor_01::Executor::status(self)
}
}