Spawns
Thread context task spawner for Rust to ease async runtime agnostic coding.
Motivation
Currently, Rust does not have a standard async runtime. This exposes us a dilemma to choose one and makes creating runtime agnostic library pretty hard. The most challenging thing we have to face is how to spawn task ?
spawns-core proposes a thread context task spawner for Rust std and async runtimes. Once delivered, we are able to spawn tasks in runtime agnostic manner. Together with other runtime agnostic io, timer, channel and etc. crates, we are capable to write runtime agnostic code easily.
API for async runtimes
/// Thin wrapper around task to accommodate possible new members.
/// Trait to spawn task.
/// Scope where tasks are [spawn]ed through given [Spawn].
/// Enters a scope where new tasks will be [spawn]ed through given [Spawn].
;
Async runtimes have to do two things to accommodate for other runtime agnostic API.
- Implements
Spawnto spawn asynchronous task. - Calls
enterin all executor threads.
API for clients
/// Spawns a new task.
///
/// # Panics
/// 1. Panic if no spawner.
/// 2. Panic if [Spawn::spawn] panic.
The API is capable to spawn, join and cancel tasks as what tokio, smol and async-std do.
Concerns
- Boxing ? Yes, it needs
GlobalAlloc. - Boxing even the entry future ? No, but
try_id()will returnNone. I guess we could provides function to wrap a bit. no_std? No, it needsthread_local!currently. We can move this to#[thread_local]once stabilized.spawn_localfor!Sendfuture ? No, at least for now. I saw onlyasync-global-executoris capable tospawn_localfreely. Personally, I think it is Rust's responsibility to not treat futures owning!Sendas!Send. This way there are little chance for us to create!Sendfutures. For futures that capturing!Sendin first place and storing thread local!Send, they need current thread executor.
Compatibility
Package spawns-core uses linkme to detect available async runtimes from external packages. Package spawns-compat provides features tokio, smol and async-global-executor to detect async runtimes for spawns-core. smol and async-global-executor can't coexist as they don't have tokio::runtime::Handle::try_current() like method to detect thread context aware executor.
Integrations
Package spawns-executor provides full functional block_on with both current thread executor and multi-thread executor.
Examples
See examples. A minimum runtime agnostic echo server is listed here for demonstration.
use *;
use io;
pub async
All you have to do for it to be function is setting up thread context task spawner.