Path: blob/main/crates/wasi/src/p3/random/mod.rs
3073 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};21/// use wasmtime::component::Linker;22/// use wasmtime_wasi::random::{WasiRandomView, WasiRandomCtx};23///24/// fn main() -> Result<()> {25/// let engine = Engine::default();26///27/// let mut linker = Linker::<MyState>::new(&engine);28/// wasmtime_wasi::p3::random::add_to_linker(&mut linker)?;29/// // ... add any further functionality to `linker` if desired ...30///31/// let mut store = Store::new(32/// &engine,33/// MyState {34/// random: WasiRandomCtx::default(),35/// },36/// );37///38/// // ... use `linker` to instantiate within `store` ...39///40/// Ok(())41/// }42///43/// struct MyState {44/// random: WasiRandomCtx,45/// }46///47/// impl WasiRandomView for MyState {48/// fn random(&mut self) -> &mut WasiRandomCtx { &mut self.random }49/// }50/// ```51pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>52where53T: WasiRandomView + 'static,54{55random::add_to_linker::<_, WasiRandom>(linker, T::random)?;56insecure::add_to_linker::<_, WasiRandom>(linker, T::random)?;57insecure_seed::add_to_linker::<_, WasiRandom>(linker, T::random)?;58Ok(())59}606162