wasm-array-cp

Crates.iowasm-array-cp
lib.rswasm-array-cp
version0.1.1
created_at2025-05-23 12:00:01.830324+00
updated_at2025-05-24 01:50:03.830122+00
descriptionCopy array buffer on the JS side
homepage
repositoryhttps://github.com/Spxg/wasm-array-cp
max_upload_size
id1686220
size47,887
(Spxg)

documentation

README

wasm-array-cp

Crates.io

Directly using js-sys copy_from and copy_to to convert Js Array and Vec<T> is fragile.

There is a possibility that the memory will grow and the array buffer will be detached during copy.

So here we convert on the js side.

Usage

use js_sys::Uint8Array;
use wasm_array_cp::ArrayBufferCopy;

fn example() {
    let buf1 = vec![1, 2, 3, 4];
    let array: Uint8Array = ArrayBufferCopy::from_slice(&buf1);
    let buf2 = ArrayBufferCopy::to_vec(&array);
    assert_eq!(buf1, buf2);

    let mut buf3 = vec![0; 2];
    ArrayBufferCopy::copy_to(&array.subarray(0, 2), &mut buf3);
    assert_eq!(buf3, vec![1, 2]);

    let buf4 = Uint8Array::new_with_length(3);
    ArrayBufferCopy::copy_from(&buf4.subarray(1, 3), &buf3);
    assert!(buf4.get_index(0) == 0);
    assert!(buf4.get_index(1) == 1);
    assert!(buf4.get_index(2) == 2);
}
Commit count: 5

cargo fmt