Skip to content

Commit

Permalink
Add copy_to_uninit() to all TypedArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 8, 2024
1 parent 54f97c9 commit 43d882f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

## Unreleased

### Added

* Add a `copy_to_uninit()` method to all `TypedArray`s. It takes `&mut [MaybeUninit<T>]` and returns `&mut [T]`.
[#4340](https://github.com/rustwasm/wasm-bindgen/pull/4340)

### Changed

* Optional parameters are now typed as `T | undefined | null` to reflect the actual JS behavior.
Expand Down
19 changes: 18 additions & 1 deletion crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use core::convert::{self, Infallible, TryFrom};
use core::f64;
use core::fmt;
use core::iter::{self, Product, Sum};
use core::mem;
use core::mem::{self, MaybeUninit};
use core::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub};
use core::str;
use core::str::FromStr;
Expand Down Expand Up @@ -6336,6 +6336,23 @@ macro_rules! arrays {
unsafe { self.raw_copy_to_ptr(dst.as_mut_ptr()); }
}

/// Copy the contents of this JS typed array into the destination
/// Rust slice.
///
/// This function will efficiently copy the memory from a typed
/// array into this Wasm module's own linear memory, initializing
/// the memory destination provided.
///
/// # Panics
///
/// This function will panic if this typed array's length is
/// different than the length of the provided `dst` array.
pub fn copy_to_uninit<'dst>(&self, dst: &'dst mut [MaybeUninit<$ty>]) -> &'dst mut [$ty] {
core::assert_eq!(self.length() as usize, dst.len());
unsafe { self.raw_copy_to_ptr(dst.as_mut_ptr().cast()); }
unsafe { &mut *(dst as *mut [MaybeUninit<$ty>] as *mut [$ty]) }
}

/// Copy the contents of the source Rust slice into this
/// JS typed array.
///
Expand Down
13 changes: 13 additions & 0 deletions crates/js-sys/tests/wasm/TypedArray.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::mem::MaybeUninit;

use js_sys::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
Expand Down Expand Up @@ -174,6 +176,17 @@ fn copy_to() {
}
}

#[wasm_bindgen_test]
fn copy_to_uninit() {
let mut x = [MaybeUninit::uninit(); 10];
let array = Int32Array::new(&10.into());
array.fill(5, 0, 10);
let x = array.copy_to_uninit(&mut x);
for i in x.iter() {
assert_eq!(*i, 5);
}
}

#[wasm_bindgen_test]
fn copy_from() {
let x = [1, 2, 3];
Expand Down

0 comments on commit 43d882f

Please sign in to comment.