@@ -79,7 +79,7 @@ use ops::CoerceUnsized;
79
79
use fmt;
80
80
use hash;
81
81
use marker::{PhantomData, Unsize};
82
- use mem;
82
+ use mem::{self, MaybeUninit} ;
83
83
use nonzero::NonZero;
84
84
85
85
use cmp::Ordering::{self, Less, Equal, Greater};
@@ -296,16 +296,12 @@ pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
296
296
#[stable(feature = "rust1", since = "1.0.0")]
297
297
pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
298
298
// Give ourselves some scratch space to work with
299
- let mut tmp: T = mem ::uninitialized();
299
+ let mut tmp = MaybeUninit::<T> ::uninitialized();
300
300
301
301
// Perform the swap
302
- copy_nonoverlapping(x, &mut tmp, 1);
302
+ copy_nonoverlapping(x, tmp.as_mut_ptr() , 1);
303
303
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);
309
305
}
310
306
311
307
/// 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) {
392
388
while i + block_size <= len {
393
389
// Create some uninitialized memory as scratch space
394
390
// 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;
397
393
let x = x.add(i);
398
394
let y = y.add(i);
399
395
@@ -407,10 +403,10 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
407
403
408
404
if i < len {
409
405
// Swap any remaining bytes
410
- let mut t: UnalignedBlock = mem::uninitialized();
406
+ let mut t = mem::MaybeUninit::<UnalignedBlock> ::uninitialized();
411
407
let rem = len - i;
412
408
413
- let t = &mut t as *mut _ as *mut u8;
409
+ let t = t.as_mut_ptr() as *mut u8;
414
410
let x = x.add(i);
415
411
let y = y.add(i);
416
412
@@ -575,9 +571,9 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
575
571
#[inline]
576
572
#[stable(feature = "rust1", since = "1.0.0")]
577
573
pub 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()
581
577
}
582
578
583
579
/// Reads the value from `src` without moving it. This leaves the
@@ -642,11 +638,11 @@ pub unsafe fn read<T>(src: *const T) -> T {
642
638
#[inline]
643
639
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
644
640
pub unsafe fn read_unaligned<T>(src: *const T) -> T {
645
- let mut tmp: T = mem ::uninitialized();
641
+ let mut tmp = MaybeUninit::<T> ::uninitialized();
646
642
copy_nonoverlapping(src as *const u8,
647
- &mut tmp as *mut T as *mut u8,
643
+ tmp.as_mut_ptr() as *mut u8,
648
644
mem::size_of::<T>());
649
- tmp
645
+ tmp.into_inner()
650
646
}
651
647
652
648
/// Overwrites a memory location with the given value without reading or
0 commit comments