Skip to main content

async_rt/global/
executor.rs

1use crate::{Executor, ExecutorBlockOn, ExecutorBlocking, ExecutorTimeout, JoinHandle};
2
3/// Executor that selects an available runtime backend at compile time.
4///
5/// * On non-Wasm targets with the `tokio`, `smol`, or `compio` feature enabled, it uses
6///   `TokioExecutor`, `SmolExecutor`, or `CompioExecutor`.
7/// * On non-Wasm targets with the `threadpool` feature enabled and the `tokio`, `smol` or `compio`
8///   feature disabled, it uses `ThreadPoolExecutor`.
9/// * On non-Wasm targets with only the `lite` runtime feature enabled, it uses
10///   `LiteExecutor`.
11/// * On Wasm targets, it uses `WasmExecutor`, backed by
12///   `wasm-bindgen-futures`.
13#[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
14pub type DefaultExecutor = crate::rt::tokio::TokioExecutor;
15
16#[cfg(all(
17    feature = "compio",
18    not(any(feature = "tokio", feature = "smol", target_arch = "wasm32"))
19))]
20pub type DefaultExecutor = crate::rt::compio::CompioExecutor;
21
22#[cfg(all(feature = "smol", not(any(feature = "tokio", target_arch = "wasm32"))))]
23pub type DefaultExecutor = crate::rt::smol::SmolExecutor;
24
25#[cfg(all(
26    feature = "threadpool",
27    not(any(
28        feature = "tokio",
29        feature = "smol",
30        feature = "compio",
31        target_arch = "wasm32"
32    ))
33))]
34pub type DefaultExecutor = crate::rt::threadpool::ThreadPoolExecutor;
35
36#[cfg(all(
37    feature = "lite",
38    not(any(
39        feature = "tokio",
40        feature = "smol",
41        feature = "compio",
42        feature = "threadpool"
43    )),
44    not(target_arch = "wasm32")
45))]
46pub type DefaultExecutor = crate::rt::lite::LiteExecutor;
47
48#[cfg(target_arch = "wasm32")]
49pub type DefaultExecutor = crate::rt::wasm::WasmExecutor;
50
51#[cfg(all(
52    not(feature = "threadpool"),
53    not(feature = "tokio"),
54    not(feature = "compio"),
55    not(feature = "smol"),
56    not(feature = "lite"),
57    not(target_arch = "wasm32")
58))]
59pub type DefaultExecutor = crate::rt::dummy::DummyExecutor;
60
61/// Built-in executor types.
62#[derive(Clone, Copy, Debug, PartialEq, Eq)]
63pub enum BuiltinExecutor {
64    #[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
65    Tokio,
66    #[cfg(all(feature = "smol", not(target_arch = "wasm32")))]
67    Smol,
68    #[cfg(all(feature = "compio", not(target_arch = "wasm32")))]
69    Compio,
70    #[cfg(all(feature = "threadpool", not(target_arch = "wasm32")))]
71    ThreadPool,
72    #[cfg(target_arch = "wasm32")]
73    Wasm,
74    #[cfg(all(feature = "lite", not(target_arch = "wasm32")))]
75    Lite,
76    Dummy,
77}
78
79impl BuiltinExecutor {
80    pub(crate) fn is_exclusive(&self) -> bool {
81        match self {
82            #[cfg(all(feature = "lite", not(target_arch = "wasm32")))]
83            Self::Lite => true,
84            _ => false,
85        }
86    }
87}
88
89impl Default for BuiltinExecutor {
90    fn default() -> Self {
91        #[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
92        return Self::Tokio;
93
94        #[cfg(all(feature = "smol", not(any(feature = "tokio", target_arch = "wasm32"))))]
95        return Self::Smol;
96
97        #[cfg(all(
98            feature = "compio",
99            not(any(feature = "tokio", feature = "smol", target_arch = "wasm32"))
100        ))]
101        return Self::Compio;
102
103        #[cfg(all(
104            feature = "threadpool",
105            not(any(
106                feature = "tokio",
107                feature = "smol",
108                feature = "compio",
109                target_arch = "wasm32"
110            ))
111        ))]
112        return Self::ThreadPool;
113
114        #[cfg(target_arch = "wasm32")]
115        return Self::Wasm;
116
117        #[cfg(all(
118            feature = "lite",
119            not(any(
120                feature = "threadpool",
121                feature = "tokio",
122                feature = "compio",
123                feature = "smol"
124            )),
125            not(target_arch = "wasm32")
126        ))]
127        return Self::Lite;
128
129        #[cfg(all(
130            not(feature = "threadpool"),
131            not(feature = "tokio"),
132            not(feature = "compio"),
133            not(feature = "smol"),
134            not(feature = "lite"),
135            not(target_arch = "wasm32")
136        ))]
137        return Self::Dummy;
138    }
139}
140
141impl Executor for BuiltinExecutor {
142    fn runtime_type(&self) -> Option<&'static str> {
143        match self {
144            #[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
145            BuiltinExecutor::Tokio => Executor::runtime_type(&crate::rt::tokio::TokioExecutor),
146            #[cfg(all(feature = "smol", not(target_arch = "wasm32")))]
147            BuiltinExecutor::Smol => Executor::runtime_type(&crate::rt::smol::SmolExecutor),
148            #[cfg(all(feature = "compio", not(target_arch = "wasm32")))]
149            BuiltinExecutor::Compio => Executor::runtime_type(&crate::rt::compio::CompioExecutor),
150            #[cfg(all(feature = "threadpool", not(target_arch = "wasm32")))]
151            BuiltinExecutor::ThreadPool => {
152                Executor::runtime_type(&crate::rt::threadpool::ThreadPoolExecutor)
153            }
154            #[cfg(target_arch = "wasm32")]
155            BuiltinExecutor::Wasm => Executor::runtime_type(&crate::rt::wasm::WasmExecutor),
156            #[cfg(all(feature = "lite", not(target_arch = "wasm32")))]
157            BuiltinExecutor::Lite => Executor::runtime_type(&crate::rt::lite::LiteExecutor),
158            BuiltinExecutor::Dummy => Executor::runtime_type(&crate::rt::dummy::DummyExecutor),
159        }
160    }
161
162    fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
163    where
164        F: Future + Send + 'static,
165        F::Output: Send + 'static,
166    {
167        match self {
168            #[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
169            BuiltinExecutor::Tokio => Executor::spawn(&crate::rt::tokio::TokioExecutor, future),
170            #[cfg(all(feature = "smol", not(target_arch = "wasm32")))]
171            BuiltinExecutor::Smol => Executor::spawn(&crate::rt::smol::SmolExecutor, future),
172            #[cfg(all(feature = "compio", not(target_arch = "wasm32")))]
173            BuiltinExecutor::Compio => Executor::spawn(&crate::rt::compio::CompioExecutor, future),
174            #[cfg(all(feature = "threadpool", not(target_arch = "wasm32")))]
175            BuiltinExecutor::ThreadPool => {
176                Executor::spawn(&crate::rt::threadpool::ThreadPoolExecutor, future)
177            }
178            #[cfg(target_arch = "wasm32")]
179            BuiltinExecutor::Wasm => Executor::spawn(&crate::rt::wasm::WasmExecutor, future),
180            #[cfg(all(feature = "lite", not(target_arch = "wasm32")))]
181            BuiltinExecutor::Lite => Executor::spawn(&crate::rt::lite::LiteExecutor, future),
182            BuiltinExecutor::Dummy => Executor::spawn(&crate::rt::dummy::DummyExecutor, future),
183        }
184    }
185}
186
187impl ExecutorBlocking for BuiltinExecutor {
188    fn spawn_blocking<F, R>(&self, f: F) -> JoinHandle<R>
189    where
190        F: FnOnce() -> R + Send + 'static,
191        R: Send + 'static,
192    {
193        match self {
194            #[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
195            BuiltinExecutor::Tokio => {
196                ExecutorBlocking::spawn_blocking(&crate::rt::tokio::TokioExecutor, f)
197            }
198            #[cfg(all(feature = "smol", not(target_arch = "wasm32")))]
199            BuiltinExecutor::Smol => {
200                ExecutorBlocking::spawn_blocking(&crate::rt::smol::SmolExecutor, f)
201            }
202            #[cfg(all(feature = "compio", not(target_arch = "wasm32")))]
203            BuiltinExecutor::Compio => {
204                ExecutorBlocking::spawn_blocking(&crate::rt::compio::CompioExecutor, f)
205            }
206            #[cfg(all(feature = "threadpool", not(target_arch = "wasm32")))]
207            BuiltinExecutor::ThreadPool => {
208                ExecutorBlocking::spawn_blocking(&crate::rt::threadpool::ThreadPoolExecutor, f)
209            }
210            #[cfg(target_arch = "wasm32")]
211            BuiltinExecutor::Wasm => {
212                ExecutorBlocking::spawn_blocking(&crate::rt::wasm::WasmExecutor, f)
213            }
214            #[cfg(all(feature = "lite", not(target_arch = "wasm32")))]
215            BuiltinExecutor::Lite => {
216                ExecutorBlocking::spawn_blocking(&crate::rt::lite::LiteExecutor, f)
217            }
218            BuiltinExecutor::Dummy => {
219                ExecutorBlocking::spawn_blocking(&crate::rt::dummy::DummyExecutor, f)
220            }
221        }
222    }
223}
224
225impl ExecutorTimeout for BuiltinExecutor {}
226
227impl ExecutorBlockOn for BuiltinExecutor {
228    fn block_on<F: Future>(&self, future: F) -> F::Output {
229        match self {
230            #[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
231            BuiltinExecutor::Tokio => {
232                ExecutorBlockOn::block_on(&crate::rt::tokio::TokioExecutor, future)
233            }
234            #[cfg(all(feature = "smol", not(target_arch = "wasm32")))]
235            BuiltinExecutor::Smol => {
236                ExecutorBlockOn::block_on(&crate::rt::smol::SmolExecutor, future)
237            }
238            #[cfg(all(feature = "compio", not(target_arch = "wasm32")))]
239            BuiltinExecutor::Compio => {
240                ExecutorBlockOn::block_on(&crate::rt::compio::CompioExecutor, future)
241            }
242            #[cfg(all(feature = "threadpool", not(target_arch = "wasm32")))]
243            BuiltinExecutor::ThreadPool => {
244                ExecutorBlockOn::block_on(&crate::rt::threadpool::ThreadPoolExecutor, future)
245            }
246            #[cfg(target_arch = "wasm32")]
247            BuiltinExecutor::Wasm => {
248                ExecutorBlockOn::block_on(&crate::rt::wasm::WasmExecutor, future)
249            }
250            #[cfg(all(feature = "lite", not(target_arch = "wasm32")))]
251            BuiltinExecutor::Lite => {
252                ExecutorBlockOn::block_on(&crate::rt::lite::LiteExecutor, future)
253            }
254            BuiltinExecutor::Dummy => {
255                ExecutorBlockOn::block_on(&crate::rt::dummy::DummyExecutor, future)
256            }
257        }
258    }
259}