2020-01-20 04:47:36 +01:00
|
|
|
//! Choose your preferred executor to power a runtime.
|
2022-01-28 17:35:47 +07:00
|
|
|
use crate::MaybeSend;
|
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`].
|
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`].
|
2022-01-28 17:35:47 +07:00
|
|
|
fn spawn(&self, future: impl Future<Output = ()> + MaybeSend + 'static);
|
2020-02-05 04:14:26 +01:00
|
|
|
|
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-01-20 04:47:36 +01:00
|
|
|
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
|
|
|
|
|
f()
|
|
|
|
|
}
|
|
|
|
|
}
|