xref: /wasmtime-44.0.1/crates/wasi/src/p3/random/mod.rs (revision cc8d04f4)
1 mod host;
2 
3 use crate::p3::bindings::random::{insecure, insecure_seed, random};
4 use crate::random::{WasiRandom, WasiRandomView};
5 use 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:random/imports` world supported by
11 /// this crate.
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 ///
18 /// # Example
19 ///
20 /// ```
21 /// use wasmtime::{Engine, Result, Store};
22 /// use wasmtime::component::Linker;
23 /// use wasmtime_wasi::random::{WasiRandomView, WasiRandomCtx};
24 ///
25 /// fn main() -> Result<()> {
26 ///     let engine = Engine::default();
27 ///
28 ///     let mut linker = Linker::<MyState>::new(&engine);
29 ///     wasmtime_wasi::p3::random::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 {
35 ///             random: WasiRandomCtx::default(),
36 ///         },
37 ///     );
38 ///
39 ///     // ... use `linker` to instantiate within `store` ...
40 ///
41 ///     Ok(())
42 /// }
43 ///
44 /// struct MyState {
45 ///     random: WasiRandomCtx,
46 /// }
47 ///
48 /// impl WasiRandomView for MyState {
49 ///     fn random(&mut self) -> &mut WasiRandomCtx { &mut self.random }
50 /// }
51 /// ```
add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()> where T: WasiRandomView + 'static,52 pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>
53 where
54     T: WasiRandomView + 'static,
55 {
56     random::add_to_linker::<_, WasiRandom>(linker, T::random)?;
57     insecure::add_to_linker::<_, WasiRandom>(linker, T::random)?;
58     insecure_seed::add_to_linker::<_, WasiRandom>(linker, T::random)?;
59     Ok(())
60 }
61