Skip to main content

Executor

Trait Executor 

Source
pub trait Executor:
    Clone
    + Send
    + Sync
    + 'static {
    // Required method
    fn execute<Fut>(&self, fut: Fut) -> JoinHandle<<Fut as Future>::Output> ⓘ
       where Fut: Future + Send + 'static,
             <Fut as Future>::Output: Send + 'static;
}
Expand description

A Tokio executor used by serve to spawn connection tasks, graceful shutdown tasks, and hyper’s internal tasks (e.g. HTTP/2 connection management).

The default executor is TokioExecutor, which simply calls to [tokio::spawn]. A custom implementation can be provided to wrap spawned tasks, e.g. to add tracing or telemetry.

Spawned futures rely on Tokio primitives internally, so the executor must run them within a Tokio runtime context (e.g. via [tokio::spawn]).

§Example

An executor that wraps every spawned task in a [tracing] span.

use std::future::Future;
use axum::serve::Executor;
use tokio::task::JoinHandle;
use tracing::Instrument;

#[derive(Clone)]
struct InstrumentedExecutor;

impl Executor for InstrumentedExecutor {
    fn execute<Fut>(&self, fut: Fut) -> JoinHandle<Fut::Output>
    where
        Fut: Future + Send + 'static,
        Fut::Output: Send + 'static,
    {
        let span = tracing::info_span!("axum.serve.task");
        tokio::spawn(fut.instrument(span))
    }
}

If your executor is expensive to clone, wrap it in an Arc. A blanket implementation is provided for Arc<T> where T: Executor.

Required Methods§

Source

fn execute<Fut>(&self, fut: Fut) -> JoinHandle<<Fut as Future>::Output> ⓘ
where Fut: Future + Send + 'static, <Fut as Future>::Output: Send + 'static,

Execute a task.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> Executor for Arc<T>
where T: Executor,

Source§

fn execute<Fut>(&self, fut: Fut) -> JoinHandle<<Fut as Future>::Output> ⓘ
where Fut: Future + Send + 'static, <Fut as Future>::Output: Send + 'static,

Implementors§