Skip to content

Commit 6f5be90

Browse files
committed
ptr: optimize {swap,replace,read}_ptr
1 parent f74250e commit 6f5be90

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/libstd/ptr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use cast;
1414
use option::{Option, Some, None};
1515
use sys;
1616
use unstable::intrinsics;
17+
use util::swap;
1718

1819
#[cfg(not(test))] use cmp::{Eq, Ord};
1920
use uint;
@@ -177,9 +178,9 @@ pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
177178
let t: *mut T = &mut tmp;
178179

179180
// Perform the swap
180-
copy_memory(t, x, 1);
181-
copy_memory(x, y, 1);
182-
copy_memory(y, t, 1);
181+
copy_nonoverlapping_memory(t, x, 1);
182+
copy_memory(x, y, 1); // `x` and `y` may overlap
183+
copy_nonoverlapping_memory(y, t, 1);
183184

184185
// y and t now point to the same thing, but we need to completely forget `tmp`
185186
// because it's no longer relevant.
@@ -192,7 +193,7 @@ pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
192193
*/
193194
#[inline]
194195
pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
195-
swap_ptr(dest, &mut src);
196+
swap(cast::transmute(dest), &mut src); // cannot overlap
196197
src
197198
}
198199

@@ -202,8 +203,7 @@ pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
202203
#[inline(always)]
203204
pub unsafe fn read_ptr<T>(src: *mut T) -> T {
204205
let mut tmp: T = intrinsics::uninit();
205-
let t: *mut T = &mut tmp;
206-
copy_memory(t, src, 1);
206+
copy_nonoverlapping_memory(&mut tmp, src, 1);
207207
tmp
208208
}
209209

0 commit comments

Comments
 (0)