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 5690223
Show file tree
Hide file tree
Showing 2 changed files with 23 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

0 comments on commit 5690223

Please sign in to comment.