1 unstable release

0.1.0 Sep 13, 2025

#145 in WebAssembly

Download history 113/week @ 2025-09-07 1936/week @ 2025-09-14 2231/week @ 2025-09-21 2310/week @ 2025-09-28 1949/week @ 2025-10-05 1827/week @ 2025-10-12 2267/week @ 2025-10-19

8,502 downloads per month
Used in 8 crates (via linera-base)

MIT license

20KB
416 lines

!! This is a temporary fork to be used only in the Linera protocol !!

Implementation of the tokio::time API, but for WASM.

Usage

This lib replicates the tokio API, so users only have to change their use declarations:

#[cfg(not(target_family="wasm"))]
use tokio::time::*;
#[cfg(target_family="wasm")]
use linera_kywasmtime::*;

Examples

(copied from src/lib.rs)

use std::time::Duration;
use linera_kywasmtime::*;

async fn demo_sleep() {
    let duration = Duration::from_millis(500);
    sleep(duration).await;
}

async fn demo_sleep_until() {
    let deadline = Instant::now() + Duration::from_millis(200);
    sleep_until(deadline).await;
}

async fn demo_interval() {
    let mut period = Duration::from_secs(1);
    let mut interval = interval(period);
    interval.tick().await;
    println!("tick 1");
    interval.tick().await;
    println!("tick 2");
    interval.tick().await;
    println!("tick 3");
}

async fn demo_interval_at() {
    let mut start = Instant::now() + Duration::from_millis(200);
    let mut period = Duration::from_secs(1);
    let mut interval = interval_at(start, period);
    interval.tick().await;
    println!("tick 1");
    interval.tick().await;
    println!("tick 2");
    interval.tick().await;
    println!("tick 3");
}

async fn demo_timeout() {
    let duration = Duration::from_millis(500);
    match timeout(duration, async move {
        // simulate long action
        sleep(Duration::from_millis(500));
        42
    }).await {
        Ok(result) => println!("{result}"),
        Err(_) => println!("timeout"),
    }
}

async fn demo_timeout_at() {
    let deadline = Instant::now() + Duration::from_millis(200);
    match timeout_at(deadline, async move {
        // simulate long action
        sleep(Duration::from_millis(500));
        42
    }).await {
        Ok(result) => println!("{result}"),
        Err(_) => println!("timeout"),
    }
}

Dependencies

~0.8–2.8MB
~50K SLoC