Path: blob/main/crates/wasi/src/p3/clocks/mod.rs
1692 views
mod host;12use crate::clocks::{WasiClocks, WasiClocksView};3use crate::p3::bindings::clocks::{monotonic_clock, wall_clock};4use cap_std::time::SystemTime;5use wasmtime::component::Linker;67/// Add all WASI interfaces from this module into the `linker` provided.8///9/// This function will add all interfaces implemented by this module to the10/// [`Linker`], which corresponds to the `wasi:clocks/imports` world supported by11/// this module.12///13/// This is low-level API for advanced use cases,14/// [`wasmtime_wasi::p3::add_to_linker`](crate::p3::add_to_linker) can be used instead15/// to add *all* wasip3 interfaces (including the ones from this module) to the `linker`.16///17/// # Example18///19/// ```20/// use wasmtime::{Engine, Result, Store, Config};21/// use wasmtime::component::{Linker, ResourceTable};22/// use wasmtime_wasi::clocks::{WasiClocksView, WasiClocksCtxView, WasiClocksCtx};23///24/// fn main() -> Result<()> {25/// let mut config = Config::new();26/// config.async_support(true);27/// config.wasm_component_model_async(true);28/// let engine = Engine::new(&config)?;29///30/// let mut linker = Linker::<MyState>::new(&engine);31/// wasmtime_wasi::p3::clocks::add_to_linker(&mut linker)?;32/// // ... add any further functionality to `linker` if desired ...33///34/// let mut store = Store::new(35/// &engine,36/// MyState::default(),37/// );38///39/// // ... use `linker` to instantiate within `store` ...40///41/// Ok(())42/// }43///44/// #[derive(Default)]45/// struct MyState {46/// clocks: WasiClocksCtx,47/// table: ResourceTable,48/// }49///50/// impl WasiClocksView for MyState {51/// fn clocks(&mut self) -> WasiClocksCtxView {52/// WasiClocksCtxView { ctx: &mut self.clocks, table: &mut self.table }53/// }54/// }55/// ```56pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>57where58T: WasiClocksView + 'static,59{60monotonic_clock::add_to_linker::<_, WasiClocks>(linker, T::clocks)?;61wall_clock::add_to_linker::<_, WasiClocks>(linker, T::clocks)?;62Ok(())63}6465impl From<crate::clocks::Datetime> for wall_clock::Datetime {66fn from(67crate::clocks::Datetime {68seconds,69nanoseconds,70}: crate::clocks::Datetime,71) -> Self {72Self {73seconds,74nanoseconds,75}76}77}7879impl From<wall_clock::Datetime> for crate::clocks::Datetime {80fn from(81wall_clock::Datetime {82seconds,83nanoseconds,84}: wall_clock::Datetime,85) -> Self {86Self {87seconds,88nanoseconds,89}90}91}9293impl TryFrom<SystemTime> for wall_clock::Datetime {94type Error = wasmtime::Error;9596fn try_from(time: SystemTime) -> Result<Self, Self::Error> {97let time = crate::clocks::Datetime::try_from(time)?;98Ok(time.into())99}100}101102103