-
With #111 we can now return a #[wasm_bindgen]
struct Thing(u32);
#[wasm_bindgen]
pub fn get_numbers() -> Vec<Thing> {
vec![Thing(1), Thing(2), Thing(3)]
} But it does not work if #[derive(serde::Serialize, serde::Deserialize, tsify::Tsify)]
#[tsify(into_wasm_abi)]
struct Thing(u32);
#[wasm_bindgen]
/*^^^^^^^^^^^^^^^ the trait `JsObject` is not implemented for `Thing`
= help: the following other types implement trait `JsObject`:
js_sys::Array
js_sys::ArrayBuffer
web_sys::features::gen_AudioBuffer::AudioBuffer
js_sys::SharedArrayBuffer
web_sys::features::gen_AudioBufferSourceNode::AudioBufferSourceNode
js_sys::BigInt
web_sys::features::gen_AudioContext::AudioContext
web_sys::features::gen_AudioDestinationNode::AudioDestinationNode
and 164 others
= note: required for `Thing` to implement `WasmDescribeVector`
= note: required for `Box<[Thing]>` to implement `WasmDescribe`
= note: 1 redundant requirement hidden
= note: required for `Vec<Thing>` to implement `WasmDescribe`
note: required by a bound in `ReturnWasmAbi`
--> wasm-bindgen-0.2.88\src\convert\traits.rs:176:26
|
176 | pub trait ReturnWasmAbi: WasmDescribe {
| ^^^^^^^^^^^^ required by this bound in `ReturnWasmAbi`
= note: this error originates in the attribute macro `wasm_bindgen` (in Nightly builds, run with -Z macro-backtrace for more info)
*/
pub fn get_numbers() -> Vec<Thing> {
vec![Thing(1), Thing(2), Thing(3)]
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
For now my best bet is to relay the array with a struct: #[derive(serde::Serialize, serde::Deserialize, tsify::Tsify)]
#[tsify(into_wasm_abi)]
pub struct ThingCollection(Vec<Thing>);
pub fn get_numbers() -> ThingCollection {
ThingCollection(vec![Thing(1), Thing(2), Thing(3)])
} This works very well as export type ThingCollection = Thing[]; But it would be even better if we can remove this indirection. |
Beta Was this translation helpful? Give feedback.
-
You might want to check out the tsify-next crate. It replaces the currently unmaintained tsify crate and recently added support for returning Vectors and passing them as parameters. |
Beta Was this translation helpful? Give feedback.
You might want to check out the tsify-next crate. It replaces the currently unmaintained tsify crate and recently added support for returning Vectors and passing them as parameters.