async_rt/
global.rs

1use crate::{Executor, JoinHandle};
2use std::future::Future;
3
4/// Executor that switches between [`TokioExecutor`](crate::rt::tokio::TokioExecutor), [`ThreadpoolExecutor`](crate::rt::threadpool::ThreadpoolExecutor) and [`WasmExecutor`](crate::rt::wasm::WasmExecutor) at compile time.
5/// * If the target is non-wasm32 and the "tokio" feature is enabled, [`tokio`] would be used.
6/// * if the target is non-wasm32 and the "threadpool" feature is enabled with [`tokio`] feature disabled, [`futures`] [`ThreadPool`](futures::executor::ThreadPool) will be used.
7/// * If the target is wasm32, [`wasm-bindgen-futures`] would be used.
8#[derive(Clone, Copy, Debug, PartialOrd, PartialEq, Eq)]
9pub struct GlobalExecutor;
10
11impl Executor for GlobalExecutor {
12    #[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
13    fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
14    where
15        F: Future + Send + 'static,
16        F::Output: Send + 'static,
17    {
18        crate::rt::tokio::TokioExecutor.spawn(future)
19    }
20
21    #[cfg(all(feature = "threadpool", not(feature = "tokio")))]
22    fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
23    where
24        F: Future + Send + 'static,
25        F::Output: Send + 'static,
26    {
27        crate::rt::threadpool::ThreadPoolExecutor.spawn(future)
28    }
29
30    #[cfg(target_arch = "wasm32")]
31    fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
32    where
33        F: Future + Send + 'static,
34        F::Output: Send + 'static,
35    {
36        crate::rt::wasm::WasmExecutor.spawn(future)
37    }
38
39    #[cfg(all(
40        not(feature = "threadpool"),
41        not(feature = "tokio"),
42        not(target_arch = "wasm32")
43    ))]
44    fn spawn<F>(&self, _: F) -> JoinHandle<F::Output>
45    where
46        F: Future + Send + 'static,
47        F::Output: Send + 'static,
48    {
49        unreachable!()
50    }
51}