linera-kywasmtime

Crates.iolinera-kywasmtime
lib.rslinera-kywasmtime
version0.1.0
created_at2025-09-13 21:48:25.284735+00
updated_at2025-09-13 21:48:25.284735+00
descriptionTime utils for WASM (Linera fork)
homepage
repositoryhttps://github.com/linera-io/kywasmtime
max_upload_size
id1838152
size25,477
Andreas Fackler (afck)

documentation

README

!! 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"),
    }
}
Commit count: 7

cargo fmt