pub trait WasiClocksNamedView: Send + 'static {
// Required method
fn clocks(&mut self, id: NamedId) -> WasiClocksCtxView<'_>;
}Expand description
A trait used to look up a specific wasi:clocks context for a named
import.
This trait is used in conjunction with the named_imports bindings
generated for all WASI interfaces. The purpose of this trait is for
embedders to define how a NamedId maps to a particular wasi:clocks
context, here returned as WasiClocksCtxView. 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>. You can also instead implement the
WasiNamedView trait for T which implies an implementation of this
trait.
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, WasiCtxNamedView};
use wasmtime_wasi::clocks::*;
use std::collections::HashMap;
struct MyStoreState {
table: ResourceTable,
states: HashMap<NamedId, WasiClocksCtx>,
}
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::p3::clocks::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 WasiClocksNamedView for MyStoreState {
fn clocks(&mut self, id: NamedId) -> WasiClocksCtxView<'_> {
let ctx = self.states.get_mut(&id).expect("state for id");
WasiClocksCtxView {
table: &mut self.table,
ctx,
}
}
}Required Methods§
Sourcefn clocks(&mut self, id: NamedId) -> WasiClocksCtxView<'_>
fn clocks(&mut self, id: NamedId) -> WasiClocksCtxView<'_>
Looks up the WasiClocksCtxView for the given NamedId.
This method will resolve the id specified to a specific clocks
context that is available to be used. Note that this method is
specifically infallible meaning that a clocks 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.
It’s strongly recommended, though not always required, that the
ResourceTable in the returned WasiClocksCtxView is the same
table that the default context, WasiClocksView::clocks, uses.
Resources created through a named import can be used through interfaces
bound to the default context (and vice versa), and with separate tables
those lookups fail or find another context’s resources.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".