async_rt/
global.rs

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
use crate::{Executor, JoinHandle};
use std::future::Future;

/// Executor that switch between [`TokioExecutor`](crate::rt::tokio::TokioExecutor), [`ThreadpoolExecutor`](crate::rt::threadpool::ThreadpoolExecutor) and [`WasmExecutor`](crate::rt::wasm::WasmExecutor) at compile time.
/// * If the target is non-wasm32 and "tokio" feature is enabled, [`tokio`] would be used.
/// * if the target is non-wasm32 and "threadpool" feature is enabled with [`tokio`] feature disabled, [`futures`] [`ThreadPool`](futures::executor::ThreadPool) will be used.
/// * If the target is wasm32, [`wasm-bindgen-futures`] would be used.
#[derive(Clone, Copy, Debug, PartialOrd, PartialEq, Eq)]
pub struct GlobalExecutor;

impl Executor for GlobalExecutor {
    fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        #[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
        {
            crate::rt::tokio::TokioExecutor.spawn(future)
        }
        #[cfg(all(
            feature = "threadpool",
            not(feature = "tokio"),
            not(target_arch = "wasm32")
        ))]
        {
            crate::rt::threadpool::ThreadPoolExecutor.spawn(future)
        }
        #[cfg(target_arch = "wasm32")]
        {
            crate::rt::wasm::WasmExecutor.spawn(future)
        }

        #[cfg(all(
            not(feature = "threadpool"),
            not(feature = "tokio"),
            not(target_arch = "wasm32")
        ))]
        {
            _ = future;
            unreachable!()
        }
    }
}