wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

How to use generics in struct that implements wasm_bindgen?

Open uros-5 opened this issue 2 years ago • 2 comments

Summary

So here is small example. Let's say there is Position trait.

#[wasm_bindgen]
pub struct WasmPosition<P: Position> {
    s: P,
}

Compiler throws errors:

structs with #[wasm_bindgen] cannot have lifetime or type parameters currently

uros-5 avatar Feb 15 '23 13:02 uros-5

May be you can use enum:

pub enum Position {
    Foo(Position1),
    Bar(Position2),
}
#[wasm_bindgen]
pub struct WasmPosition {
    s: Position
}

or dyn trait

#[wasm_bindgen]
pub struct WasmPosition {
    s: Box<dyn Position>,
}

// reference to wasm exports is static
#[wasm_bindgen]
pub struct WasmPosition {
    s: &'static dyn Position,
}

thy486 avatar Sep 23 '23 04:09 thy486

I don't actually think we have a tracking issue for this, so we can just leave this open.

daxpedda avatar Sep 23 '23 07:09 daxpedda