Add conversions between typed arrays and Rust

For all typed arrays, this commit adds:

* `TypedArray::view(src: &[Type])`
* `TypedArray::copy_to(&self, dst: &mut [Type])`

The `view` function is unsafe because it doesn't provide any guarantees
about lifetimes or mutability. The `copy_to` function is, however, safe.

Closes #811
This commit is contained in:
Alex Crichton
2019-01-04 10:40:11 -08:00
parent 1758c8d5af
commit 2d7f601696
2 changed files with 133 additions and 0 deletions

View File

@ -100,3 +100,24 @@ macro_rules! test_slice {
fn new_slice() {
each!(test_slice);
}
#[wasm_bindgen_test]
fn view() {
let x = [1, 2, 3];
let array = unsafe { Int32Array::view(&x) };
assert_eq!(array.length(), 3);
array.for_each(&mut |x, i, _| {
assert_eq!(x, (i + 1) as i32);
});
}
#[wasm_bindgen_test]
fn copy_to() {
let mut x = [0; 10];
let array = Int32Array::new(&10.into());
array.fill(5, 0, 10);
array.copy_to(&mut x);
for i in x.iter() {
assert_eq!(*i, 5);
}
}