cross-runtime 0.1.0

One task, timer, and runtime surface across native Tokio and browser WebAssembly
Documentation
#![allow(async_fn_in_trait)]

mod runtime;
mod task;

pub use runtime::Runtime;
pub use task::{JoinError, JoinHandle, spawn, spawn_local};

/// Timeout expiry error, using the clock native to each runtime.
#[cfg(not(target_arch = "wasm32"))]
pub use tokio::time::error::Elapsed;
#[cfg(target_arch = "wasm32")]
pub use wasmtimer::tokio::error::Elapsed;

/// Sleep without coupling callers to the active platform runtime.
pub async fn sleep(duration: std::time::Duration) {
    #[cfg(not(target_arch = "wasm32"))]
    tokio::time::sleep(duration).await;

    #[cfg(target_arch = "wasm32")]
    wasmtimer::tokio::sleep(duration).await;
}

/// Bound `future` by `duration` using the active platform runtime.
pub async fn timeout<F: std::future::Future>(
    duration: std::time::Duration,
    future: F,
) -> Result<F::Output, Elapsed> {
    #[cfg(not(target_arch = "wasm32"))]
    {
        tokio::time::timeout(duration, future).await
    }

    #[cfg(target_arch = "wasm32")]
    {
        wasmtimer::tokio::timeout(duration, future).await
    }
}