Skip to content

No safe way to create an ArrayBuffer from a &[u8] #1303

Closed
@tomaka

Description

@tomaka

Motivation

I'm trying to use an existing JS library (from NPM). One of the methods in this library's API expects to get passed an ArrayBuffer object. Therefore, I need to be able to generate an ArrayBuffer from the Rust code.

It is already possible to do so by doing:

let arr = unsafe { Uint8Array::view(my_buf) };
let arr_clone = Uint8Array::new(arr).buffer();

However that requires using unsafe code.

Proposed Solution

Add an ArrayBuffer::from_data(&[u8]) function in order to move the safety issues within wasm-bindgen.
Similarly, we can also add similar from_data methods to the Uint8Array types and similar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    js-sysIssues related to the `js-sys` crate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions