pub fn add_to_linker_async<T: Send + 'static>(
linker: &mut Linker<T>,
f: impl Fn(&mut T) -> &mut WasiP1Ctx + Copy + Send + Sync + 'static,
) -> Result<()>p1 only.Expand description
Adds asynchronous versions of all WASIp1 functions to the
wasmtime::Linker provided.
This method will add WASIp1 functions to linker. Access to WasiP1Ctx
is provided with f by projecting from the store-local state of T to
WasiP1Ctx. The closure f is invoked every time a WASIp1 function is
called to get access to WasiP1Ctx from T. The returned WasiP1Ctx is
used to implement I/O and controls what each function will return.
It’s recommended that WasiP1Ctx is stored as a field in T or that T = WasiP1Ctx itself. The closure f should be a small projection (e.g. &mut arg.field) or something otherwise “small” as it will be executed every time
a WASI call is made.
Note that this function is intended for use with
Config::async_support(true). If you’re looking for a synchronous version
see add_to_linker_sync.
§Examples
If the T in Linker<T> is just WasiP1Ctx:
use wasmtime::{Result, Linker, Engine, Config};
use wasmtime_wasi::p1::{self, WasiP1Ctx};
fn main() -> Result<()> {
let mut config = Config::new();
config.async_support(true);
let engine = Engine::new(&config)?;
let mut linker: Linker<WasiP1Ctx> = Linker::new(&engine);
p1::add_to_linker_async(&mut linker, |cx| cx)?;
// ... continue to add more to `linker` as necessary and use it ...
Ok(())
}If the T in Linker<T> is custom state:
use wasmtime::{Result, Linker, Engine, Config};
use wasmtime_wasi::p1::{self, WasiP1Ctx};
struct MyState {
// .. other custom state here ..
wasi: WasiP1Ctx,
}
fn main() -> Result<()> {
let mut config = Config::new();
config.async_support(true);
let engine = Engine::new(&config)?;
let mut linker: Linker<MyState> = Linker::new(&engine);
p1::add_to_linker_async(&mut linker, |cx| &mut cx.wasi)?;
// ... continue to add more to `linker` as necessary and use it ...
Ok(())
}