2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Unsafe operations"];
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-20 17:12:30 -05:00
|
|
|
export reinterpret_cast, forget;
|
2012-01-17 19:28:21 -06:00
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
#[abi = "rust-intrinsic"]
|
|
|
|
native mod rusti {
|
|
|
|
fn cast<T, U>(src: T) -> U;
|
|
|
|
fn leak<T>(-thing: T);
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "
|
2011-12-13 18:25:51 -06:00
|
|
|
Casts the value at `src` to U. The two types must have the same length.
|
2012-03-06 21:09:32 -06:00
|
|
|
"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2011-12-13 18:25:51 -06:00
|
|
|
unsafe fn reinterpret_cast<T, U>(src: T) -> U {
|
|
|
|
let t1 = sys::get_type_desc::<T>();
|
|
|
|
let t2 = sys::get_type_desc::<U>();
|
|
|
|
if (*t1).size != (*t2).size {
|
|
|
|
fail "attempt to cast values of differing sizes";
|
|
|
|
}
|
|
|
|
ret rusti::cast(src);
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc ="
|
|
|
|
Move a thing into the void
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-20 17:12:30 -05:00
|
|
|
The forget function will take ownership of the provided value but neglect
|
2011-12-13 18:25:51 -06: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 21:09:32 -06:00
|
|
|
"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2012-03-20 17:12:30 -05:00
|
|
|
unsafe fn forget<T>(-thing: T) { rusti::leak(thing); }
|
2012-01-17 19:28:21 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reinterpret_cast() unsafe {
|
|
|
|
assert reinterpret_cast(1) == 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
#[ignore(cfg(target_os = "win32"))]
|
|
|
|
fn test_reinterpret_cast_wrong_size() unsafe {
|
|
|
|
let _i: uint = reinterpret_cast(0u8);
|
|
|
|
}
|
|
|
|
}
|