@@ -704,7 +704,7 @@ impl<T> Box<[T]> {
704
704
}
705
705
706
706
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
707
- /// the allocation fails
707
+ /// the allocation fails.
708
708
///
709
709
/// # Examples
710
710
///
@@ -739,7 +739,7 @@ impl<T> Box<[T]> {
739
739
}
740
740
741
741
/// Constructs a new boxed slice with uninitialized contents, with the memory
742
- /// being filled with `0` bytes. Returns an error if the allocation fails
742
+ /// being filled with `0` bytes. Returns an error if the allocation fails.
743
743
///
744
744
/// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
745
745
/// of this method.
@@ -831,6 +831,85 @@ impl<T, A: Allocator> Box<[T], A> {
831
831
pub fn new_zeroed_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
832
832
unsafe { RawVec :: with_capacity_zeroed_in ( len, alloc) . into_box ( len) }
833
833
}
834
+
835
+ /// Constructs a new boxed slice with uninitialized contents in the provided allocator. Returns an error if
836
+ /// the allocation fails.
837
+ ///
838
+ /// # Examples
839
+ ///
840
+ /// ```
841
+ /// #![feature(allocator_api, new_uninit)]
842
+ ///
843
+ /// use std::alloc::System;
844
+ ///
845
+ /// let mut values = Box::<[u32], _>::try_new_uninit_slice_in(3, System)?;
846
+ /// let values = unsafe {
847
+ /// // Deferred initialization:
848
+ /// values[0].as_mut_ptr().write(1);
849
+ /// values[1].as_mut_ptr().write(2);
850
+ /// values[2].as_mut_ptr().write(3);
851
+ /// values.assume_init()
852
+ /// };
853
+ ///
854
+ /// assert_eq!(*values, [1, 2, 3]);
855
+ /// # Ok::<(), std::alloc::AllocError>(())
856
+ /// ```
857
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
858
+ #[ inline]
859
+ pub fn try_new_uninit_slice_in (
860
+ len : usize ,
861
+ alloc : A ,
862
+ ) -> Result < Box < [ mem:: MaybeUninit < T > ] , A > , AllocError > {
863
+ let ptr = if T :: IS_ZST || len == 0 {
864
+ NonNull :: dangling ( )
865
+ } else {
866
+ let layout = match Layout :: array :: < mem:: MaybeUninit < T > > ( len) {
867
+ Ok ( l) => l,
868
+ Err ( _) => return Err ( AllocError ) ,
869
+ } ;
870
+ alloc. allocate ( layout) ?. cast ( )
871
+ } ;
872
+ unsafe { Ok ( RawVec :: from_raw_parts_in ( ptr. as_ptr ( ) , len, alloc) . into_box ( len) ) }
873
+ }
874
+
875
+ /// Constructs a new boxed slice with uninitialized contents in the provided allocator, with the memory
876
+ /// being filled with `0` bytes. Returns an error if the allocation fails.
877
+ ///
878
+ /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
879
+ /// of this method.
880
+ ///
881
+ /// # Examples
882
+ ///
883
+ /// ```
884
+ /// #![feature(allocator_api, new_uninit)]
885
+ ///
886
+ /// use std::alloc::System;
887
+ ///
888
+ /// let values = Box::<[u32], _>::try_new_zeroed_slice_in(3, System)?;
889
+ /// let values = unsafe { values.assume_init() };
890
+ ///
891
+ /// assert_eq!(*values, [0, 0, 0]);
892
+ /// # Ok::<(), std::alloc::AllocError>(())
893
+ /// ```
894
+ ///
895
+ /// [zeroed]: mem::MaybeUninit::zeroed
896
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
897
+ #[ inline]
898
+ pub fn try_new_zeroed_slice_in (
899
+ len : usize ,
900
+ alloc : A ,
901
+ ) -> Result < Box < [ mem:: MaybeUninit < T > ] , A > , AllocError > {
902
+ let ptr = if T :: IS_ZST || len == 0 {
903
+ NonNull :: dangling ( )
904
+ } else {
905
+ let layout = match Layout :: array :: < mem:: MaybeUninit < T > > ( len) {
906
+ Ok ( l) => l,
907
+ Err ( _) => return Err ( AllocError ) ,
908
+ } ;
909
+ alloc. allocate_zeroed ( layout) ?. cast ( )
910
+ } ;
911
+ unsafe { Ok ( RawVec :: from_raw_parts_in ( ptr. as_ptr ( ) , len, alloc) . into_box ( len) ) }
912
+ }
834
913
}
835
914
836
915
impl < T , A : Allocator > Box < mem:: MaybeUninit < T > , A > {
0 commit comments