Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It's not possible to return Vec<T> if T only implements IntoWasmAbi #3824

Closed
bes opened this issue Feb 4, 2024 · 2 comments
Closed

It's not possible to return Vec<T> if T only implements IntoWasmAbi #3824

bes opened this issue Feb 4, 2024 · 2 comments
Labels

Comments

@bes
Copy link

bes commented Feb 4, 2024

Describe the Bug

It is not possible to return Vec<T: IntoWasmAbi>, but it is allowed to return just T.

Steps to Reproduce

Given this code

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

#[wasm_bindgen(js_name = TestClass)]
pub struct TestClass;

#[wasm_bindgen(js_class = TestClass)]
impl TestClass {
    #[wasm_bindgen]
    pub fn get_dummy_vec(&self) -> Vec<Dummy> {
        vec![Dummy {
            data: "abc".to_string(),
        }]
    }
}

#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Dummy {
    data: String,
}
error[E0277]: the trait bound `Dummy: JsObject` is not satisfied
   --> src/test_bindgen.rs:8:1
    |
8   | #[wasm_bindgen(js_class = TestClass)]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `JsObject` is not implemented for `Dummy`
    |
    = help: the following other types implement trait `JsObject`:
              Array
              ArrayBuffer
            and 71 others
    = note: required for `Dummy` to implement `WasmDescribeVector`
    = note: required for `Box<[Dummy]>` to implement `wasm_bindgen::describe::WasmDescribe`
    = note: 1 redundant requirement hidden
    = note: required for `Vec<Dummy>` to implement `wasm_bindgen::describe::WasmDescribe`
note: required by a bound in `ReturnWasmAbi`
   --> /Users/bes/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasm-bindgen-0.2.90/src/convert/traits.rs:177:26
    |
177 | pub trait ReturnWasmAbi: WasmDescribe {
    |                          ^^^^^^^^^^^^ required by this bound in `ReturnWasmAbi`
    = note: this error originates in the attribute macro `wasm_bindgen::prelude::__wasm_bindgen_class_marker` (in Nightly builds, run with -Z macro-backtrace for more info)

But, if we wrap the Vec in something that itself implements IntoWasmAbi everything works:

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

#[wasm_bindgen(js_name = TestClass)]
pub struct TestClass;

#[wasm_bindgen(js_class = TestClass)]
impl TestClass {
    // #[wasm_bindgen]
    // pub fn get_dummy_vec(&self) -> Vec<Dummy> {
    //     vec![Dummy {
    //         data: "abc".to_string(),
    //     }]
    // }
    //
    #[wasm_bindgen]
    pub fn get_dummy_collection(&self) -> DummyCollection {
        let dummies = vec![Dummy {
            data: "abc".to_string(),
        }];
        DummyCollection(dummies)
    }
}

#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Dummy {
    data: String,
}

#[derive(Tsify, Serialize, Deserialize)]
#[tsify(from_wasm_abi, into_wasm_abi)]
pub struct DummyCollection(Vec<Dummy>);

Expected Behavior

I expect it to be possible to return Vec<T> where T: IntoWasmAbi.

Actual Behavior

It isn't possible to return Vec<T> where T: IntoWasmAbi, but it is perfectly possible to return the Vec when a wrapper type is added.

Additional Context

See also

@bes bes added the bug label Feb 4, 2024
@daxpedda
Copy link
Collaborator

daxpedda commented Feb 5, 2024

Returning a Vec requires VectorIntoWasmAbi.

FWIW the whole wasm_bindgen::convert module is unstable.

Let me know if this doesn't solve your issue!

@daxpedda daxpedda closed this as completed Feb 5, 2024
@eneoli
Copy link

eneoli commented Aug 5, 2024

I realized too late that tsify has been inactive for about 9 months. There is a maintained fork here, but unfortunately no issue tracker (yet?).

tsify-next now implements VectorIntoWasmAbi and VectorFromWasmAbi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants