Skip to main content

Crate async_rs

Crate async_rs 

Source
Expand description

A Rust async runtime abstraction library.

Provides a unified Runtime type and traits such as Executor, Reactor, and AsyncToSocketAddrs that abstract over Tokio, smol, and async-global-executor. Features enable implementations and may be combined. Applications choose a runtime when constructing Runtime; library crates can use generic trait bounds to remain runtime-agnostic.

§Feature flags

FlagNotes
tokio (default)Tokio runtime
smolsmol executor
async-global-executorExecutor; combine with async-io for AGERuntime
async-ioasync-io reactor (required by smol)
hickory-dnsHickory DNS resolver (tokio only)

NoopRuntime is always available without a feature flag.

An owned Tokio runtime uses nonblocking shutdown when its final owner is dropped while a Tokio handle is current. This includes a plain Handle::enter() scope, so spawn_blocking work can continue after the drop returns. Await work that must finish before dropping the final owner. On a plain thread, leave the Handle::enter() scope first or call TokioRuntime::shutdown_blocking() to wait. The method returns the runtime if another clone still owns it or it is called from a Tokio task, including a spawn_blocking task. Call it from an ordinary thread, not a runtime worker or an active async executor.

§Example

use async_rs::{Runtime, TokioRuntime, traits::*};
use std::{io, time::Duration};

async fn get_a(rt: &TokioRuntime) -> io::Result<u32> {
    rt.spawn_blocking(|| Ok(12)).await
}

async fn get_b(rt: &TokioRuntime) -> io::Result<u32> {
    rt.spawn(async { Ok(30) }).await
}

async fn tokio_main(rt: &TokioRuntime) -> io::Result<()> {
    let a = get_a(rt).await?;
    let b = get_b(rt).await?;
    rt.sleep(Duration::from_millis(500)).await;
    assert_eq!(a + b, 42);
    Ok(())
}

fn main() -> io::Result<()> {
    let rt = Runtime::tokio()?;
    rt.block_on(tokio_main(&rt))
}

Note that the io::Result above is the task’s own output: awaiting a Task leaves no room to report that the task itself failed, so a task which panicked resumes its panic in the awaiting task, and awaiting one which was canceled, or whose runtime went away, panics too.

Modules§

traits
A collection of traits to define a common interface across async runtimes
util
A collection of utilities to deal with IO, futures and runtimes

Structs§

AsyncGlobalExecutor
Dummy object implementing executor common interfaces on top of async-global-executor
AsyncIO
Dummy object implementing reactor common interfaces on top of async-io
HickoryToSocketAddrs
Perform async DNS resolution using hickory-dns
Noop
A no-op RuntimeKit implementation that never actually executes tasks or I/O
Runtime
A full-featured async runtime that combines an executor and a reactor.
Smol
The RuntimeKit implementation backed by the smol async runtime
Tokio
The RuntimeKit implementation backed by the tokio async runtime

Type Aliases§

AGERuntime
Type alias for the async-global-executor runtime
NoopRuntime
Type alias for the noop runtime
SmolRuntime
Type alias for the smol runtime
TokioRuntime
Type alias for the tokio runtime