Stream: git-wasmtime

Topic: wasmtime / issue #3200 Use struct as argument


view this post on Zulip Wasmtime GitHub notifications bot (Aug 18 2021 at 08:52):

silence-coding opened issue #3200:

How to use struct as a parameter? I don't find any useful information in the example.

#[derive(Debug, Default)]
#[repr(C)]
pub struct Msg {
    id: u64,
    data: String,
}

use model::Msg;

#[no_mangle]
pub extern "C" fn print_msg(msg: Msg) {
    println!("Hello: {:?}", msg);
}

pub fn test_struct() -> Result<(), Box<dyn Error>> {
    let engine = Engine::default();
    let mut linker = Linker::new(&engine);
    wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
    let module = Module::from_file(&engine, "target/wasm32-wasi/debug/struct.wasm")?;
    let wasi = WasiCtxBuilder::new()
        .inherit_stdio()
        .inherit_args()?
        .build();
    let mut store = Store::new(&engine, wasi);
    let instance = linker.instantiate(&mut store, &module)?;
    let greet = instance.get_typed_func::<Msg, (), _>(&mut store, "print_msg")?;
    greet.call(&mut store, Msg::default())?;
    Ok(())
}

view this post on Zulip Wasmtime GitHub notifications bot (Aug 18 2021 at 08:52):

silence-coding edited issue #3200:

How to use struct as a parameter? I don't find any useful information in the example.

#[derive(Debug, Default)]
#[repr(C)]
pub struct Msg {
    id: u64,
    data: String,
}

use model::Msg;

#[no_mangle]
pub extern "C" fn print_msg(msg: Msg) {
    println!("Hello: {:?}", msg);
}

pub fn test_struct() -> Result<(), Box<dyn Error>> {
    let engine = Engine::default();
    let mut linker = Linker::new(&engine);
    wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
    let module = Module::from_file(&engine, "target/wasm32-wasi/debug/struct.wasm")?;
    let wasi = WasiCtxBuilder::new()
        .inherit_stdio()
        .inherit_args()?
        .build();
    let mut store = Store::new(&engine, wasi);
    let instance = linker.instantiate(&mut store, &module)?;
    let greet = instance.get_typed_func::<Msg, (), _>(&mut store, "print_msg")?;
    greet.call(&mut store, Msg::default())?;
    Ok(())
}

error[E0277]: the trait bound `Msg: WasmTy` is not satisfied
  --> run\src\struct2.rs:17:26
   |
17 |     let greet = instance.get_typed_func::<Msg, (), _>(&mut store, "print_msg")?;
   |                          ^^^^^^^^^^^^^^ the trait `WasmTy` is not implemented for `Msg`
   |
   = note: required because of the requirements on the impl of `WasmParams` for `Msg`


Last updated: Oct 23 2024 at 20:03 UTC