Skip to main content

wasmtime_wasi/p3/clocks/
mod.rs

1mod host;
2
3use crate::clocks::{WasiClocks, WasiClocksView};
4use crate::p3::bindings::clocks::{monotonic_clock, system_clock, types};
5use wasmtime::component::Linker;
6
7/// Add all WASI interfaces from this module into the `linker` provided.
8///
9/// This function will add all interfaces implemented by this module to the
10/// [`Linker`], which corresponds to the `wasi:clocks/imports` world supported by
11/// 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 instead
15/// to add *all* wasip3 interfaces (including the ones from this module) to the `linker`.
16///
17/// # Example
18///
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.wasm_component_model_async(true);
27///     let engine = Engine::new(&config)?;
28///
29///     let mut linker = Linker::<MyState>::new(&engine);
30///     wasmtime_wasi::p3::clocks::add_to_linker(&mut linker)?;
31///     // ... add any further functionality to `linker` if desired ...
32///
33///     let mut store = Store::new(
34///         &engine,
35///         MyState::default(),
36///     );
37///
38///     // ... use `linker` to instantiate within `store` ...
39///
40///     Ok(())
41/// }
42///
43/// #[derive(Default)]
44/// struct MyState {
45///     clocks: WasiClocksCtx,
46///     table: ResourceTable,
47/// }
48///
49/// impl WasiClocksView for MyState {
50///     fn clocks(&mut self) -> WasiClocksCtxView {
51///         WasiClocksCtxView { ctx: &mut self.clocks, table: &mut self.table }
52///     }
53/// }
54/// ```
55pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>
56where
57    T: WasiClocksView + 'static,
58{
59    types::add_to_linker::<_, WasiClocks>(linker, T::clocks)?;
60    monotonic_clock::add_to_linker::<_, WasiClocks>(linker, T::clocks)?;
61    system_clock::add_to_linker::<_, WasiClocks>(linker, T::clocks)?;
62    Ok(())
63}
64
65impl From<crate::clocks::Datetime> for system_clock::Instant {
66    fn from(
67        crate::clocks::Datetime {
68            seconds,
69            nanoseconds,
70        }: crate::clocks::Datetime,
71    ) -> Self {
72        Self {
73            seconds,
74            nanoseconds,
75        }
76    }
77}
78
79impl From<system_clock::Instant> for crate::clocks::Datetime {
80    fn from(
81        system_clock::Instant {
82            seconds,
83            nanoseconds,
84        }: system_clock::Instant,
85    ) -> Self {
86        Self {
87            seconds,
88            nanoseconds,
89        }
90    }
91}