@@ -641,7 +641,8 @@ impl<T> Box<[T]> {
641641 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
642642 #[ must_use]
643643 pub fn new_uninit_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
644- unsafe { RawVec :: with_capacity ( len) . into_box ( len) }
644+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
645+ unsafe { RawVec :: < T , Global , false > :: with_capacity ( len) . into_box ( len) }
645646 }
646647
647648 /// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -666,7 +667,8 @@ impl<T> Box<[T]> {
666667 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
667668 #[ must_use]
668669 pub fn new_zeroed_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
669- unsafe { RawVec :: with_capacity_zeroed ( len) . into_box ( len) }
670+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
671+ unsafe { RawVec :: < T , Global , false > :: with_capacity_zeroed ( len) . into_box ( len) }
670672 }
671673
672674 /// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -698,7 +700,12 @@ impl<T> Box<[T]> {
698700 Err ( _) => return Err ( AllocError ) ,
699701 } ;
700702 let ptr = Global . allocate ( layout) ?;
701- Ok ( RawVec :: from_raw_parts_in ( ptr. as_mut_ptr ( ) as * mut _ , len, Global ) . into_box ( len) )
703+ Ok ( RawVec :: < T , Global , false > :: from_raw_parts_in (
704+ ptr. as_mut_ptr ( ) as * mut _ ,
705+ len,
706+ Global ,
707+ )
708+ . into_box ( len) )
702709 }
703710 }
704711
@@ -730,12 +737,20 @@ impl<T> Box<[T]> {
730737 Err ( _) => return Err ( AllocError ) ,
731738 } ;
732739 let ptr = Global . allocate_zeroed ( layout) ?;
733- Ok ( RawVec :: from_raw_parts_in ( ptr. as_mut_ptr ( ) as * mut _ , len, Global ) . into_box ( len) )
740+ Ok ( RawVec :: < T , Global , false > :: from_raw_parts_in (
741+ ptr. as_mut_ptr ( ) as * mut _ ,
742+ len,
743+ Global ,
744+ )
745+ . into_box ( len) )
734746 }
735747 }
736748}
737749
738- impl < T , A : Allocator > Box < [ T ] , A > {
750+ impl < T , A : Allocator > Box < [ T ] , A >
751+ where
752+ [ ( ) ; core:: alloc:: co_alloc_metadata_num_slots :: < A > ( ) ] : ,
753+ {
739754 /// Constructs a new boxed slice with uninitialized contents in the provided allocator.
740755 ///
741756 /// # Examples
@@ -762,8 +777,13 @@ impl<T, A: Allocator> Box<[T], A> {
762777 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
763778 // #[unstable(feature = "new_uninit", issue = "63291")]
764779 #[ must_use]
765- pub fn new_uninit_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
766- unsafe { RawVec :: with_capacity_in ( len, alloc) . into_box ( len) }
780+ #[ allow( unused_braces) ]
781+ pub fn new_uninit_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A >
782+ where
783+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
784+ [ ( ) ; core:: alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( false ) ] : ,
785+ {
786+ unsafe { RawVec :: < T , A , false > :: with_capacity_in ( len, alloc) . into_box ( len) }
767787 }
768788
769789 /// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -790,8 +810,13 @@ impl<T, A: Allocator> Box<[T], A> {
790810 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
791811 // #[unstable(feature = "new_uninit", issue = "63291")]
792812 #[ must_use]
793- pub fn new_zeroed_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
794- unsafe { RawVec :: with_capacity_zeroed_in ( len, alloc) . into_box ( len) }
813+ #[ allow( unused_braces) ]
814+ pub fn new_zeroed_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A >
815+ where
816+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
817+ [ ( ) ; core:: alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( false ) ] : ,
818+ {
819+ unsafe { RawVec :: < T , A , false > :: with_capacity_zeroed_in ( len, alloc) . into_box ( len) }
795820 }
796821}
797822
@@ -1496,7 +1521,8 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
14961521 /// ```
14971522 fn from ( slice : & [ T ] ) -> Box < [ T ] > {
14981523 let len = slice. len ( ) ;
1499- let buf = RawVec :: with_capacity ( len) ;
1524+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
1525+ let buf = RawVec :: < T , Global , false > :: with_capacity ( len) ;
15001526 unsafe {
15011527 ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
15021528 buf. into_box ( slice. len ( ) ) . assume_init ( )
@@ -1661,8 +1687,12 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
16611687
16621688#[ cfg( not( no_global_oom_handling) ) ]
16631689#[ stable( feature = "boxed_array_try_from_vec" , since = "1.66.0" ) ]
1664- impl < T , const N : usize > TryFrom < Vec < T > > for Box < [ T ; N ] > {
1665- type Error = Vec < T > ;
1690+ impl < T , const N : usize , const COOP_PREFERRED : bool > TryFrom < Vec < T , Global , COOP_PREFERRED > >
1691+ for Box < [ T ; N ] >
1692+ where
1693+ [ ( ) ; core:: alloc:: co_alloc_metadata_num_slots_with_preference :: < Global > ( COOP_PREFERRED ) ] : ,
1694+ {
1695+ type Error = Vec < T , Global , COOP_PREFERRED > ;
16661696
16671697 /// Attempts to convert a `Vec<T>` into a `Box<[T; N]>`.
16681698 ///
@@ -1682,7 +1712,7 @@ impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
16821712 /// let state: Box<[f32; 100]> = vec![1.0; 100].try_into().unwrap();
16831713 /// assert_eq!(state.len(), 100);
16841714 /// ```
1685- fn try_from ( vec : Vec < T > ) -> Result < Self , Self :: Error > {
1715+ fn try_from ( vec : Vec < T , Global , COOP_PREFERRED > ) -> Result < Self , Self :: Error > {
16861716 if vec. len ( ) == N {
16871717 let boxed_slice = vec. into_boxed_slice ( ) ;
16881718 Ok ( unsafe { boxed_slice_as_array_unchecked ( boxed_slice) } )
@@ -2019,10 +2049,14 @@ impl<I> FromIterator<I> for Box<[I]> {
20192049
20202050#[ cfg( not( no_global_oom_handling) ) ]
20212051#[ stable( feature = "box_slice_clone" , since = "1.3.0" ) ]
2022- impl < T : Clone , A : Allocator + Clone > Clone for Box < [ T ] , A > {
2052+ impl < T : Clone , A : Allocator + Clone > Clone for Box < [ T ] , A >
2053+ where
2054+ [ ( ) ; core:: alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( false ) ] : ,
2055+ {
20232056 fn clone ( & self ) -> Self {
20242057 let alloc = Box :: allocator ( self ) . clone ( ) ;
2025- self . to_vec_in ( alloc) . into_boxed_slice ( )
2058+ // false = no need for co-alloc metadata, since it would get lost once converted to the boxed slice.
2059+ self . to_vec_in :: < A , false > ( alloc) . into_boxed_slice ( )
20262060 }
20272061
20282062 fn clone_from ( & mut self , other : & Self ) {
0 commit comments