Path: blob/main/crates/wasi/src/p3/random/mod.rs
1693 views
mod host;12use crate::p3::bindings::random::{insecure, insecure_seed, random};3use crate::random::{WasiRandom, WasiRandomView};4use wasmtime::component::Linker;56/// Add all WASI interfaces from this module into the `linker` provided.7///8/// This function will add all interfaces implemented by this module to the9/// [`Linker`], which corresponds to the `wasi:random/imports` world supported by10/// this crate.11///12/// This is low-level API for advanced use cases,13/// [`wasmtime_wasi::p3::add_to_linker`](crate::p3::add_to_linker) can be used instead14/// to add *all* wasip3 interfaces (including the ones from this module) to the `linker`.15///16///17/// # Example18///19/// ```20/// use wasmtime::{Engine, Result, Store, Config};21/// use wasmtime::component::Linker;22/// use wasmtime_wasi::random::{WasiRandomView, WasiRandomCtx};23///24/// fn main() -> Result<()> {25/// let mut config = Config::new();26/// config.async_support(true);27/// let engine = Engine::new(&config)?;28///29/// let mut linker = Linker::<MyState>::new(&engine);30/// wasmtime_wasi::p3::random::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 {36/// random: WasiRandomCtx::default(),37/// },38/// );39///40/// // ... use `linker` to instantiate within `store` ...41///42/// Ok(())43/// }44///45/// struct MyState {46/// random: WasiRandomCtx,47/// }48///49/// impl WasiRandomView for MyState {50/// fn random(&mut self) -> &mut WasiRandomCtx { &mut self.random }51/// }52/// ```53pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>54where55T: WasiRandomView + 'static,56{57random::add_to_linker::<_, WasiRandom>(linker, T::random)?;58insecure::add_to_linker::<_, WasiRandom>(linker, T::random)?;59insecure_seed::add_to_linker::<_, WasiRandom>(linker, T::random)?;60Ok(())61}626364