File tree Expand file tree Collapse file tree 3 files changed +33
-3
lines changed Expand file tree Collapse file tree 3 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -142,6 +142,9 @@ impl<T> Box<T> {
142142#[ unstable( feature = "new_uninit" ,  issue = "63291" ) ]  
143143    pub  fn  new_uninit ( )  -> Box < mem:: MaybeUninit < T > >  { 
144144        let  layout = alloc:: Layout :: new :: < mem:: MaybeUninit < T > > ( ) ; 
145+         if  layout. size ( )  == 0  { 
146+             return  Box ( NonNull :: dangling ( ) . into ( ) ) 
147+         } 
145148        let  ptr = unsafe  { 
146149            Global . alloc ( layout) 
147150                . unwrap_or_else ( |_| alloc:: handle_alloc_error ( layout) ) 
@@ -182,9 +185,16 @@ impl<T> Box<[T]> {
182185#[ unstable( feature = "new_uninit" ,  issue = "63291" ) ]  
183186    pub  fn  new_uninit_slice ( len :  usize )  -> Box < [ mem:: MaybeUninit < T > ] >  { 
184187        let  layout = alloc:: Layout :: array :: < mem:: MaybeUninit < T > > ( len) . unwrap ( ) ; 
185-         let  ptr = unsafe  {  alloc:: alloc ( layout)  } ; 
186-         let  unique = Unique :: new ( ptr) . unwrap_or_else ( || alloc:: handle_alloc_error ( layout) ) ; 
187-         let  slice = unsafe  {  slice:: from_raw_parts_mut ( unique. cast ( ) . as_ptr ( ) ,  len)  } ; 
188+         let  ptr = if  layout. size ( )  == 0  { 
189+             NonNull :: dangling ( ) 
190+         }  else  { 
191+             unsafe  { 
192+                 Global . alloc ( layout) 
193+                     . unwrap_or_else ( |_| alloc:: handle_alloc_error ( layout) ) 
194+                     . cast ( ) 
195+             } 
196+         } ; 
197+         let  slice = unsafe  {  slice:: from_raw_parts_mut ( ptr. as_ptr ( ) ,  len)  } ; 
188198        Box ( Unique :: from ( slice) ) 
189199    } 
190200} 
Original file line number Diff line number Diff line change 1+ use  std:: ptr:: NonNull ; 
2+ use  std:: mem:: MaybeUninit ; 
3+ 
4+ #[ test]  
5+ fn  unitialized_zero_size_box ( )  { 
6+     assert_eq ! ( 
7+         & * Box :: <( ) >:: new_uninit( )  as  * const  _, 
8+         NonNull :: <MaybeUninit <( ) >>:: dangling( ) . as_ptr( ) , 
9+     ) ; 
10+     assert_eq ! ( 
11+         Box :: <[ ( ) ] >:: new_uninit_slice( 4 ) . as_ptr( ) , 
12+         NonNull :: <MaybeUninit <( ) >>:: dangling( ) . as_ptr( ) , 
13+     ) ; 
14+     assert_eq ! ( 
15+         Box :: <[ String ] >:: new_uninit_slice( 0 ) . as_ptr( ) , 
16+         NonNull :: <MaybeUninit <String >>:: dangling( ) . as_ptr( ) , 
17+     ) ; 
18+ } 
Original file line number Diff line number Diff line change 22#![ feature( box_syntax) ]  
33#![ feature( drain_filter) ]  
44#![ feature( exact_size_is_empty) ]  
5+ #![ feature( new_uninit) ]  
56#![ feature( option_flattening) ]  
67#![ feature( pattern) ]  
78#![ feature( trusted_len) ]  
@@ -14,6 +15,7 @@ use std::collections::hash_map::DefaultHasher;
1415
1516mod  arc; 
1617mod  binary_heap; 
18+ mod  boxed; 
1719mod  btree; 
1820mod  cow_str; 
1921mod  fmt; 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments