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;
@@ -640,7 +642,10 @@ impl<T> Box<[T]> {
640
642
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
641
643
#[ must_use]
642
644
pub fn new_uninit_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
643
- unsafe { RawVec :: with_capacity ( len) . into_box ( len) }
645
+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
646
+ unsafe {
647
+ RawVec :: < T , Global , { CO_ALLOC_PREF_META_NO ! ( ) } > :: with_capacity ( len) . into_box ( len)
648
+ }
644
649
}
645
650
646
651
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -665,7 +670,11 @@ impl<T> Box<[T]> {
665
670
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
666
671
#[ must_use]
667
672
pub fn new_zeroed_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
668
- unsafe { RawVec :: with_capacity_zeroed ( len) . into_box ( len) }
673
+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
674
+ unsafe {
675
+ RawVec :: < T , Global , { CO_ALLOC_PREF_META_NO ! ( ) } > :: with_capacity_zeroed ( len)
676
+ . into_box ( len)
677
+ }
669
678
}
670
679
671
680
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -697,7 +706,12 @@ impl<T> Box<[T]> {
697
706
Err ( _) => return Err ( AllocError ) ,
698
707
} ;
699
708
let ptr = Global . allocate ( layout) ?;
700
- Ok ( RawVec :: from_raw_parts_in ( ptr. as_mut_ptr ( ) as * mut _ , len, Global ) . into_box ( len) )
709
+ Ok ( RawVec :: < T , Global , { CO_ALLOC_PREF_META_NO ! ( ) } > :: from_raw_parts_in (
710
+ ptr. as_mut_ptr ( ) as * mut _ ,
711
+ len,
712
+ Global ,
713
+ )
714
+ . into_box ( len) )
701
715
}
702
716
}
703
717
@@ -729,12 +743,21 @@ impl<T> Box<[T]> {
729
743
Err ( _) => return Err ( AllocError ) ,
730
744
} ;
731
745
let ptr = Global . allocate_zeroed ( layout) ?;
732
- Ok ( RawVec :: from_raw_parts_in ( ptr. as_mut_ptr ( ) as * mut _ , len, Global ) . into_box ( len) )
746
+ Ok ( RawVec :: < T , Global , { CO_ALLOC_PREF_META_NO ! ( ) } > :: from_raw_parts_in (
747
+ ptr. as_mut_ptr ( ) as * mut _ ,
748
+ len,
749
+ Global ,
750
+ )
751
+ . into_box ( len) )
733
752
}
734
753
}
735
754
}
736
755
737
- 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
+ {
738
761
/// Constructs a new boxed slice with uninitialized contents in the provided allocator.
739
762
///
740
763
/// # Examples
@@ -761,8 +784,11 @@ impl<T, A: Allocator> Box<[T], A> {
761
784
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
762
785
// #[unstable(feature = "new_uninit", issue = "63291")]
763
786
#[ must_use]
787
+ #[ allow( unused_braces) ]
764
788
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) }
789
+ unsafe {
790
+ RawVec :: < T , A , { CO_ALLOC_PREF_META_NO ! ( ) } > :: with_capacity_in ( len, alloc) . into_box ( len)
791
+ }
766
792
}
767
793
768
794
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -789,8 +815,12 @@ impl<T, A: Allocator> Box<[T], A> {
789
815
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
790
816
// #[unstable(feature = "new_uninit", issue = "63291")]
791
817
#[ must_use]
818
+ #[ allow( unused_braces) ]
792
819
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) }
820
+ unsafe {
821
+ RawVec :: < T , A , { CO_ALLOC_PREF_META_NO ! ( ) } > :: with_capacity_zeroed_in ( len, alloc)
822
+ . into_box ( len)
823
+ }
794
824
}
795
825
}
796
826
@@ -1497,7 +1527,8 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
1497
1527
/// ```
1498
1528
fn from ( slice : & [ T ] ) -> Box < [ T ] > {
1499
1529
let len = slice. len ( ) ;
1500
- let buf = RawVec :: with_capacity ( len) ;
1530
+ // false = no need for co-alloc metadata, since it would get lost once converted to Box.
1531
+ let buf = RawVec :: < T , Global , { CO_ALLOC_PREF_META_NO ! ( ) } > :: with_capacity ( len) ;
1501
1532
unsafe {
1502
1533
ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
1503
1534
buf. into_box ( slice. len ( ) ) . assume_init ( )
@@ -1661,8 +1692,13 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
1661
1692
1662
1693
#[ cfg( not( no_global_oom_handling) ) ]
1663
1694
#[ 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 > ;
1695
+ #[ allow( unused_braces) ]
1696
+ impl < T , const N : usize , const CO_ALLOC_PREF : CoAllocPref > TryFrom < Vec < T , Global , CO_ALLOC_PREF > >
1697
+ for Box < [ T ; N ] >
1698
+ where
1699
+ [ ( ) ; { meta_num_slots_global ! ( CO_ALLOC_PREF ) } ] : ,
1700
+ {
1701
+ type Error = Vec < T , Global , CO_ALLOC_PREF > ;
1666
1702
1667
1703
/// Attempts to convert a `Vec<T>` into a `Box<[T; N]>`.
1668
1704
///
@@ -1682,7 +1718,7 @@ impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
1682
1718
/// let state: Box<[f32; 100]> = vec![1.0; 100].try_into().unwrap();
1683
1719
/// assert_eq!(state.len(), 100);
1684
1720
/// ```
1685
- fn try_from ( vec : Vec < T > ) -> Result < Self , Self :: Error > {
1721
+ fn try_from ( vec : Vec < T , Global , CO_ALLOC_PREF > ) -> Result < Self , Self :: Error > {
1686
1722
if vec. len ( ) == N {
1687
1723
let boxed_slice = vec. into_boxed_slice ( ) ;
1688
1724
Ok ( unsafe { boxed_slice_as_array_unchecked ( boxed_slice) } )
@@ -2019,10 +2055,15 @@ impl<I> FromIterator<I> for Box<[I]> {
2019
2055
2020
2056
#[ cfg( not( no_global_oom_handling) ) ]
2021
2057
#[ stable( feature = "box_slice_clone" , since = "1.3.0" ) ]
2022
- impl < T : Clone , A : Allocator + Clone > Clone for Box < [ T ] , A > {
2058
+ #[ allow( unused_braces) ]
2059
+ impl < T : Clone , A : Allocator + Clone > Clone for Box < [ T ] , A >
2060
+ where
2061
+ [ ( ) ; { crate :: meta_num_slots!( A , crate :: CO_ALLOC_PREF_META_NO !( ) ) } ] : ,
2062
+ {
2023
2063
fn clone ( & self ) -> Self {
2024
2064
let alloc = Box :: allocator ( self ) . clone ( ) ;
2025
- self . to_vec_in ( alloc) . into_boxed_slice ( )
2065
+ // false = no need for co-alloc metadata, since it would get lost once converted to the boxed slice.
2066
+ self . to_vec_in_co :: < A , { CO_ALLOC_PREF_META_NO ! ( ) } > ( alloc) . into_boxed_slice ( )
2026
2067
}
2027
2068
2028
2069
fn clone_from ( & mut self , other : & Self ) {
0 commit comments