2012-03-06 19:09:32 -08:00
|
|
|
#[doc = "Unsafe operations"];
|
2011-12-13 16:25:51 -08:00
|
|
|
|
2012-06-07 23:36:34 -07:00
|
|
|
export reinterpret_cast, forget, transmute;
|
2012-01-17 17:28:21 -08:00
|
|
|
|
2012-03-23 15:05:16 +01:00
|
|
|
#[abi = "rust-intrinsic"]
|
2011-12-13 16:25:51 -08:00
|
|
|
native mod rusti {
|
2012-03-22 12:30:10 +01:00
|
|
|
fn forget<T>(-x: T);
|
|
|
|
|
fn reinterpret_cast<T, U>(e: T) -> U;
|
2011-12-13 16:25:51 -08:00
|
|
|
}
|
|
|
|
|
|
2012-03-06 19:09:32 -08:00
|
|
|
#[doc = "
|
2011-12-13 16:25:51 -08:00
|
|
|
Casts the value at `src` to U. The two types must have the same length.
|
2012-03-06 19:09:32 -08:00
|
|
|
"]
|
2012-03-06 11:20:43 -08:00
|
|
|
#[inline(always)]
|
2011-12-13 16:25:51 -08:00
|
|
|
unsafe fn reinterpret_cast<T, U>(src: T) -> U {
|
2012-03-22 12:30:10 +01:00
|
|
|
rusti::reinterpret_cast(src)
|
2011-12-13 16:25:51 -08:00
|
|
|
}
|
|
|
|
|
|
2012-03-06 19:09:32 -08:00
|
|
|
#[doc ="
|
|
|
|
|
Move a thing into the void
|
2011-12-13 16:25:51 -08:00
|
|
|
|
2012-03-20 15:12:30 -07:00
|
|
|
The forget function will take ownership of the provided value but neglect
|
2011-12-13 16:25:51 -08:00
|
|
|
to run any required cleanup or memory-management operations on it. This
|
|
|
|
|
can be used for various acts of magick, particularly when using
|
|
|
|
|
reinterpret_cast on managed pointer types.
|
2012-03-06 19:09:32 -08:00
|
|
|
"]
|
2012-03-06 11:20:43 -08:00
|
|
|
#[inline(always)]
|
2012-03-22 12:30:10 +01:00
|
|
|
unsafe fn forget<T>(-thing: T) { rusti::forget(thing); }
|
2012-01-17 17:28:21 -08:00
|
|
|
|
2012-06-07 23:36:34 -07:00
|
|
|
#[doc = "
|
|
|
|
|
Transform a value of one type into a value of another type.
|
|
|
|
|
Both types must have the same size and alignment.
|
|
|
|
|
|
|
|
|
|
# Example
|
|
|
|
|
|
|
|
|
|
assert transmute(\"L\") == [76u8, 0u8];
|
|
|
|
|
"]
|
|
|
|
|
unsafe fn transmute<L, G>(-thing: L) -> G {
|
|
|
|
|
let newthing = reinterpret_cast(thing);
|
|
|
|
|
forget(thing);
|
|
|
|
|
ret newthing;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-17 17:28:21 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
|
|
#[test]
|
2012-06-24 20:18:18 -07:00
|
|
|
fn test_reinterpret_cast() {
|
|
|
|
|
assert unsafe { reinterpret_cast(1) } == 1u;
|
2012-01-17 17:28:21 -08:00
|
|
|
}
|
2012-06-07 23:36:34 -07:00
|
|
|
|
|
|
|
|
#[test]
|
2012-06-24 20:18:18 -07:00
|
|
|
fn test_transmute() {
|
|
|
|
|
unsafe {
|
|
|
|
|
let x = @1;
|
|
|
|
|
let x: *int = transmute(x);
|
|
|
|
|
assert *x == 1;
|
|
|
|
|
let _x: @int = transmute(x);
|
|
|
|
|
}
|
2012-06-07 23:36:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2012-06-24 20:18:18 -07:00
|
|
|
fn test_transmute2() {
|
|
|
|
|
unsafe {
|
|
|
|
|
assert transmute("L") == [76u8, 0u8];
|
|
|
|
|
}
|
2012-06-07 23:36:34 -07:00
|
|
|
}
|
2012-01-17 17:28:21 -08:00
|
|
|
}
|