cross-runtime 0.1.0

One task, timer, and runtime surface across native Tokio and browser WebAssembly
Documentation
#![cfg(not(target_arch = "wasm32"))]

use cross_runtime::{Runtime, spawn, timeout};
use std::time::Duration;

#[test]
fn runtime_spawns_and_joins() {
    let runtime = Runtime::default();
    let value = runtime.block_on(async { runtime.spawn(async { 42 }).await });
    assert_eq!(value.unwrap(), 42);
}

#[test]
fn free_spawn_uses_current_runtime() {
    let runtime = Runtime::default();
    let value = runtime.block_on(async { spawn(async { 42 }).await });
    assert_eq!(value.unwrap(), 42);
}

#[test]
fn dropped_handle_detaches() {
    let runtime = Runtime::default();
    runtime.block_on(async {
        let (completed, receipt) = tokio::sync::oneshot::channel();
        drop(spawn(async move {
            let _ = completed.send(42);
        }));
        let value = timeout(Duration::from_secs(1), receipt)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(value, 42);
    });
}

#[test]
fn abort_cancels() {
    let runtime = Runtime::default();
    runtime.block_on(async {
        let task = spawn(std::future::pending::<()>());
        task.abort();
        assert!(task.await.unwrap_err().is_cancelled());
    });
}

#[test]
fn timeout_uses_runtime_clock() {
    let runtime = Runtime::default();
    runtime.block_on(async {
        let result = timeout(Duration::from_millis(1), std::future::pending::<()>()).await;
        assert!(result.is_err());
    });
}