146
146
147
147
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
148
148
149
+ #[ cfg( not( no_global_oom_handling) ) ]
150
+ use crate :: co_alloc:: CoAllocPref ;
149
151
use core:: any:: Any ;
150
152
use core:: async_iter:: AsyncIterator ;
151
153
use core:: borrow;
@@ -632,7 +634,10 @@ impl<T> Box<[T]> {
632
634
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
633
635
#[ must_use]
634
636
pub fn new_uninit_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
635
- unsafe { RawVec :: with_capacity ( len) . into_box ( len) }
637
+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
638
+ unsafe {
639
+ RawVec :: < T , Global , { CO_ALLOC_PREF_META_NO ! ( ) } > :: with_capacity ( len) . into_box ( len)
640
+ }
636
641
}
637
642
638
643
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -657,7 +662,11 @@ impl<T> Box<[T]> {
657
662
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
658
663
#[ must_use]
659
664
pub fn new_zeroed_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
660
- unsafe { RawVec :: with_capacity_zeroed ( len) . into_box ( len) }
665
+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
666
+ unsafe {
667
+ RawVec :: < T , Global , { CO_ALLOC_PREF_META_NO ! ( ) } > :: with_capacity_zeroed ( len)
668
+ . into_box ( len)
669
+ }
661
670
}
662
671
663
672
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -692,7 +701,14 @@ impl<T> Box<[T]> {
692
701
} ;
693
702
Global . allocate ( layout) ?. cast ( )
694
703
} ;
695
- unsafe { Ok ( RawVec :: from_raw_parts_in ( ptr. as_ptr ( ) , len, Global ) . into_box ( len) ) }
704
+ unsafe {
705
+ Ok ( RawVec :: < T , Global , { CO_ALLOC_PREF_META_NO ! ( ) } > :: from_raw_parts_in (
706
+ ptr. as_ptr ( ) ,
707
+ len,
708
+ Global ,
709
+ )
710
+ . into_box ( len) )
711
+ }
696
712
}
697
713
698
714
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -726,11 +742,22 @@ impl<T> Box<[T]> {
726
742
} ;
727
743
Global . allocate_zeroed ( layout) ?. cast ( )
728
744
} ;
729
- unsafe { Ok ( RawVec :: from_raw_parts_in ( ptr. as_ptr ( ) , len, Global ) . into_box ( len) ) }
745
+ unsafe {
746
+ Ok ( RawVec :: < T , Global , { CO_ALLOC_PREF_META_NO ! ( ) } > :: from_raw_parts_in (
747
+ ptr. as_ptr ( ) ,
748
+ len,
749
+ Global ,
750
+ )
751
+ . into_box ( len) )
752
+ }
730
753
}
731
754
}
732
755
733
- impl < T , A : Allocator > Box < [ T ] , A > {
756
+ #[ allow( unused_braces) ]
757
+ impl < T , A : Allocator > Box < [ T ] , A >
758
+ where
759
+ [ ( ) ; { crate :: meta_num_slots!( A , crate :: CO_ALLOC_PREF_META_NO !( ) ) } ] : ,
760
+ {
734
761
/// Constructs a new boxed slice with uninitialized contents in the provided allocator.
735
762
///
736
763
/// # Examples
@@ -757,8 +784,11 @@ impl<T, A: Allocator> Box<[T], A> {
757
784
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
758
785
// #[unstable(feature = "new_uninit", issue = "63291")]
759
786
#[ must_use]
787
+ #[ allow( unused_braces) ]
760
788
pub fn new_uninit_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
761
- unsafe { RawVec :: with_capacity_in ( len, alloc) . into_box ( len) }
789
+ unsafe {
790
+ RawVec :: < T , A , { CO_ALLOC_PREF_META_NO ! ( ) } > :: with_capacity_in ( len, alloc) . into_box ( len)
791
+ }
762
792
}
763
793
764
794
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -785,8 +815,12 @@ impl<T, A: Allocator> Box<[T], A> {
785
815
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
786
816
// #[unstable(feature = "new_uninit", issue = "63291")]
787
817
#[ must_use]
818
+ #[ allow( unused_braces) ]
788
819
pub fn new_zeroed_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
789
- unsafe { RawVec :: with_capacity_zeroed_in ( len, alloc) . into_box ( len) }
820
+ unsafe {
821
+ RawVec :: < T , A , { CO_ALLOC_PREF_META_NO ! ( ) } > :: with_capacity_zeroed_in ( len, alloc)
822
+ . into_box ( len)
823
+ }
790
824
}
791
825
}
792
826
@@ -1487,7 +1521,7 @@ trait BoxFromSlice<T> {
1487
1521
impl < T : Clone > BoxFromSlice < T > for Box < [ T ] > {
1488
1522
#[ inline]
1489
1523
default fn from_slice ( slice : & [ T ] ) -> Self {
1490
- slice. to_vec ( ) . into_boxed_slice ( )
1524
+ slice. to_vec_co :: < { CO_ALLOC_PREF_META_NO ! ( ) } > ( ) . into_boxed_slice ( )
1491
1525
}
1492
1526
}
1493
1527
@@ -1496,7 +1530,7 @@ impl<T: Copy> BoxFromSlice<T> for Box<[T]> {
1496
1530
#[ inline]
1497
1531
fn from_slice ( slice : & [ T ] ) -> Self {
1498
1532
let len = slice. len ( ) ;
1499
- let buf = RawVec :: with_capacity ( len) ;
1533
+ let buf = RawVec :: < T , Global , { CO_ALLOC_PREF_META_NO ! ( ) } > :: with_capacity ( len) ;
1500
1534
unsafe {
1501
1535
ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
1502
1536
buf. into_box ( slice. len ( ) ) . assume_init ( )
@@ -1682,8 +1716,13 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
1682
1716
1683
1717
#[ cfg( not( no_global_oom_handling) ) ]
1684
1718
#[ stable( feature = "boxed_array_try_from_vec" , since = "1.66.0" ) ]
1685
- impl < T , const N : usize > TryFrom < Vec < T > > for Box < [ T ; N ] > {
1686
- type Error = Vec < T > ;
1719
+ #[ allow( unused_braces) ]
1720
+ impl < T , const N : usize , const CO_ALLOC_PREF : CoAllocPref > TryFrom < Vec < T , Global , CO_ALLOC_PREF > >
1721
+ for Box < [ T ; N ] >
1722
+ where
1723
+ [ ( ) ; { meta_num_slots_global ! ( CO_ALLOC_PREF ) } ] : ,
1724
+ {
1725
+ type Error = Vec < T , Global , CO_ALLOC_PREF > ;
1687
1726
1688
1727
/// Attempts to convert a `Vec<T>` into a `Box<[T; N]>`.
1689
1728
///
@@ -1703,7 +1742,7 @@ impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
1703
1742
/// let state: Box<[f32; 100]> = vec![1.0; 100].try_into().unwrap();
1704
1743
/// assert_eq!(state.len(), 100);
1705
1744
/// ```
1706
- fn try_from ( vec : Vec < T > ) -> Result < Self , Self :: Error > {
1745
+ fn try_from ( vec : Vec < T , Global , CO_ALLOC_PREF > ) -> Result < Self , Self :: Error > {
1707
1746
if vec. len ( ) == N {
1708
1747
let boxed_slice = vec. into_boxed_slice ( ) ;
1709
1748
Ok ( unsafe { boxed_slice_as_array_unchecked ( boxed_slice) } )
@@ -2038,10 +2077,15 @@ impl<I> FromIterator<I> for Box<[I]> {
2038
2077
2039
2078
#[ cfg( not( no_global_oom_handling) ) ]
2040
2079
#[ stable( feature = "box_slice_clone" , since = "1.3.0" ) ]
2041
- impl < T : Clone , A : Allocator + Clone > Clone for Box < [ T ] , A > {
2080
+ #[ allow( unused_braces) ]
2081
+ impl < T : Clone , A : Allocator + Clone > Clone for Box < [ T ] , A >
2082
+ where
2083
+ [ ( ) ; { crate :: meta_num_slots!( A , crate :: CO_ALLOC_PREF_META_NO !( ) ) } ] : ,
2084
+ {
2042
2085
fn clone ( & self ) -> Self {
2043
2086
let alloc = Box :: allocator ( self ) . clone ( ) ;
2044
- self . to_vec_in ( alloc) . into_boxed_slice ( )
2087
+ // false = no need for co-alloc metadata, since it would get lost once converted to the boxed slice.
2088
+ self . to_vec_in_co :: < A , { CO_ALLOC_PREF_META_NO ! ( ) } > ( alloc) . into_boxed_slice ( )
2045
2089
}
2046
2090
2047
2091
fn clone_from ( & mut self , other : & Self ) {
0 commit comments