Skip to content

Support VectorIntoJsValue #66

@Hinton

Description

@Hinton

When using async in combination with vectors, wasm-bindgen requires that VectorIntoJsValue is implemented for the inner type of the vector.

It would be useful if tsify could automatically implement the VectorIntoJsValue trait in the macros, similar to how wasm-bindgen does it, https://github.com/rustwasm/wasm-bindgen/blob/c35cc9369d5e0dc418986f7811a0dd702fb33ef9/crates/backend/src/codegen.rs#L457-L462.

Currently the following code snippet produces a error[E0277]: the trait bound TestStruct: JsObject is not satisfied error.

use serde::{Deserialize, Serialize};
use tsify::Tsify;
use wasm_bindgen::prelude::*;

#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
struct TestStruct {}

#[wasm_bindgen]
pub async fn test() -> Vec<TestStruct> {
    vec![TestStruct {}]
}

Adding a manual implementation of VectorIntoJsValue resolves the compilation error.

impl wasm_bindgen::__rt::VectorIntoJsValue for TestStruct {
    fn vector_into_jsvalue(
        vector: wasm_bindgen::__rt::std::boxed::Box<[Self]>,
    ) -> wasm_bindgen::JsValue {
        wasm_bindgen::__rt::js_value_vector_into_jsvalue(vector)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions