Search code examples
rusttyped-arrayswasm-bindgen

How to use JS TypedArray as Rust BufRead/Read without memory copying?


I need Uint8Array in the form of BufRead or Read.

I could just copy the whole memory using something like to_vec and construct some type of BufRead/Read but I don't want to because the array I'm dealing with can be huge (50~100mb)

I think in order to do that I have to get the address to the mutable continuous memory block, probably in type of *mut [u8]. (I'm not familiar with Rust yet so the syntax might not be right but hope you get the idea). But I can't find the suitable method from Uint8Array doc.

Is it possible? If so, how can I do it?


Solution

  • You have to copy the data. WebAssembly does not directly have access to memory in the JavaScript environment and must be copied over. Using to_vec is the way to go.