@@ -196,12 +196,14 @@ impl<T> Box<T> {
196196 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
197197 pub fn new_uninit ( ) -> Box < mem:: MaybeUninit < T > > {
198198 let layout = alloc:: Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
199- if layout. size ( ) == 0 {
200- return Box ( NonNull :: dangling ( ) . into ( ) ) ;
199+ unsafe {
200+ let ptr = if layout. size ( ) == 0 {
201+ NonNull :: dangling ( )
202+ } else {
203+ Global . alloc ( layout) . unwrap_or_else ( |_| alloc:: handle_alloc_error ( layout) ) . cast ( )
204+ } ;
205+ Box :: from_raw ( ptr. as_ptr ( ) )
201206 }
202- let ptr =
203- unsafe { Global . alloc ( layout) . unwrap_or_else ( |_| alloc:: handle_alloc_error ( layout) ) } ;
204- Box ( ptr. cast ( ) . into ( ) )
205207 }
206208
207209 /// Constructs a new `Box` with uninitialized contents, with the memory
@@ -264,15 +266,14 @@ impl<T> Box<[T]> {
264266 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
265267 pub fn new_uninit_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
266268 let layout = alloc:: Layout :: array :: < mem:: MaybeUninit < T > > ( len) . unwrap ( ) ;
267- let ptr = if layout . size ( ) == 0 {
268- NonNull :: dangling ( )
269- } else {
270- unsafe {
269+ unsafe {
270+ let ptr = if layout . size ( ) == 0 {
271+ NonNull :: dangling ( )
272+ } else {
271273 Global . alloc ( layout) . unwrap_or_else ( |_| alloc:: handle_alloc_error ( layout) ) . cast ( )
272- }
273- } ;
274- let slice = unsafe { slice:: from_raw_parts_mut ( ptr. as_ptr ( ) , len) } ;
275- Box ( Unique :: from ( slice) )
274+ } ;
275+ Box :: from_raw ( slice:: from_raw_parts_mut ( ptr. as_ptr ( ) , len) )
276+ }
276277 }
277278}
278279
@@ -308,7 +309,7 @@ impl<T> Box<mem::MaybeUninit<T>> {
308309 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
309310 #[ inline]
310311 pub unsafe fn assume_init ( self ) -> Box < T > {
311- Box ( Box :: into_unique ( self ) . cast ( ) )
312+ Box :: from_raw ( Box :: into_raw ( self ) as * mut T )
312313 }
313314}
314315
@@ -346,7 +347,7 @@ impl<T> Box<[mem::MaybeUninit<T>]> {
346347 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
347348 #[ inline]
348349 pub unsafe fn assume_init ( self ) -> Box < [ T ] > {
349- Box ( Unique :: new_unchecked ( Box :: into_raw ( self ) as _ ) )
350+ Box :: from_raw ( Box :: into_raw ( self ) as * mut [ T ] )
350351 }
351352}
352353
0 commit comments