Skip to content

Commit 3fb03d0

Browse files
committed
use MaybeUninit in core::ptr::swap
Code by @japaric, I just split it into individual commits
1 parent 0bb2e2d commit 3fb03d0

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/libcore/ptr.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -295,17 +295,14 @@ pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
295295
#[inline]
296296
#[stable(feature = "rust1", since = "1.0.0")]
297297
pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
298-
// Give ourselves some scratch space to work with
299-
let mut tmp: T = mem::uninitialized();
298+
// Give ourselves some scratch space to work with.
299+
// We do not have to worry about drops: `MaybeUninit` does nothing when dropped.
300+
let mut tmp = MaybeUninit::<T>::uninitialized();
300301

301302
// Perform the swap
302-
copy_nonoverlapping(x, &mut tmp, 1);
303+
copy_nonoverlapping(x, tmp.as_mut_ptr(), 1);
303304
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);
305+
copy_nonoverlapping(tmp.get_ref(), y, 1);
309306
}
310307

311308
/// Swaps `count * size_of::<T>()` bytes between the two regions of memory

0 commit comments

Comments
 (0)