std::io::Read + WASM

I want to parse a file's contents with a library that expects a std::io::Read. What's the most efficient way to do this in WASM-land?

I can use Node's readFileSync to get a Buffer object, but is there a better approach to getting a Read-able object than copying its entire contents into a byte array?

I don't think you'll be able to use the std::io::Read trait with Node types because Read is a synchronous pull-based API (i.e. the caller directly calls a read() method to read more), while JavaScript uses asynchronous push-based APIs (it does the reading in the background and will trigger a callback when it's done).

What I'd do is use fs.readFile() to ask Node to read the entire file into a buffer in the background, then pass that buffer to the WebAssembly code once it's done.

You could imagine the JavaScript looking something like this:

fs.readFile('/etc/passwd', (err, data) => {
  if (err) throw err;
  wasm.doSomethingWithData(data);
});

And on the Rust side:

#[wasm_bindgen]
pub fn doSomethingWithData(data: &[u8])  {
  ...
}

Don't forget that &[u8] also implements Read, so you shouldn't need to do anything special to pass it to your library code.

1 Like

Thanks for the response. In your example, isn't data a Buffer rather than a byte array? Or is there some automatic conversion?

It depends. I think wasm-bindgen automatically turns a Uint8Array into a &[u8].