wasmtime_wasi/p3/clocks/
mod.rs

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