Converting JsValue to Vec<u8>

2.6k views Asked by At

I need to use web_sys::Blob::array_buffer which returns a Promise that resolves to an ArrayBuffer. Promise currently only resolves to JsValue in Rust. How do I convert that to Vec<u8>?

1

There are 1 answers

0
Dull Bananas On

First you must convert it to Uint8Array with Uint8Array::new which takes a &JsValue.

Then you can use:

let buffer: JsValue = /* ... */;
let array = Uint8Array::new(&buffer);
let bytes: Vec<u8> = array.to_vec();