Open
Description
Summary
In the code below, how can I cast the JsValue
to Foo
?
#[wasm_bindgen]
pub struct Foo(u8); // Assume this can't be serialized or deserialized
#[wasm_bindgen]
pub fn foo_downcast(foo: JsValue) {
let foo: Foo = todo!();
// ...
}
Additional Details
From<Foo> for JsValue
is implemented automatically, so I'd expect something like TryInto<Foo> for JsValue
to be implemented too, but I couldn't find anything.
Reasons why you'd want to do this
- Passing a vector or slice of objects to an exported function (currently
Vec<Foo>
and&[Foo]
are unsupported, butVec<JsValue>
and&[JsValue]
are supported, I think) - Having automatic casting rules for parameters of exported functions (automatically parsing strings if the parameter is a string, etc)