1
Fork 0

mv the raw pointer {swap,replace}_ptr to std::ptr

This commit is contained in:
Daniel Micay 2013-05-31 10:21:29 -04:00
parent 030f471f26
commit 29aba8033a
6 changed files with 48 additions and 48 deletions

View file

@ -15,6 +15,7 @@ use cast;
#[cfg(stage0)] use libc::{c_void, size_t};
use option::{Option, Some, None};
use sys;
use unstable::intrinsics;
#[cfg(not(test))] use cmp::{Eq, Ord};
use uint;
@ -206,6 +207,36 @@ pub unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint) {
memset64(dst, c, count as u64);
}
/**
* Swap the values at two mutable locations of the same type, without
* deinitialising or copying either one.
*/
#[inline]
pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
// Give ourselves some scratch space to work with
let mut tmp: T = intrinsics::uninit();
let t: *mut T = &mut tmp;
// Perform the swap
copy_memory(t, x, 1);
copy_memory(x, y, 1);
copy_memory(y, t, 1);
// y and t now point to the same thing, but we need to completely forget `tmp`
// because it's no longer relevant.
cast::forget(tmp);
}
/**
* Replace the value at a mutable location with a new one, returning the old
* value, without deinitialising or copying either one.
*/
#[inline(always)]
pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
swap_ptr(dest, &mut src);
src
}
/**
Transform a region pointer - &T - to an unsafe pointer - *T.
This is safe, but is implemented with an unsafe block due to