Stream: wasmtime

Topic: ✔ linking single binding


view this post on Zulip Trent (Aug 30 2024 at 03:14):

Trying out wasmtime for the first time.

I am trying to satisfy this error "component imports instance wasi:cli/environment@0.2.0" by calling wasmtime_wasi::bindings::cli::environment::add_to_linker in my binary, but I can't get the types to work out. I tried using the type_annotate trick shown https://docs.rs/wasmtime-wasi/latest/src/wasmtime_wasi/lib.rs.html#380 but no dice.

I still get this

expected `{closure@main.rs:13:47}` to be a closure that returns `&mut _`, but it returns `WasiImpl<&mut MyState>`
expected mutable reference `&mut _`
              found struct `wasmtime_wasi::WasiImpl<&mut MyState>`

view this post on Zulip Alex Crichton (Aug 30 2024 at 04:20):

does the code example on add_to_linker_sync help? Or are you trying to do something different than that?

view this post on Zulip Trent (Aug 30 2024 at 07:49):

Not really. I only want to link the feature I want exposed, not everything in https://docs.rs/wasmtime-wasi/latest/src/wasmtime_wasi/lib.rs.html#382-408. The API for enabling all the WASI interfaces is fine, but the API for enabling individually requires a second param that is pretty obscure.

My code looks indentical to the source of the function you linked, but it still doesn't want to compile, giving me the above error.

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
    }
}

fn type_annotate<T: WasiView, F>(val: F) -> F
where
    F: Fn(&mut T) -> WasiImpl<&mut T>,
{
    val
}

let closure = type_annotate::<MyState, _>(|t| WasiImpl(t));
wasmtime_wasi::bindings::cli::environment::add_to_linker(&mut linker, closure);

view this post on Zulip Alex Crichton (Aug 30 2024 at 14:44):

Oh I think you need to call add_to_linker_get_host instead of add_to_linker?

view this post on Zulip Trent (Aug 31 2024 at 01:16):

Oh, well that fixed it, don't know how I missed that :sweat_smile: cheers

view this post on Zulip Notification Bot (Sep 08 2024 at 13:47):

Till Schneidereit has marked this topic as resolved.


Last updated: Oct 23 2024 at 20:03 UTC