Reputation: 1052
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>
?
Upvotes: 1
Views: 3279
Reputation: 1052
First you must convert it to Uint8Array
with Uint8Array::new
which takes a &JsValue
.
Then you can use:
Uint8Array::to_vec
to get a Vec<u8>
Uint8Array::copy_to
to fill an existing &mut [u8]
of the same sizelet buffer: JsValue = /* ... */;
let array = Uint8Array::new(&buffer);
let bytes: Vec<u8> = array.to_vec();
Upvotes: 3