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 spawningSend + 'static
futuresLocalExecutor
: For spawning'static
futures (not necessarilySend
)
Both traits produce tasks that implement the Task
trait, providing:
Future
implementation for awaiting resultsTask::poll_result
for explicit error handlingTask::poll_cancel
for task cancellation
§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
andLocalExecutor
traits using Generic Associated Types (GAT) to prevent unnecessary heap allocation and dynamic dispatch - Type Erasure:
AnyExecutor
andAnyLocalExecutor
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.
Modules§
- async_
executor async-executor
- Integration with the
async-executor
crate. - async_
task async-task
- Integration with the
async-task
crate. - tokio
tokio
- Integration with the Tokio async runtime.
- web
web
- Integration for Web/WASM environments.
Structs§
- AnyExecutor
- A type-erased
Executor
that can hold any executor implementation. - AnyExecutor
Task - Task type returned by
AnyExecutor
. - AnyLocal
Executor - A type-erased
LocalExecutor
that can hold any local executor implementation. - AnyLocal
Executor Task - Task type returned by
AnyLocalExecutor
. - Cancel
Future - Future returned by
Task::cancel()
. - Default
Executor - A default executor that uses the global and local executors.
- Result
Future - Future returned by
Task::result()
.
Traits§
- Executor
- A trait for spawning
Send + 'static
futures. - Local
Executor - A trait for spawning
'static
futures that may not beSend
. - 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.