Skip to content

Downcast a JsValue to an exported struct #2231

Open
@io12

Description

@io12

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

  1. Passing a vector or slice of objects to an exported function (currently Vec<Foo> and &[Foo] are unsupported, but Vec<JsValue> and &[JsValue] are supported, I think)
  2. Having automatic casting rules for parameters of exported functions (automatically parsing strings if the parameter is a string, etc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions