Skip to content

Commit ab204c5

Browse files
committed
Add {Box,Rc,Arc}::new_zeroed_slice
1 parent 361f668 commit ab204c5

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

library/alloc/src/boxed.rs

+23
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,29 @@ impl<T> Box<[T]> {
273273
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
274274
unsafe { RawVec::with_capacity(len).into_box(len) }
275275
}
276+
277+
/// Constructs a new boxed slice with uninitialized contents, with the memory
278+
/// being filled with `0` bytes.
279+
///
280+
/// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
281+
/// of this method.
282+
///
283+
/// # Examples
284+
///
285+
/// ```
286+
/// #![feature(new_uninit)]
287+
///
288+
/// let values = Box::<[u32]>::new_zeroed_slice(3);
289+
/// let values = unsafe { values.assume_init() };
290+
///
291+
/// assert_eq!(*values, [0, 0, 0])
292+
/// ```
293+
///
294+
/// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed
295+
#[unstable(feature = "new_uninit", issue = "63291")]
296+
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
297+
unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
298+
}
276299
}
277300

278301
impl<T> Box<mem::MaybeUninit<T>> {

library/alloc/src/rc.rs

+34
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,40 @@ impl<T> Rc<[T]> {
469469
pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
470470
unsafe { Rc::from_ptr(Rc::allocate_for_slice(len)) }
471471
}
472+
473+
/// Constructs a new reference-counted slice with uninitialized contents, with the memory being
474+
/// filled with `0` bytes.
475+
///
476+
/// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
477+
/// incorrect usage of this method.
478+
///
479+
/// # Examples
480+
///
481+
/// ```
482+
/// #![feature(new_uninit)]
483+
///
484+
/// use std::rc::Rc;
485+
///
486+
/// let values = Rc::<[u32]>::new_zeroed_slice(3);
487+
/// let values = unsafe { values.assume_init() };
488+
///
489+
/// assert_eq!(*values, [0, 0, 0])
490+
/// ```
491+
///
492+
/// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed
493+
#[unstable(feature = "new_uninit", issue = "63291")]
494+
pub fn new_zeroed_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
495+
unsafe {
496+
Rc::from_ptr(Rc::allocate_for_layout(
497+
Layout::array::<T>(len).unwrap(),
498+
|layout| Global.alloc_zeroed(layout),
499+
|mem| {
500+
ptr::slice_from_raw_parts_mut(mem as *mut T, len)
501+
as *mut RcBox<[mem::MaybeUninit<T>]>
502+
},
503+
))
504+
}
505+
}
472506
}
473507

474508
impl<T> Rc<mem::MaybeUninit<T>> {

library/alloc/src/sync.rs

+34
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,40 @@ impl<T> Arc<[T]> {
468468
pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
469469
unsafe { Arc::from_ptr(Arc::allocate_for_slice(len)) }
470470
}
471+
472+
/// Constructs a new atomically reference-counted slice with uninitialized contents, with the memory being
473+
/// filled with `0` bytes.
474+
///
475+
/// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
476+
/// incorrect usage of this method.
477+
///
478+
/// # Examples
479+
///
480+
/// ```
481+
/// #![feature(new_uninit)]
482+
///
483+
/// use std::sync::Arc;
484+
///
485+
/// let values = Arc::<[u32]>::new_zeroed_slice(3);
486+
/// let values = unsafe { values.assume_init() };
487+
///
488+
/// assert_eq!(*values, [0, 0, 0])
489+
/// ```
490+
///
491+
/// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed
492+
#[unstable(feature = "new_uninit", issue = "63291")]
493+
pub fn new_zeroed_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
494+
unsafe {
495+
Arc::from_ptr(Arc::allocate_for_layout(
496+
Layout::array::<T>(len).unwrap(),
497+
|layout| Global.alloc_zeroed(layout),
498+
|mem| {
499+
ptr::slice_from_raw_parts_mut(mem as *mut T, len)
500+
as *mut ArcInner<[mem::MaybeUninit<T>]>
501+
},
502+
))
503+
}
504+
}
471505
}
472506

473507
impl<T> Arc<mem::MaybeUninit<T>> {

0 commit comments

Comments
 (0)