2020-01-20 04:47:36 +01:00
|
|
|
//! Choose your preferred executor to power a runtime.
|
|
|
|
|
mod null;
|
|
|
|
|
|
2020-02-05 04:14:26 +01:00
|
|
|
#[cfg(all(not(target_arch = "wasm32"), feature = "thread-pool"))]
|
2020-01-20 04:47:36 +01:00
|
|
|
mod thread_pool;
|
|
|
|
|
|
2020-02-05 01:40:27 +01:00
|
|
|
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
|
2020-01-20 04:47:36 +01:00
|
|
|
mod tokio;
|
|
|
|
|
|
2020-02-05 01:40:27 +01:00
|
|
|
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
|
2020-01-20 04:47:36 +01:00
|
|
|
mod async_std;
|
|
|
|
|
|
2020-01-20 05:43:09 +01:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
|
mod wasm_bindgen;
|
|
|
|
|
|
2020-01-20 04:47:36 +01:00
|
|
|
pub use null::Null;
|
|
|
|
|
|
2020-02-05 04:14:26 +01:00
|
|
|
#[cfg(all(not(target_arch = "wasm32"), feature = "thread-pool"))]
|
2020-01-20 05:43:09 +01:00
|
|
|
pub use thread_pool::ThreadPool;
|
|
|
|
|
|
2020-02-05 01:40:27 +01:00
|
|
|
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
|
2020-01-20 04:47:36 +01:00
|
|
|
pub use self::tokio::Tokio;
|
|
|
|
|
|
2020-02-05 01:40:27 +01:00
|
|
|
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
|
2020-01-20 04:47:36 +01:00
|
|
|
pub use self::async_std::AsyncStd;
|
|
|
|
|
|
2020-01-20 05:43:09 +01:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
|
pub use wasm_bindgen::WasmBindgen;
|
|
|
|
|
|
2020-01-20 04:47:36 +01:00
|
|
|
use futures::Future;
|
|
|
|
|
|
2020-01-20 09:49:17 +01:00
|
|
|
/// A type that can run futures.
|
2020-01-20 04:47:36 +01:00
|
|
|
pub trait Executor: Sized {
|
2020-01-20 09:49:17 +01:00
|
|
|
/// Creates a new [`Executor`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Executor`]: trait.Executor.html
|
2020-01-20 04:47:36 +01:00
|
|
|
fn new() -> Result<Self, futures::io::Error>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
|
|
|
|
|
2020-01-20 09:49:17 +01:00
|
|
|
/// Spawns a future in the [`Executor`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Executor`]: trait.Executor.html
|
2020-02-05 04:14:26 +01:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-01-20 04:47:36 +01:00
|
|
|
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static);
|
|
|
|
|
|
2020-02-05 04:14:26 +01:00
|
|
|
/// Spawns a local future in the [`Executor`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Executor`]: trait.Executor.html
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
|
fn spawn(&self, future: impl Future<Output = ()> + 'static);
|
|
|
|
|
|
2020-01-20 09:49:17 +01:00
|
|
|
/// Runs the given closure inside the [`Executor`].
|
|
|
|
|
///
|
|
|
|
|
/// Some executors, like `tokio`, require some global state to be in place
|
|
|
|
|
/// before creating futures. This method can be leveraged to set up this
|
|
|
|
|
/// global state, call a function, restore the state, and obtain the result
|
|
|
|
|
/// of the call.
|
2020-04-01 00:24:43 +02:00
|
|
|
///
|
|
|
|
|
/// [`Executor`]: trait.Executor.html
|
2020-01-20 04:47:36 +01:00
|
|
|
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
|
|
|
|
|
f()
|
|
|
|
|
}
|
|
|
|
|
}
|