Crate executor_core

Source
Expand description

A flexible task executor abstraction layer for Rust async runtimes.

This crate provides a unified interface for spawning and managing async tasks across different executor backends. It supports both global and local executors, with built-in implementations for popular runtimes like async-executor and tokio.

§Quick Start

use executor_core::spawn;

// Spawn a task on the global executor
let task = spawn(async {
    println!("Hello from async task!");
    42
});

// Await the result
let result = task.await;
assert_eq!(result, 42);

§Error Handling

Tasks can be awaited directly (panics propagate) or handled explicitly:

use executor_core::{spawn, Error};
let task = spawn(async {
    panic!("Something went wrong");
    42
});

// Option 1: Direct await (panics propagate)
let result = task.await;

// Option 2: Explicit error handling
match task.result().await {
    Ok(value) => println!("Success: {}", value),
    Err(Error::Panicked(msg)) => println!("Task panicked: {}", msg),
    Err(Error::Cancelled) => println!("Task was cancelled"),
}

§Thread Safety

The crate provides separate traits for thread-safe and thread-local execution:

use executor_core::{Executor, LocalExecutor};
use std::rc::Rc;

// ✅ Send futures work with Executor
let executor = async_executor::Executor::new();
let task = executor.spawn(async { "Hello".to_string() });

// ✅ Non-Send futures work with LocalExecutor
let local_executor = async_executor::LocalExecutor::new();
let local_task = local_executor.spawn(async {
    let data = Rc::new(42); // Rc is not Send
    *data
});

§Feature Flags

  • default-async-executor (default) - Use async-executor as the global executor
  • default-tokio - Use tokio as the global executor
  • async-executor - Enable async-executor backend support
  • tokio - Enable tokio backend support
  • std - Enable standard library support (auto-enabled by executor backends)

Enums§

Error
Errors that can occur during task execution.

Traits§

Executor
A trait for executor implementations that can spawn thread-safe futures.
LocalExecutor
A trait for executor implementations that can spawn futures on the current thread.
LocalTask
A handle to a spawned task that may not be Send.
Task
A handle to a spawned task that can be safely moved between threads.

Functions§

spawn
Spawns a new task on the global executor.