pub trait WasiHttpNamedView: Send + 'static {
// Required method
fn http(&mut self, id: NamedId) -> WasiHttpCtxView<'_>;
}Expand description
A trait used to look up a specific wasi:http context for a named import.
This trait is used in conjunction with the named_imports bindings
generated for all wasi:http interfaces. The purpose of this trait is for
embedders to define how a NamedId maps to a particular wasi:http
context, here returned as WasiHttpCtxView. Embedders are responsible
for assigning meaning to NamedId values themselves. These IDs are
assigned when add_named_to_linker is called, for example, as the
lookup argument to that function.
When using add_named_to_linker it’s sufficient to implement this trait
for the T in Store<T>.
When using add_to_linker in the generated bindings::named_imports
module then values implementing this live within the T of Store<T>, and
be temporarily referenced in WasiCtxNamedView where internally that’ll
hold WasiCtxNamedView(&mut your_type).
§Examples
use wasmtime::component::{Linker, Component, ResourceTable};
use wasmtime::{Engine, Result};
use wasmtime_wasi::NamedId;
use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpCtxView, WasiHttpNamedView};
use std::collections::HashMap;
struct MyStoreState {
table: ResourceTable,
states: HashMap<NamedId, WasiHttpCtx>,
}
fn main() -> Result<()> {
let engine = Engine::default();
let mut linker = Linker::new(&engine);
let component = Component::new(&engine, "(component)")?;
let mut name_map = HashMap::new();
wasmtime_wasi_http::p3::add_named_to_linker::<MyStoreState>(
&mut linker,
&component,
|_, name| {
let len = name_map.len();
Ok(NamedId(*name_map.entry(name.to_string()).or_insert(len)))
},
)?;
Ok(())
}
impl WasiHttpNamedView for MyStoreState {
fn http(&mut self, id: NamedId) -> WasiHttpCtxView<'_> {
let ctx = self.states.get_mut(&id).expect("state for id");
WasiHttpCtxView {
ctx,
table: &mut self.table,
hooks: Default::default(),
}
}
}Required Methods§
Sourcefn http(&mut self, id: NamedId) -> WasiHttpCtxView<'_>
fn http(&mut self, id: NamedId) -> WasiHttpCtxView<'_>
Looks up the WasiHttpCtxView for the given NamedId.
This method will resolve the id specified to a specific HTTP context
that is available to be used. Note that this method is specifically
infallible meaning that an HTTP context must be returned and this cannot
generate a trap or panic or similar.
Embedders are responsible for allocating NamedId and assigning
meaning to ids. When a Linker is populated embedders will have the
ability to generate a NamedId for all imports found, and then that
embedder-allocated id is then passed back here when the corresponding
imported function is invoked.
Note that the ResourceTable referenced in the returned
WasiHttpCtxView need not be unique. It’s ok to use the same
ResourceTable for all imports. This is not a guest-visible
abstraction and just helps the host allocate and manage state.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".