@@ -157,7 +157,6 @@ use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw};
157
157
use crate :: alloc:: { AllocError , Allocator , Global , Layout } ;
158
158
#[ cfg( not( no_global_oom_handling) ) ]
159
159
use crate :: borrow:: Cow ;
160
- #[ cfg( not( no_global_oom_handling) ) ]
161
160
use crate :: raw_vec:: RawVec ;
162
161
#[ cfg( not( no_global_oom_handling) ) ]
163
162
use crate :: str:: from_boxed_utf8_unchecked;
@@ -589,6 +588,71 @@ impl<T> Box<[T]> {
589
588
pub fn new_zeroed_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
590
589
unsafe { RawVec :: with_capacity_zeroed ( len) . into_box ( len) }
591
590
}
591
+
592
+ /// Constructs a new boxed slice with uninitialized contents. Returns an error if
593
+ /// the allocation fails
594
+ ///
595
+ /// # Examples
596
+ ///
597
+ /// ```
598
+ /// #![feature(allocator_api, new_uninit)]
599
+ ///
600
+ /// let mut values = Box::<[u32]>::try_new_uninit_slice(3)?;
601
+ /// let values = unsafe {
602
+ /// // Deferred initialization:
603
+ /// values[0].as_mut_ptr().write(1);
604
+ /// values[1].as_mut_ptr().write(2);
605
+ /// values[2].as_mut_ptr().write(3);
606
+ /// values.assume_init()
607
+ /// };
608
+ ///
609
+ /// assert_eq!(*values, [1, 2, 3]);
610
+ /// # Ok::<(), std::alloc::AllocError>(())
611
+ /// ```
612
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
613
+ #[ inline]
614
+ pub fn try_new_uninit_slice ( len : usize ) -> Result < Box < [ mem:: MaybeUninit < T > ] > , AllocError > {
615
+ unsafe {
616
+ let layout = match Layout :: array :: < mem:: MaybeUninit < T > > ( len) {
617
+ Ok ( l) => l,
618
+ Err ( _) => return Err ( AllocError ) ,
619
+ } ;
620
+ let ptr = Global . allocate ( layout) ?;
621
+ Ok ( RawVec :: from_raw_parts_in ( ptr. as_mut_ptr ( ) as * mut _ , len, Global ) . into_box ( len) )
622
+ }
623
+ }
624
+
625
+ /// Constructs a new boxed slice with uninitialized contents, with the memory
626
+ /// being filled with `0` bytes. Returns an error if the allocation fails
627
+ ///
628
+ /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
629
+ /// of this method.
630
+ ///
631
+ /// # Examples
632
+ ///
633
+ /// ```
634
+ /// #![feature(allocator_api, new_uninit)]
635
+ ///
636
+ /// let values = Box::<[u32]>::try_new_zeroed_slice(3)?;
637
+ /// let values = unsafe { values.assume_init() };
638
+ ///
639
+ /// assert_eq!(*values, [0, 0, 0]);
640
+ /// # Ok::<(), std::alloc::AllocError>(())
641
+ /// ```
642
+ ///
643
+ /// [zeroed]: mem::MaybeUninit::zeroed
644
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
645
+ #[ inline]
646
+ pub fn try_new_zeroed_slice ( len : usize ) -> Result < Box < [ mem:: MaybeUninit < T > ] > , AllocError > {
647
+ unsafe {
648
+ let layout = match Layout :: array :: < mem:: MaybeUninit < T > > ( len) {
649
+ Ok ( l) => l,
650
+ Err ( _) => return Err ( AllocError ) ,
651
+ } ;
652
+ let ptr = Global . allocate_zeroed ( layout) ?;
653
+ Ok ( RawVec :: from_raw_parts_in ( ptr. as_mut_ptr ( ) as * mut _ , len, Global ) . into_box ( len) )
654
+ }
655
+ }
592
656
}
593
657
594
658
impl < T , A : Allocator > Box < [ T ] , A > {
0 commit comments