Path: blob/main/crates/wasi/src/p3/sockets/mod.rs
3090 views
use crate::TrappableError;1use crate::p3::bindings::sockets::{ip_name_lookup, types};2use crate::sockets::{WasiSockets, WasiSocketsView};3use wasmtime::component::Linker;45mod conv;6mod host;78pub type SocketResult<T> = Result<T, SocketError>;9pub type SocketError = TrappableError<types::ErrorCode>;1011/// Add all WASI interfaces from this module into the `linker` provided.12///13/// This function will add all interfaces implemented by this module to the14/// [`Linker`], which corresponds to the `wasi:sockets/imports` world supported by15/// this module.16///17/// This is low-level API for advanced use cases,18/// [`wasmtime_wasi::p3::add_to_linker`](crate::p3::add_to_linker) can be used instead19/// to add *all* wasip3 interfaces (including the ones from this module) to the `linker`.20///21/// # Example22///23/// ```24/// use wasmtime::{Engine, Result, Store, Config};25/// use wasmtime::component::{Linker, ResourceTable};26/// use wasmtime_wasi::sockets::{WasiSocketsCtx, WasiSocketsCtxView, WasiSocketsView};27///28/// fn main() -> Result<()> {29/// let mut config = Config::new();30/// config.wasm_component_model_async(true);31/// let engine = Engine::new(&config)?;32///33/// let mut linker = Linker::<MyState>::new(&engine);34/// wasmtime_wasi::p3::sockets::add_to_linker(&mut linker)?;35/// // ... add any further functionality to `linker` if desired ...36///37/// let mut store = Store::new(38/// &engine,39/// MyState::default(),40/// );41///42/// // ... use `linker` to instantiate within `store` ...43///44/// Ok(())45/// }46///47/// #[derive(Default)]48/// struct MyState {49/// sockets: WasiSocketsCtx,50/// table: ResourceTable,51/// }52///53/// impl WasiSocketsView for MyState {54/// fn sockets(&mut self) -> WasiSocketsCtxView<'_> {55/// WasiSocketsCtxView {56/// ctx: &mut self.sockets,57/// table: &mut self.table,58/// }59/// }60/// }61/// ```62pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>63where64T: WasiSocketsView + 'static,65{66ip_name_lookup::add_to_linker::<_, WasiSockets>(linker, T::sockets)?;67types::add_to_linker::<_, WasiSockets>(linker, T::sockets)?;68Ok(())69}707172