Using tagged unions from JavaScript with wasm-bindgen

130 Views Asked by At

I have a JavaScript object type where the kind field determines what other fields are present. Examples:

{ kind: 0, name: "foo" }
{ kind: 1, name: "foo", value: 69 }
{ kind: 2 }

I put it into an extern block to use it in Rust:

#[wasm_bindgen]
extern "C" {
    type Foo;
}

I need to access a Foo value's fields in this function:

#[wasm_bindgen]
fn use_foo(foo: Foo) {
    // ...
}

How do I do this in a type-safe way?

0

There are 0 best solutions below