Skip to content

Commit 5690223

Browse files
committed
Add copy_to_uninit() to all TypedArrays
1 parent 54f97c9 commit 5690223

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
## Unreleased
55

6+
### Added
7+
8+
* Add a `copy_to_uninit()` method to all `TypedArray`s. It takes `&mut [MaybeUninit<T>]` and returns `&mut [T]`.
9+
[#4340](https://github.com/rustwasm/wasm-bindgen/pull/4340)
10+
611
### Changed
712

813
* Optional parameters are now typed as `T | undefined | null` to reflect the actual JS behavior.

crates/js-sys/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use core::convert::{self, Infallible, TryFrom};
3232
use core::f64;
3333
use core::fmt;
3434
use core::iter::{self, Product, Sum};
35-
use core::mem;
35+
use core::mem::{self, MaybeUninit};
3636
use core::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub};
3737
use core::str;
3838
use core::str::FromStr;
@@ -6336,6 +6336,23 @@ macro_rules! arrays {
63366336
unsafe { self.raw_copy_to_ptr(dst.as_mut_ptr()); }
63376337
}
63386338

6339+
/// Copy the contents of this JS typed array into the destination
6340+
/// Rust slice.
6341+
///
6342+
/// This function will efficiently copy the memory from a typed
6343+
/// array into this Wasm module's own linear memory, initializing
6344+
/// the memory destination provided.
6345+
///
6346+
/// # Panics
6347+
///
6348+
/// This function will panic if this typed array's length is
6349+
/// different than the length of the provided `dst` array.
6350+
pub fn copy_to_uninit<'dst>(&self, dst: &'dst mut [MaybeUninit<$ty>]) -> &'dst mut [$ty] {
6351+
core::assert_eq!(self.length() as usize, dst.len());
6352+
unsafe { self.raw_copy_to_ptr(dst.as_mut_ptr().cast()); }
6353+
unsafe { &mut *(dst as *mut [MaybeUninit<$ty>] as *mut [$ty]) }
6354+
}
6355+
63396356
/// Copy the contents of the source Rust slice into this
63406357
/// JS typed array.
63416358
///

0 commit comments

Comments
 (0)