Crate executor_core

Crate executor_core 

Source
Expand description

§executor-core

A flexible task executor abstraction layer for Rust async runtimes.

This crate provides unified traits and type-erased wrappers for different async executors, allowing you to write code that’s agnostic to the underlying executor implementation.

§Overview

The crate is built around two main traits:

  • Executor: For spawning Send + 'static futures
  • LocalExecutor: For spawning 'static futures (not necessarily Send)

Both traits produce tasks that implement the Task trait, providing:

§Quick Start

use executor_core::{Executor, init_global_executor, spawn};
use executor_core::tokio::TokioExecutor;

#[tokio::main]
async fn main() {
    // Initialize the global executor
    init_global_executor(TokioExecutor::new());

    // Spawn a task
    let task = spawn(async {
        println!("Hello from spawned task!");
        42
    });

    // The task can be awaited to get the result
    let result = task.await;
    println!("Task result: {}", result);
}

§Features

  • Zero-cost Executor Abstraction: Unified Executor and LocalExecutor traits using Generic Associated Types (GAT) to prevent unnecessary heap allocation and dynamic dispatch
  • Type Erasure: AnyExecutor and AnyLocalExecutor for runtime flexibility
  • Multiple Runtime Support: Tokio, async-executor, Web/WASM
  • Task Management: Rich task API with cancellation and error handling
  • No-std Compatible: Core functionality works in no-std environments
  • Panic Safety: Proper panic handling and propagation

§Lifetime Constraints

The current API requires 'static lifetimes for both futures and their outputs. This constraint comes from the underlying async runtimes and ensures memory safety when tasks may outlive their spawning scope. While this limits flexibility, it matches the constraints of most async runtime implementations in Rust.

Re-exports§

pub use async_executor::AsyncTask;async-executor
pub use tokio::TokioExecutor;tokio
pub use tokio::TokioLocalTask;tokio
pub use tokio::TokioTask;tokio
pub use web::WebExecutor;web
pub use web::WebTask;web

Modules§

async_executorasync-executor
Integration with the async-executor crate.
tokiotokio
Integration with the Tokio async runtime.
webweb
Integration for Web/WASM environments.

Structs§

AnyExecutor
A type-erased Executor that can hold any executor implementation.
AnyExecutorTask
Task type returned by AnyExecutor.
AnyLocalExecutor
A type-erased LocalExecutor that can hold any local executor implementation.
AnyLocalExecutorTask
Task type returned by AnyLocalExecutor.
CancelFuture
Future returned by Task::cancel().
DefaultExecutor
A default executor that uses the global and local executors.
ResultFuture
Future returned by Task::result().

Traits§

Executor
A trait for spawning Send + 'static futures.
LocalExecutor
A trait for spawning 'static futures that may not be Send.
Task
A trait representing a spawned task that can be awaited, cancelled, or queried for results.

Functions§

init_global_executor
Initialize the global executor for spawning Send futures.
init_local_executor
Initialize the thread-local executor for spawning non-Send futures.
spawn
Spawn a Send future on the global executor.
spawn_local
Spawn a future on the thread-local executor.
try_init_global_executor
Try to initialize the global executor for spawning Send futures.
try_init_local_executor
Try to initialize the thread-local executor for spawning non-Send futures.