Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi/src/p3/random/mod.rs
1693 views
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, Config};
22
/// use wasmtime::component::Linker;
23
/// use wasmtime_wasi::random::{WasiRandomView, WasiRandomCtx};
24
///
25
/// fn main() -> Result<()> {
26
/// let mut config = Config::new();
27
/// config.async_support(true);
28
/// let engine = Engine::new(&config)?;
29
///
30
/// let mut linker = Linker::<MyState>::new(&engine);
31
/// wasmtime_wasi::p3::random::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 {
37
/// random: WasiRandomCtx::default(),
38
/// },
39
/// );
40
///
41
/// // ... use `linker` to instantiate within `store` ...
42
///
43
/// Ok(())
44
/// }
45
///
46
/// struct MyState {
47
/// random: WasiRandomCtx,
48
/// }
49
///
50
/// impl WasiRandomView for MyState {
51
/// fn random(&mut self) -> &mut WasiRandomCtx { &mut self.random }
52
/// }
53
/// ```
54
pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>
55
where
56
T: WasiRandomView + 'static,
57
{
58
random::add_to_linker::<_, WasiRandom>(linker, T::random)?;
59
insecure::add_to_linker::<_, WasiRandom>(linker, T::random)?;
60
insecure_seed::add_to_linker::<_, WasiRandom>(linker, T::random)?;
61
Ok(())
62
}
63
64