Path: blob/main/crates/wasi/src/p3/clocks/mod.rs
3101 views
mod host;12use crate::clocks::{WasiClocks, WasiClocksView};3use crate::p3::bindings::clocks::{monotonic_clock, system_clock, types};4use wasmtime::component::Linker;56/// Add all WASI interfaces from this module into the `linker` provided.7///8/// This function will add all interfaces implemented by this module to the9/// [`Linker`], which corresponds to the `wasi:clocks/imports` world supported by10/// this module.11///12/// This is low-level API for advanced use cases,13/// [`wasmtime_wasi::p3::add_to_linker`](crate::p3::add_to_linker) can be used instead14/// to add *all* wasip3 interfaces (including the ones from this module) to the `linker`.15///16/// # Example17///18/// ```19/// use wasmtime::{Engine, Result, Store, Config};20/// use wasmtime::component::{Linker, ResourceTable};21/// use wasmtime_wasi::clocks::{WasiClocksView, WasiClocksCtxView, WasiClocksCtx};22///23/// fn main() -> Result<()> {24/// let mut config = Config::new();25/// config.wasm_component_model_async(true);26/// let engine = Engine::new(&config)?;27///28/// let mut linker = Linker::<MyState>::new(&engine);29/// wasmtime_wasi::p3::clocks::add_to_linker(&mut linker)?;30/// // ... add any further functionality to `linker` if desired ...31///32/// let mut store = Store::new(33/// &engine,34/// MyState::default(),35/// );36///37/// // ... use `linker` to instantiate within `store` ...38///39/// Ok(())40/// }41///42/// #[derive(Default)]43/// struct MyState {44/// clocks: WasiClocksCtx,45/// table: ResourceTable,46/// }47///48/// impl WasiClocksView for MyState {49/// fn clocks(&mut self) -> WasiClocksCtxView {50/// WasiClocksCtxView { ctx: &mut self.clocks, table: &mut self.table }51/// }52/// }53/// ```54pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>55where56T: WasiClocksView + 'static,57{58types::add_to_linker::<_, WasiClocks>(linker, T::clocks)?;59monotonic_clock::add_to_linker::<_, WasiClocks>(linker, T::clocks)?;60system_clock::add_to_linker::<_, WasiClocks>(linker, T::clocks)?;61Ok(())62}6364impl From<crate::clocks::Datetime> for system_clock::Instant {65fn from(66crate::clocks::Datetime {67seconds,68nanoseconds,69}: crate::clocks::Datetime,70) -> Self {71Self {72seconds,73nanoseconds,74}75}76}7778impl From<system_clock::Instant> for crate::clocks::Datetime {79fn from(80system_clock::Instant {81seconds,82nanoseconds,83}: system_clock::Instant,84) -> Self {85Self {86seconds,87nanoseconds,88}89}90}919293