Wasm-bindgen: Passing Vec<&[u8]> to Rust code

I'm writing Rust code which right now accepts two files as Vec<u8>. I want to make it accept an arbitrary number of files, but using something like Vec<&[u8]> returns

the trait `JsObject` is not implemented for `&[u8]`

For Vec<Vec<u8>> I get a similar message:

the trait `JsObject` is not implemented for `Vec<u8>`

Is there any way I can make it work?

I saw this pull request and the assocated issues https://github.com/rustwasm/wasm-bindgen/pull/2614, however this doesn't help me understand how I could achieve this. Maybe I should try something like Vec<Uint8Array> instead? But then wasm-bindgen would first copy each Uint8Array into the Wasm memory and then I'd need to copy Uint8Array into a new Vec just so that I can read that data in Rust.

I'm using wasm-bindgen 0.2.75 and Rust 1.54.

For now I solved it with using Vec<js_sys::Uint8Array> as the argument type. Then in the function body I map over the vector and call .to_vec() on each element.

It's probably not the best solution, but hey, it works.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.