Skip to content

Commit 07c5e5d

Browse files
committed
std: Clean up the swap function a little
1 parent 1b77331 commit 07c5e5d

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/libstd/util.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,16 @@ pub fn id<T>(x: T) -> T { x }
2626
pub fn swap<T>(x: &mut T, y: &mut T) {
2727
unsafe {
2828
// Give ourselves some scratch space to work with
29-
let mut tmp: T = mem::uninit();
30-
let t: *mut T = &mut tmp;
29+
let mut t: T = mem::uninit();
3130

3231
// Perform the swap, `&mut` pointers never alias
33-
let x_raw: *mut T = x;
34-
let y_raw: *mut T = y;
35-
ptr::copy_nonoverlapping_memory(t, &*x_raw, 1);
36-
ptr::copy_nonoverlapping_memory(x, &*y_raw, 1);
37-
ptr::copy_nonoverlapping_memory(y, &*t, 1);
32+
ptr::copy_nonoverlapping_memory(&mut t, &*x, 1);
33+
ptr::copy_nonoverlapping_memory(x, &*y, 1);
34+
ptr::copy_nonoverlapping_memory(y, &t, 1);
3835

3936
// y and t now point to the same thing, but we need to completely forget `tmp`
4037
// because it's no longer relevant.
41-
cast::forget(tmp);
38+
cast::forget(t);
4239
}
4340
}
4441

0 commit comments

Comments
 (0)