@@ -79,7 +79,7 @@ use ops::CoerceUnsized;
7979use fmt;
8080use hash;
8181use marker:: { PhantomData , Unsize } ;
82- use mem;
82+ use mem:: { self , MaybeUninit } ;
8383use nonzero:: NonZero ;
8484
8585use cmp:: Ordering :: { self , Less , Equal , Greater } ;
@@ -296,16 +296,12 @@ pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
296296#[ stable( feature = "rust1" , since = "1.0.0" ) ]
297297pub unsafe fn swap < T > ( x : * mut T , y : * mut T ) {
298298 // Give ourselves some scratch space to work with
299- let mut tmp: T = mem :: uninitialized ( ) ;
299+ let mut tmp = MaybeUninit :: < T > :: uninitialized ( ) ;
300300
301301 // Perform the swap
302- copy_nonoverlapping ( x, & mut tmp, 1 ) ;
302+ copy_nonoverlapping ( x, tmp. as_mut_ptr ( ) , 1 ) ;
303303 copy ( y, x, 1 ) ; // `x` and `y` may overlap
304- copy_nonoverlapping ( & tmp, y, 1 ) ;
305-
306- // y and t now point to the same thing, but we need to completely forget `tmp`
307- // because it's no longer relevant.
308- mem:: forget ( tmp) ;
304+ copy_nonoverlapping ( tmp. get_ref ( ) , y, 1 ) ;
309305}
310306
311307/// Swaps `count * size_of::<T>()` bytes between the two regions of memory
@@ -392,8 +388,8 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
392388 while i + block_size <= len {
393389 // Create some uninitialized memory as scratch space
394390 // Declaring `t` here avoids aligning the stack when this loop is unused
395- let mut t: Block = mem:: uninitialized ( ) ;
396- let t = & mut t as * mut _ as * mut u8 ;
391+ let mut t = mem:: MaybeUninit :: < Block > :: uninitialized ( ) ;
392+ let t = t . as_mut_ptr ( ) as * mut u8 ;
397393 let x = x. add ( i) ;
398394 let y = y. add ( i) ;
399395
@@ -407,10 +403,10 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
407403
408404 if i < len {
409405 // Swap any remaining bytes
410- let mut t: UnalignedBlock = mem:: uninitialized ( ) ;
406+ let mut t = mem:: MaybeUninit :: < UnalignedBlock > :: uninitialized ( ) ;
411407 let rem = len - i;
412408
413- let t = & mut t as * mut _ as * mut u8 ;
409+ let t = t . as_mut_ptr ( ) as * mut u8 ;
414410 let x = x. add ( i) ;
415411 let y = y. add ( i) ;
416412
@@ -575,9 +571,9 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
575571#[ inline]
576572#[ stable( feature = "rust1" , since = "1.0.0" ) ]
577573pub unsafe fn read < T > ( src : * const T ) -> T {
578- let mut tmp: T = mem :: uninitialized ( ) ;
579- copy_nonoverlapping ( src, & mut tmp, 1 ) ;
580- tmp
574+ let mut tmp = MaybeUninit :: < T > :: uninitialized ( ) ;
575+ copy_nonoverlapping ( src, tmp. as_mut_ptr ( ) , 1 ) ;
576+ tmp. into_inner ( )
581577}
582578
583579/// Reads the value from `src` without moving it. This leaves the
@@ -642,11 +638,11 @@ pub unsafe fn read<T>(src: *const T) -> T {
642638#[ inline]
643639#[ stable( feature = "ptr_unaligned" , since = "1.17.0" ) ]
644640pub unsafe fn read_unaligned < T > ( src : * const T ) -> T {
645- let mut tmp: T = mem :: uninitialized ( ) ;
641+ let mut tmp = MaybeUninit :: < T > :: uninitialized ( ) ;
646642 copy_nonoverlapping ( src as * const u8 ,
647- & mut tmp as * mut T as * mut u8 ,
643+ tmp. as_mut_ptr ( ) as * mut u8 ,
648644 mem:: size_of :: < T > ( ) ) ;
649- tmp
645+ tmp. into_inner ( )
650646}
651647
652648/// Overwrites a memory location with the given value without reading or
0 commit comments