Trait Executor

pub trait Executor: Sized {
    // Required methods
    fn new() -> Result<Self, Error>
       where Self: Sized;
    fn spawn(&self, future: impl Future<Output = ()> + MaybeSend + 'static);
    fn block_on<T>(&self, future: impl Future<Output = T>) -> T;

    // Provided method
    fn enter<R>(&self, f: impl FnOnce() -> R) -> R { ... }
}
Expand description

A type that can run futures.

Required Methods§

fn new() -> Result<Self, Error>
where Self: Sized,

Creates a new Executor.

fn spawn(&self, future: impl Future<Output = ()> + MaybeSend + 'static)

Spawns a future in the Executor.

fn block_on<T>(&self, future: impl Future<Output = T>) -> T

Runs a future to completion in the current thread within the Executor.

Provided Methods§

fn enter<R>(&self, f: impl FnOnce() -> R) -> R

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

§

impl Executor for Runtime

§

fn new() -> Result<Runtime, Error>

§

fn spawn(&self, future: impl Future<Output = ()> + Send + 'static)

§

fn enter<R>(&self, f: impl FnOnce() -> R) -> R

§

fn block_on<T>(&self, future: impl Future<Output = T>) -> T

§

impl Executor for ThreadPool

§

fn new() -> Result<ThreadPool, Error>

§

fn spawn(&self, future: impl Future<Output = ()> + Send + 'static)

§

fn block_on<T>(&self, future: impl Future<Output = T>) -> T

Implementors§

§

impl Executor for Executor

§

impl Executor for Executor