I have a Vec
I would like to return and convert to a typed array with wasm-bindgen, ie, to turn a Vec<u32>
into a Uint32Array
. From my research it appears that wasm-bindgen cannot handle automatically converting these by itself right now (like it does for String
) and instead you must use the js-sys crate. I haven't found clear examples of how to use this crate however. It would be much appreciated if a clear simple example of how to use it could be provided.
For completeness' sake, it would be great if answers could explain both how to expose a function returning a Vec<u32>
, as well as a struct member, ie, how do you convert these definitions into something that will work:
#[wasm_bindgen]
pub fn my_func() -> Vec<u32> {
inner_func() // returns Vec<u32>
}
#[wasm_bindgen]
pub struct my_struct {
#[wasm_bindgen(readonly)]
pub my_vec: Vec<u32>,
}
As of wasm-bindgen v0.2.88 you can simply return a Vec<T>
(or a Box<[T]>
etc), and it will be automatically converted into a the corresponding typed array for JS to use. Likewise typed arrays can be passed in from JS, and converted to Box<[T]>
.
#[wasm_bindgen]
pub fn decode(buffer: Box<[u8]>) -> Vec<u8> { ... }