Returning a Uint8Array from a [wasm_bind] function

I'm building a function in Rust that processes binary files: It accepts file contents as input, and returns totally new file contents.

I want to expose my Rust function in Javascript. It accepts a Uint8Array as an argument. I got that working very quickly.

However, I'm stumped how to return an Uint8Array.

While investigating, I tried copying the code from

pub fn return_boxed_number_slice() -> Box<[u32]> {

However, when I try to compile the code with wasm-pack, I see this error:

% wasm-pack build | more
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
   Compiling freesplit v0.1.0 (/Users/jeff/gitrepos/code/code/freesplit)

    Finished release [optimized] target(s) in 0.21s
[INFO]: Installing wasm-bindgen...
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[wasm-validator error in module] unexpected true: Exported global cannot be mutable, on 

My code doesn't export any globals, so I'm stumped.

Thanks for helping!

No idea what is going on with the global.

Uint8Array docs list:
impl<'a> From<&'a [u8]> for Uint8Array

so you can create it from any &[u8] (including anything that derefs to it like Vec) by calling .into().

pub fn return_uint8array() -> Uint8Array {
    let vec = (0..42).collect::<Vec<u32>>();

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.