executor-core 0.3.0

A flexible task executor abstraction layer for Rust async runtimes
docs.rs failed to build executor-core-0.3.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: executor-core-0.2.1

executor-core

MIT licensed Crates.io Documentation

Write async libraries without choosing a runtime.

Your users should decide whether to use tokio, async-std, or any other runtime. Not you.

How It Works

Instead of hard-coding tokio::spawn, accept an executor parameter:

use executor_core::Executor;

pub async fn parallel_sum<E: Executor>(
    executor: &E,
    numbers: Vec<i32>
) -> i32 {
    let (left, right) = numbers.split_at(numbers.len() / 2);

    let left_sum = executor.spawn(async move {
        left.iter().sum::<i32>()
    });

    let right_sum = executor.spawn(async move {
        right.iter().sum::<i32>()
    });

    left_sum.await + right_sum.await
}

Users call it with their runtime:

// tokio users
let runtime = tokio::runtime::Runtime::new()?;
let sum = parallel_sum(&runtime, vec![1, 2, 3, 4]).await;

// async-executor users
let executor = async_executor::Executor::new();
let sum = parallel_sum(&executor, vec![1, 2, 3, 4]).await;

Quick Start

Library authors:

[dependencies]
executor-core = "0.2"

App developers:

[dependencies]
executor-core = { version = "0.2", features = ["tokio"] }

API

Two traits:

  • Executor - For Send futures
  • LocalExecutor - For non-Send futures

Both return async_task::Task:

let task = executor.spawn(async { work() });
let result = task.await;    // Get result
task.cancel().await;         // Cancel task
task.detach();              // Run in background

Supported Runtimes

Runtime Feature
tokio "tokio"
async-executor "async-executor"
Web/WASM "web"

License

MIT