diff options
| author | The8472 <git@infinite-source.de> | 2021-08-05 21:21:52 +0200 |
|---|---|---|
| committer | The8472 <git@infinite-source.de> | 2021-08-05 21:21:52 +0200 |
| commit | 1c21373b50f39269f553e736933c70841f419d2e (patch) | |
| tree | eeb9db5bfbc1546c9b583bb300fd9c3525ced91c | |
| parent | 55def120a4d54123625ad05c4d6384c5044642f1 (diff) | |
| download | rust-1c21373b50f39269f553e736933c70841f419d2e.tar.gz rust-1c21373b50f39269f553e736933c70841f419d2e.zip | |
add Box::try_new_uninit_slice for symmetry
| -rw-r--r-- | library/alloc/src/boxed.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 6b2f52eb511..72216852376 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -589,6 +589,39 @@ impl<T> Box<[T]> { unsafe { RawVec::with_capacity_zeroed(len).into_box(len) } } + /// Constructs a new boxed slice with uninitialized contents. Returns an error if + /// the allocation fails + /// + /// # Examples + /// + /// ``` + /// #![feature(allocator_api, new_uninit)] + /// + /// let mut values = Box::<[u32]>::try_new_uninit_slice(3)?; + /// let values = unsafe { + /// // Deferred initialization: + /// values[0].as_mut_ptr().write(1); + /// values[1].as_mut_ptr().write(2); + /// values[2].as_mut_ptr().write(3); + /// values.assume_init() + /// }; + /// + /// assert_eq!(*values, [1, 2, 3]); + /// # Ok::<(), std::alloc::AllocError>(()) + /// ``` + #[unstable(feature = "allocator_api", issue = "32838")] + #[inline] + pub fn try_new_uninit_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> { + unsafe { + let layout = match Layout::array::<mem::MaybeUninit<T>>(len) { + Ok(l) => l, + Err(_) => return Err(AllocError), + }; + let ptr = Global.allocate(layout)?; + Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len)) + } + } + /// Constructs a new boxed slice with uninitialized contents, with the memory /// being filled with `0` bytes. Returns an error if the allocation fails /// |
