Skip to main content

add_to_linker_async

Function add_to_linker_async 

Source
pub fn add_to_linker_async<T>(l: &mut Linker<T>) -> Result<()>
where T: WasiHttpView + WasiView + 'static,
Available on crate feature p2 only.
Expand description

Add all of the wasi:http/proxy world’s interfaces to a wasmtime::component::Linker.

This function will add the async variant of all interfaces into the Linker provided. For embeddings with async support disabled see add_to_linker_sync instead.

§Example

use wasmtime::{Engine, Result};
use wasmtime::component::{ResourceTable, Linker};
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
use wasmtime_wasi_http::{WasiHttpCtx, p2::{WasiHttpView, WasiHttpCtxView}};

fn main() -> Result<()> {
    let engine = Engine::default();

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

    Ok(())
}

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

impl WasiHttpView for MyState {
    fn http(&mut self) -> WasiHttpCtxView<'_> {
        WasiHttpCtxView {
            ctx: &mut self.http_ctx,
            table: &mut self.table,
            hooks: Default::default(),
        }
    }
}

impl WasiView for MyState {
    fn ctx(&mut self) -> WasiCtxView<'_> {
        WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }
    }
}