将 JsValue 转换为 Vec<u8>

时间:2021-05-10 02:28:30

标签: rust arraybuffer typed-arrays wasm-bindgen typedarray

我需要使用 web_sys::Blob::array_buffer 返回解析为 PromiseArrayBufferPromise 目前仅在 Rust 中解析为 JsValue。我如何将其转换为 Vec<u8>

1 个答案:

答案 0 :(得分:0)

首先,您必须使用带 Uint8ArrayUint8Array::new 将其转换为 &JsValue

然后你可以使用:

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