@@ -640,7 +640,8 @@ impl<T> Box<[T]> {
640
640
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
641
641
#[ must_use]
642
642
pub fn new_uninit_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
643
- unsafe { RawVec :: with_capacity ( len) . into_box ( len) }
643
+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
644
+ unsafe { RawVec :: < T , Global , false > :: with_capacity ( len) . into_box ( len) }
644
645
}
645
646
646
647
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -665,7 +666,8 @@ impl<T> Box<[T]> {
665
666
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
666
667
#[ must_use]
667
668
pub fn new_zeroed_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
668
- unsafe { RawVec :: with_capacity_zeroed ( len) . into_box ( len) }
669
+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
670
+ unsafe { RawVec :: < T , Global , false > :: with_capacity_zeroed ( len) . into_box ( len) }
669
671
}
670
672
671
673
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -697,7 +699,12 @@ impl<T> Box<[T]> {
697
699
Err ( _) => return Err ( AllocError ) ,
698
700
} ;
699
701
let ptr = Global . allocate ( layout) ?;
700
- Ok ( RawVec :: from_raw_parts_in ( ptr. as_mut_ptr ( ) as * mut _ , len, Global ) . into_box ( len) )
702
+ Ok ( RawVec :: < T , Global , false > :: from_raw_parts_in (
703
+ ptr. as_mut_ptr ( ) as * mut _ ,
704
+ len,
705
+ Global ,
706
+ )
707
+ . into_box ( len) )
701
708
}
702
709
}
703
710
@@ -729,12 +736,20 @@ impl<T> Box<[T]> {
729
736
Err ( _) => return Err ( AllocError ) ,
730
737
} ;
731
738
let ptr = Global . allocate_zeroed ( layout) ?;
732
- Ok ( RawVec :: from_raw_parts_in ( ptr. as_mut_ptr ( ) as * mut _ , len, Global ) . into_box ( len) )
739
+ Ok ( RawVec :: < T , Global , false > :: from_raw_parts_in (
740
+ ptr. as_mut_ptr ( ) as * mut _ ,
741
+ len,
742
+ Global ,
743
+ )
744
+ . into_box ( len) )
733
745
}
734
746
}
735
747
}
736
748
737
- impl < T , A : Allocator > Box < [ T ] , A > {
749
+ impl < T , A : Allocator > Box < [ T ] , A >
750
+ where
751
+ [ ( ) ; core:: alloc:: co_alloc_metadata_num_slots :: < A > ( ) ] : ,
752
+ {
738
753
/// Constructs a new boxed slice with uninitialized contents in the provided allocator.
739
754
///
740
755
/// # Examples
@@ -761,8 +776,13 @@ impl<T, A: Allocator> Box<[T], A> {
761
776
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
762
777
// #[unstable(feature = "new_uninit", issue = "63291")]
763
778
#[ must_use]
764
- pub fn new_uninit_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
765
- unsafe { RawVec :: with_capacity_in ( len, alloc) . into_box ( len) }
779
+ #[ allow( unused_braces) ]
780
+ pub fn new_uninit_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A >
781
+ where
782
+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
783
+ [ ( ) ; core:: alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( false ) ] : ,
784
+ {
785
+ unsafe { RawVec :: < T , A , false > :: with_capacity_in ( len, alloc) . into_box ( len) }
766
786
}
767
787
768
788
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -789,8 +809,13 @@ impl<T, A: Allocator> Box<[T], A> {
789
809
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
790
810
// #[unstable(feature = "new_uninit", issue = "63291")]
791
811
#[ must_use]
792
- pub fn new_zeroed_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
793
- unsafe { RawVec :: with_capacity_zeroed_in ( len, alloc) . into_box ( len) }
812
+ #[ allow( unused_braces) ]
813
+ pub fn new_zeroed_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A >
814
+ where
815
+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
816
+ [ ( ) ; core:: alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( false ) ] : ,
817
+ {
818
+ unsafe { RawVec :: < T , A , false > :: with_capacity_zeroed_in ( len, alloc) . into_box ( len) }
794
819
}
795
820
}
796
821
@@ -1497,7 +1522,8 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
1497
1522
/// ```
1498
1523
fn from ( slice : & [ T ] ) -> Box < [ T ] > {
1499
1524
let len = slice. len ( ) ;
1500
- let buf = RawVec :: with_capacity ( len) ;
1525
+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
1526
+ let buf = RawVec :: < T , Global , false > :: with_capacity ( len) ;
1501
1527
unsafe {
1502
1528
ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
1503
1529
buf. into_box ( slice. len ( ) ) . assume_init ( )
@@ -1661,8 +1687,12 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
1661
1687
1662
1688
#[ cfg( not( no_global_oom_handling) ) ]
1663
1689
#[ 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 > ;
1666
1696
1667
1697
/// Attempts to convert a `Vec<T>` into a `Box<[T; N]>`.
1668
1698
///
@@ -1682,7 +1712,7 @@ impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
1682
1712
/// let state: Box<[f32; 100]> = vec![1.0; 100].try_into().unwrap();
1683
1713
/// assert_eq!(state.len(), 100);
1684
1714
/// ```
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 > {
1686
1716
if vec. len ( ) == N {
1687
1717
let boxed_slice = vec. into_boxed_slice ( ) ;
1688
1718
Ok ( unsafe { boxed_slice_as_array_unchecked ( boxed_slice) } )
@@ -2019,10 +2049,14 @@ impl<I> FromIterator<I> for Box<[I]> {
2019
2049
2020
2050
#[ cfg( not( no_global_oom_handling) ) ]
2021
2051
#[ 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
+ {
2023
2056
fn clone ( & self ) -> Self {
2024
2057
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 ( )
2026
2060
}
2027
2061
2028
2062
fn clone_from ( & mut self , other : & Self ) {
0 commit comments