Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ A small library designed to utilize async executors through an common API while
default = ["tokio"]
threadpool = ["futures/thread-pool"]
tokio = ["dep:tokio"]
compio = ["dep:compio"]

[dependencies]
futures = { version = "0.3.31", default-features = false, features = ["std", "alloc", "async-await"] }
Expand All @@ -28,12 +29,14 @@ either = { version = "1.15.0", optional = true}
thiserror = "1.0.69"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.49.0", optional = true, features = ["rt", "rt-multi-thread"] }
tokio = { version = "1.49.0", optional = true, features = ["rt", "rt-multi-thread"] }
compio = { version = "0.19", optional = true, default-features = false, features = ["runtime"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4.50"

[dev-dependencies]
futures-timer = "3.0.3"
tokio = { version = "1.49.0", features = ["rt", "rt-multi-thread", "macros"] }
compio = { version = "0.19", default-features = false, features = ["runtime", "macros"]}
futures = { version = "0.3.31", default-features = false, features = ["std", "alloc", "async-await", "thread-pool", "executor"] }
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
## Overview

async-rt is a small library designed to utilize async executors (i.e Tokio) through a common API while extending functionality
for abortable task, tasks that receive messages and being able to switch between two async runtimes at compile time if specific
conditions are met (i.e tokio if the feature is enabled and its a non-wasm32 arch, while wasm32-bindgen-futures is used if the arch is wasm32).
for abortable task, tasks that receive messages and being able to switch between multiple async runtimes at compile time if specific
conditions are met (i.e tokio or compio if the feature is enabled and its a non-wasm32 arch, while wasm32-bindgen-futures is used if the arch is wasm32).

## MSRV

Expand Down
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ impl From<tokio::task::JoinError> for JoinError {
}
}

#[cfg(all(feature = "compio", not(target_arch = "wasm32")))]
impl From<compio::runtime::JoinError> for JoinError {
fn from(err: compio::runtime::JoinError) -> Self {
match err {
compio::runtime::JoinError::Cancelled => JoinError::Cancelled,
compio::runtime::JoinError::Panicked(_) => JoinError::Panicked,
}
}
}

/// Error indicating a task did not complete before its timeout elapsed.
///
/// Returned as the inner error of a timeout task's output, for example from
Expand Down
36 changes: 31 additions & 5 deletions src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::future::Future;

/// Executor that selects an available runtime backend at compile time.
///
/// * On non-Wasm targets with the `tokio` feature enabled, it uses
/// `TokioExecutor`.
/// * On non-Wasm targets with the `threadpool` feature enabled and the `tokio`
/// * On non-Wasm targets with the `tokio` or `compio` feature enabled, it uses
/// `TokioExecutor` or `CompioExecutor`.
/// * On non-Wasm targets with the `threadpool` feature enabled and the `tokio` or `compio`
/// feature disabled, it uses `ThreadPoolExecutor`.
/// * On Wasm targets, it uses `WasmExecutor`, backed by
/// `wasm-bindgen-futures`.
Expand All @@ -23,10 +23,22 @@ impl Executor for GlobalExecutor {
}

#[cfg(all(
feature = "threadpool",
feature = "compio",
not(any(feature = "tokio", target_arch = "wasm32"))
))]
fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
crate::rt::compio::CompioExecutor.spawn(future)
}

#[cfg(all(
feature = "threadpool",
not(any(feature = "tokio", feature = "compio", target_arch = "wasm32"))
))]
fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
Expand All @@ -46,6 +58,7 @@ impl Executor for GlobalExecutor {
#[cfg(all(
not(feature = "threadpool"),
not(feature = "tokio"),
not(feature = "compio"),
not(target_arch = "wasm32")
))]
fn spawn<F>(&self, _: F) -> JoinHandle<F::Output>
Expand All @@ -68,10 +81,22 @@ impl ExecutorBlocking for GlobalExecutor {
}

#[cfg(all(
feature = "threadpool",
feature = "compio",
not(any(feature = "tokio", target_arch = "wasm32"))
))]
fn spawn_blocking<F, R>(&self, f: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
crate::rt::compio::CompioExecutor.spawn_blocking(f)
}

#[cfg(all(
feature = "threadpool",
not(any(feature = "tokio", feature = "compio", target_arch = "wasm32"))
))]
fn spawn_blocking<F, R>(&self, f: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
Expand All @@ -91,6 +116,7 @@ impl ExecutorBlocking for GlobalExecutor {
#[cfg(all(
not(feature = "threadpool"),
not(feature = "tokio"),
not(feature = "compio"),
not(target_arch = "wasm32")
))]
fn spawn_blocking<F, R>(&self, _: F) -> JoinHandle<R>
Expand Down
Loading
Loading