Sha256: 0270df0bf3555b234e48db714beea0d1482c67675e0cf7361b96db4954a254f2

Contents?: true

Size: 1.93 KB

Versions: 14

Compression:

Stored size: 1.93 KB

Contents

use super::{HostMonotonicClock, HostWallClock};
use cap_std::time::{Duration, Instant, SystemClock};
use cap_std::{ambient_authority, AmbientAuthority};
use cap_time_ext::{MonotonicClockExt, SystemClockExt};

pub struct WallClock {
    /// The underlying system clock.
    clock: cap_std::time::SystemClock,
}

impl WallClock {
    pub fn new(ambient_authority: AmbientAuthority) -> Self {
        Self {
            clock: cap_std::time::SystemClock::new(ambient_authority),
        }
    }
}

impl HostWallClock for WallClock {
    fn resolution(&self) -> Duration {
        self.clock.resolution()
    }

    fn now(&self) -> Duration {
        // WASI defines wall clocks to return "Unix time".
        self.clock
            .now()
            .duration_since(SystemClock::UNIX_EPOCH)
            .unwrap()
    }
}

pub struct MonotonicClock {
    /// The underlying system clock.
    clock: cap_std::time::MonotonicClock,

    /// The `Instant` this clock was created. All returned times are
    /// durations since that time.
    initial: Instant,
}

impl MonotonicClock {
    pub fn new(ambient_authority: AmbientAuthority) -> Self {
        let clock = cap_std::time::MonotonicClock::new(ambient_authority);
        let initial = clock.now();
        Self { clock, initial }
    }
}

impl HostMonotonicClock for MonotonicClock {
    fn resolution(&self) -> u64 {
        self.clock.resolution().as_nanos().try_into().unwrap()
    }

    fn now(&self) -> u64 {
        // Unwrap here and in `resolution` above; a `u64` is wide enough to
        // hold over 584 years of nanoseconds.
        self.clock
            .now()
            .duration_since(self.initial)
            .as_nanos()
            .try_into()
            .unwrap()
    }
}

pub fn monotonic_clock() -> Box<dyn HostMonotonicClock + Send> {
    Box::new(MonotonicClock::new(ambient_authority()))
}

pub fn wall_clock() -> Box<dyn HostWallClock + Send> {
    Box::new(WallClock::new(ambient_authority()))
}

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/wasmtime-wasi-29.0.0/src/clocks/host.rs
wasmtime-28.0.0 ./ext/cargo-vendor/wasmtime-wasi-28.0.0/src/clocks/host.rs
wasmtime-27.0.0 ./ext/cargo-vendor/wasmtime-wasi-27.0.0/src/clocks/host.rs
wasmtime-26.0.0 ./ext/cargo-vendor/wasmtime-wasi-26.0.0/src/clocks/host.rs
wasmtime-25.0.2 ./ext/cargo-vendor/wasmtime-wasi-25.0.2/src/clocks/host.rs
wasmtime-25.0.1 ./ext/cargo-vendor/wasmtime-wasi-25.0.1/src/clocks/host.rs
wasmtime-25.0.0 ./ext/cargo-vendor/wasmtime-wasi-25.0.0/src/clocks/host.rs
wasmtime-24.0.0 ./ext/cargo-vendor/wasmtime-wasi-24.0.0/src/clocks/host.rs
wasmtime-23.0.2 ./ext/cargo-vendor/wasmtime-wasi-23.0.2/src/clocks/host.rs
wasmtime-22.0.0 ./ext/cargo-vendor/wasmtime-wasi-22.0.0/src/clocks/host.rs
wasmtime-21.0.1 ./ext/cargo-vendor/wasmtime-wasi-21.0.1/src/clocks/host.rs
wasmtime-20.0.2 ./ext/cargo-vendor/wasmtime-wasi-20.0.2/src/clocks/host.rs
wasmtime-20.0.0 ./ext/cargo-vendor/wasmtime-wasi-20.0.0/src/clocks/host.rs
wasmtime-18.0.3 ./ext/cargo-vendor/wasmtime-wasi-18.0.3/src/preview2/clocks/host.rs