wasm-bindgen
wasm-bindgen copied to clipboard
How to use generics in struct that implements wasm_bindgen?
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
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,
}
I don't actually think we have a tracking issue for this, so we can just leave this open.