Stream: wasmtime

Topic: ✔ static caller in Func::wrap


view this post on Zulip Mossaka (Joe) (Jun 26 2022 at 22:17):

I had the following code to link a host function

let host_hello = Func::wrap(&mut store, |caller: Caller<'static, u32>, param: i32| {
        let t = thread::spawn(move || {
            println!("Got {} from WebAssembly", param);
            println!("my host state is: {}", caller.data());
        });
        t.join().unwrap();
  });

and it gives an error saying that

error[E0277]: the trait bound `[closure@src/main.rs:25:45: 31:6]: IntoFunc<{integer}, _, _>` is not satisfied
   --> src/main.rs:25:22
    |
25  |     let host_hello = Func::wrap(&mut store, |caller: Caller<'static, u32>, param: i32| {
    |                      ^^^^^^^^^^ the trait `IntoFunc<{integer}, _, _>` is not implemented for `[closure@src/main.rs:25:45: 31:6]`

I read the documentation of IntoFunc, which says that

This trait should not be implemented by external users, it’s only intended as an implementation detail of this crate.

So I am wondering if it is possible to have a static caller in host function?

view this post on Zulip fitzgen (he/him) (Jun 27 2022 at 15:51):

it's not possible because the Caller is only valid while your wrapped function is called, and if it was 'static then you could store it in some global state that outlives that call

if you want to be able to spawn threads that use the Caller, you can use scoped threads (either from crossbeam or the upcoming support for scoped threads in std)

view this post on Zulip Mossaka (Joe) (Jun 27 2022 at 16:17):

Ahh thanks :pray: I didn't know scopethread like crossbeam existed.

view this post on Zulip Notification Bot (Jun 27 2022 at 16:17):

mossaka has marked this topic as resolved.


Last updated: Dec 23 2024 at 14:03 UTC