Skip to content

Commit 1f51831

Browse files
Pauanalexcrichton
authored andcommitted
Adding in to_vec method for typed arrays (#1844)
* Adding in to_vec method for typed arrays * Fixing type error
1 parent 6159d50 commit 1f51831

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

crates/js-sys/src/lib.rs

+26-15
Original file line numberDiff line numberDiff line change
@@ -4657,7 +4657,7 @@ pub fn global() -> Object {
46574657
}
46584658

46594659
macro_rules! arrays {
4660-
($(#[doc = $ctor:literal] #[doc = $mdn:literal] $name:ident: $ty:ident,)*) => ($(
4660+
($(#[doc = $ctor:literal] #[doc = $mdn:literal] $name:ident: $ty:ident => $zero:literal,)*) => ($(
46614661
#[wasm_bindgen]
46624662
extern "C" {
46634663
#[wasm_bindgen(extends = Object)]
@@ -4792,6 +4792,14 @@ macro_rules! arrays {
47924792
)
47934793
}
47944794

4795+
fn raw_copy_to(&self, dst: &mut [$ty]) {
4796+
let buf = wasm_bindgen::memory();
4797+
let mem = buf.unchecked_ref::<WebAssembly::Memory>();
4798+
let all_wasm_memory = $name::new(&mem.buffer());
4799+
let offset = dst.as_ptr() as usize / mem::size_of::<$ty>();
4800+
all_wasm_memory.set(self, offset as u32);
4801+
}
4802+
47954803
/// Copy the contents of this JS typed array into the destination
47964804
/// Rust slice.
47974805
///
@@ -4805,11 +4813,14 @@ macro_rules! arrays {
48054813
/// different than the length of the provided `dst` array.
48064814
pub fn copy_to(&self, dst: &mut [$ty]) {
48074815
assert_eq!(self.length() as usize, dst.len());
4808-
let buf = wasm_bindgen::memory();
4809-
let mem = buf.unchecked_ref::<WebAssembly::Memory>();
4810-
let all_wasm_memory = $name::new(&mem.buffer());
4811-
let offset = dst.as_ptr() as usize / mem::size_of::<$ty>();
4812-
all_wasm_memory.set(self, offset as u32);
4816+
self.raw_copy_to(dst);
4817+
}
4818+
4819+
/// Efficiently copies the contents of this JS typed array into a new Vec.
4820+
pub fn to_vec(&self) -> Vec<$ty> {
4821+
let mut output = vec![$zero; self.length() as usize];
4822+
self.raw_copy_to(&mut output);
4823+
output
48134824
}
48144825
}
48154826

@@ -4826,37 +4837,37 @@ macro_rules! arrays {
48264837
arrays! {
48274838
/// `Int8Array()`
48284839
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array
4829-
Int8Array: i8,
4840+
Int8Array: i8 => 0,
48304841

48314842
/// `Int16Array()`
48324843
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array
4833-
Int16Array: i16,
4844+
Int16Array: i16 => 0,
48344845

48354846
/// `Int32Array()`
48364847
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array
4837-
Int32Array: i32,
4848+
Int32Array: i32 => 0,
48384849

48394850
/// `Uint8Array()`
48404851
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
4841-
Uint8Array: u8,
4852+
Uint8Array: u8 => 0,
48424853

48434854
/// `Uint8ClampedArray()`
48444855
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray
4845-
Uint8ClampedArray: u8,
4856+
Uint8ClampedArray: u8 => 0,
48464857

48474858
/// `Uint16Array()`
48484859
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array
4849-
Uint16Array: u16,
4860+
Uint16Array: u16 => 0,
48504861

48514862
/// `Uint32Array()`
48524863
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
4853-
Uint32Array: u32,
4864+
Uint32Array: u32 => 0,
48544865

48554866
/// `Float32Array()`
48564867
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array
4857-
Float32Array: f32,
4868+
Float32Array: f32 => 0.0,
48584869

48594870
/// `Float64Array()`
48604871
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
4861-
Float64Array: f64,
4872+
Float64Array: f64 => 0.0,
48624873
}

crates/js-sys/tests/wasm/TypedArray.rs

+7
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,10 @@ fn copy_to() {
131131
assert_eq!(*i, 5);
132132
}
133133
}
134+
135+
#[wasm_bindgen_test]
136+
fn to_vec() {
137+
let array = Int32Array::new(&10.into());
138+
array.fill(5, 0, 10);
139+
assert_eq!(array.to_vec(), vec![5, 5, 5, 5, 5, 5, 5, 5, 5, 5]);
140+
}

0 commit comments

Comments
 (0)