Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi/src/p3/sockets/mod.rs
3090 views
1
use crate::TrappableError;
2
use crate::p3::bindings::sockets::{ip_name_lookup, types};
3
use crate::sockets::{WasiSockets, WasiSocketsView};
4
use wasmtime::component::Linker;
5
6
mod conv;
7
mod host;
8
9
pub type SocketResult<T> = Result<T, SocketError>;
10
pub type SocketError = TrappableError<types::ErrorCode>;
11
12
/// Add all WASI interfaces from this module into the `linker` provided.
13
///
14
/// This function will add all interfaces implemented by this module to the
15
/// [`Linker`], which corresponds to the `wasi:sockets/imports` world supported by
16
/// this module.
17
///
18
/// This is low-level API for advanced use cases,
19
/// [`wasmtime_wasi::p3::add_to_linker`](crate::p3::add_to_linker) can be used instead
20
/// to add *all* wasip3 interfaces (including the ones from this module) to the `linker`.
21
///
22
/// # Example
23
///
24
/// ```
25
/// use wasmtime::{Engine, Result, Store, Config};
26
/// use wasmtime::component::{Linker, ResourceTable};
27
/// use wasmtime_wasi::sockets::{WasiSocketsCtx, WasiSocketsCtxView, WasiSocketsView};
28
///
29
/// fn main() -> Result<()> {
30
/// let mut config = Config::new();
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
/// ```
63
pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>
64
where
65
T: WasiSocketsView + 'static,
66
{
67
ip_name_lookup::add_to_linker::<_, WasiSockets>(linker, T::sockets)?;
68
types::add_to_linker::<_, WasiSockets>(linker, T::sockets)?;
69
Ok(())
70
}
71
72