From 3fb03d0650825ba254f67fdd5ac94e83e2622415 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 12 Oct 2018 08:59:38 +0200 Subject: [PATCH] use MaybeUninit in core::ptr::swap Code by @japaric, I just split it into individual commits --- src/libcore/ptr.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 405f95acf195..5032c112f7cc 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -295,17 +295,14 @@ pub const fn null_mut() -> *mut T { 0 as *mut T } #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn swap(x: *mut T, y: *mut T) { - // Give ourselves some scratch space to work with - let mut tmp: T = mem::uninitialized(); + // Give ourselves some scratch space to work with. + // We do not have to worry about drops: `MaybeUninit` does nothing when dropped. + let mut tmp = MaybeUninit::::uninitialized(); // Perform the swap - copy_nonoverlapping(x, &mut tmp, 1); + copy_nonoverlapping(x, tmp.as_mut_ptr(), 1); copy(y, x, 1); // `x` and `y` may overlap - copy_nonoverlapping(&tmp, y, 1); - - // y and t now point to the same thing, but we need to completely forget `tmp` - // because it's no longer relevant. - mem::forget(tmp); + copy_nonoverlapping(tmp.get_ref(), y, 1); } /// Swaps `count * size_of::()` bytes between the two regions of memory