@@ -641,7 +641,8 @@ impl<T> Box<[T]> {
641
641
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
642
642
#[ must_use]
643
643
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) }
645
646
}
646
647
647
648
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -666,7 +667,8 @@ impl<T> Box<[T]> {
666
667
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
667
668
#[ must_use]
668
669
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) }
670
672
}
671
673
672
674
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -698,7 +700,12 @@ impl<T> Box<[T]> {
698
700
Err ( _) => return Err ( AllocError ) ,
699
701
} ;
700
702
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) )
702
709
}
703
710
}
704
711
@@ -730,12 +737,20 @@ impl<T> Box<[T]> {
730
737
Err ( _) => return Err ( AllocError ) ,
731
738
} ;
732
739
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) )
734
746
}
735
747
}
736
748
}
737
749
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
+ {
739
754
/// Constructs a new boxed slice with uninitialized contents in the provided allocator.
740
755
///
741
756
/// # Examples
@@ -762,8 +777,13 @@ impl<T, A: Allocator> Box<[T], A> {
762
777
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
763
778
// #[unstable(feature = "new_uninit", issue = "63291")]
764
779
#[ 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) }
767
787
}
768
788
769
789
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -790,8 +810,13 @@ impl<T, A: Allocator> Box<[T], A> {
790
810
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
791
811
// #[unstable(feature = "new_uninit", issue = "63291")]
792
812
#[ 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) }
795
820
}
796
821
}
797
822
@@ -1496,7 +1521,8 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
1496
1521
/// ```
1497
1522
fn from ( slice : & [ T ] ) -> Box < [ T ] > {
1498
1523
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) ;
1500
1526
unsafe {
1501
1527
ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
1502
1528
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