Path: blob/main/crates/wasi/src/p3/sockets/mod.rs
1692 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.async_support(true);31/// config.wasm_component_model_async(true);32/// let engine = Engine::new(&config)?;33///34/// let mut linker = Linker::<MyState>::new(&engine);35/// wasmtime_wasi::p3::sockets::add_to_linker(&mut linker)?;36/// // ... add any further functionality to `linker` if desired ...37///38/// let mut store = Store::new(39/// &engine,40/// MyState::default(),41/// );42///43/// // ... use `linker` to instantiate within `store` ...44///45/// Ok(())46/// }47///48/// #[derive(Default)]49/// struct MyState {50/// sockets: WasiSocketsCtx,51/// table: ResourceTable,52/// }53///54/// impl WasiSocketsView for MyState {55/// fn sockets(&mut self) -> WasiSocketsCtxView<'_> {56/// WasiSocketsCtxView {57/// ctx: &mut self.sockets,58/// table: &mut self.table,59/// }60/// }61/// }62/// ```63pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>64where65T: WasiSocketsView + 'static,66{67ip_name_lookup::add_to_linker::<_, WasiSockets>(linker, T::sockets)?;68types::add_to_linker::<_, WasiSockets>(linker, T::sockets)?;69Ok(())70}717273