Function wasmtime_wasi_http::proxy::add_to_linker

source ·
pub fn add_to_linker<T>(l: &mut Linker<T>) -> Result<()>
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. By async this means that this function is only compatible with Config::async_support(true). For embeddings with async support disabled see sync::add_to_linker instead.

§Example

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

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_http::proxy::add_to_linker(&mut linker)?;
    // ... add any further functionality to `linker` if desired ...

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

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

    Ok(())
}

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

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