wasmtime_wasi

Function add_to_linker_async

source
pub fn add_to_linker_async<T: WasiView>(linker: &mut Linker<T>) -> Result<()>
Expand description

Add all WASI interfaces from this crate into the linker provided.

This function will add the async variant of all interfaces into the [Linker] provided. By async this means that this function is only compatible with Config::async_support(true). For embeddings with async support disabled see add_to_linker_sync instead.

This function will add all interfaces implemented by this crate to the [Linker], which corresponds to the wasi:cli/imports world supported by this crate.

ยงExample

use wasmtime::{Engine, Result, Store, Config};
use wasmtime::component::{ResourceTable, Linker};
use wasmtime_wasi::{WasiCtx, WasiView, WasiCtxBuilder};

fn main() -> Result<()> {
    let mut config = Config::new();
    config.async_support(true);
    let engine = Engine::new(&config)?;

    let mut linker = Linker::<MyState>::new(&engine);
    wasmtime_wasi::add_to_linker_async(&mut linker)?;
    // ... add any further functionality to `linker` if desired ...

    let mut builder = WasiCtxBuilder::new();

    // ... configure `builder` more to add env vars, args, etc ...

    let mut store = Store::new(
        &engine,
        MyState {
            ctx: builder.build(),
            table: ResourceTable::new(),
        },
    );

    // ... use `linker` to instantiate within `store` ...

    Ok(())
}

struct MyState {
    ctx: WasiCtx,
    table: ResourceTable,
}

impl WasiView for MyState {
    fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
    fn table(&mut self) -> &mut ResourceTable { &mut self.table }
}