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 + 'staticfuturesLocalExecutor: For spawning'staticfutures (not necessarilySend)
Both traits produce tasks that implement the Task trait, providing:
Futureimplementation for awaiting resultsTask::poll_resultfor explicit error handlingTask::poll_cancelfor task cancellation
§Quick Start
use executor_core::{Executor, init_global_executor, spawn};
use executor_core::tokio::DefaultExecutor;
#[tokio::main]
async fn main() {
// Initialize the global executor
init_global_executor(DefaultExecutor::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
ExecutorandLocalExecutortraits using Generic Associated Types (GAT) to prevent unnecessary heap allocation and dynamic dispatch - Type Erasure:
AnyExecutorandAnyLocalExecutorfor 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-executorpub use tokio::DefaultExecutor;tokiopub use tokio::TokioLocalTask;tokiopub use tokio::TokioTask;tokiopub use web::WebExecutor;webpub use web::WebTask;web
Modules§
- async_
executor async-executor - Integration with the
async-executorcrate. - tokio
tokio - Integration with the Tokio async runtime.
- web
web - Integration for Web/WASM environments.
Structs§
- AnyExecutor
- A type-erased
Executorthat can hold any executor implementation. - AnyExecutor
Task - Task type returned by
AnyExecutor. - AnyLocal
Executor - A type-erased
LocalExecutorthat can hold any local executor implementation. - AnyLocal
Executor Task - Task type returned by
AnyLocalExecutor. - Cancel
Future - Future returned by
Task::cancel(). - Result
Future - Future returned by
Task::result().
Traits§
- Executor
- A trait for spawning
Send + 'staticfutures. - Local
Executor - A trait for spawning
'staticfutures 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
Sendfuture 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.