Dull Bananas
Dull Bananas

Reputation: 1052

Converting JsValue to Vec<u8>

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

Answers (1)

Dull Bananas
Dull Bananas

Reputation: 1052

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();

Upvotes: 3

Related Questions