#javascript #buffer #vec

wasm-array-cp

Copy array buffer on the JS side

2 releases

0.1.1 May 24, 2025
0.1.0 May 23, 2025

#314 in WebAssembly

Download history 622/week @ 2025-06-02 1020/week @ 2025-06-09 1358/week @ 2025-06-16 1005/week @ 2025-06-23 1648/week @ 2025-06-30 1464/week @ 2025-07-07 1482/week @ 2025-07-14 1310/week @ 2025-07-21 1526/week @ 2025-07-28 1513/week @ 2025-08-04 1898/week @ 2025-08-11 1515/week @ 2025-08-18 1394/week @ 2025-08-25 1528/week @ 2025-09-01 1853/week @ 2025-09-08 3909/week @ 2025-09-15

8,718 downloads per month
Used in 132 crates (via sqlite-wasm-rs)

MIT license

19KB
308 lines

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

Dependencies

~0.8–1.5MB
~27K SLoC