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>,
}
js-sys
? It's the regular crate so you can add it to the list of your crate's dependencies and then use it in crate's code. – Topeelet array = js_sys::Uint32Array::new_with_length(42);
then put your values inside:array.fill(13, 0, 41);
and finally return it from your function. – TopeeVec<u32>
from somewhere else and expose it through wasm-bindgen. Usingjs_sys::Uint32Array
from the ground up may work for some, but I'm asking how to convert aVec
in this question. – Ita