Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy_to_uninit() to all TypedArrays #4340

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading