diff --git a/src/libcore/unsafe.rs b/src/libcore/unsafe.rs index 7b99727b48a4..0c8261f71ad5 100644 --- a/src/libcore/unsafe.rs +++ b/src/libcore/unsafe.rs @@ -1,6 +1,6 @@ #[doc = "Unsafe operations"]; -export reinterpret_cast, forget; +export reinterpret_cast, forget, transmute; #[abi = "rust-intrinsic"] native mod rusti { @@ -27,6 +27,20 @@ reinterpret_cast on managed pointer types. #[inline(always)] unsafe fn forget(-thing: T) { rusti::forget(thing); } +#[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(-thing: L) -> G { + let newthing = reinterpret_cast(thing); + forget(thing); + ret newthing; +} + #[cfg(test)] mod tests { @@ -34,4 +48,17 @@ mod tests { fn test_reinterpret_cast() unsafe { assert reinterpret_cast(1) == 1u; } + + #[test] + fn test_transmute() unsafe { + let x = @1; + let x: *int = transmute(x); + assert *x == 1; + let _x: @int = transmute(x); + } + + #[test] + fn test_transmute2() unsafe { + assert transmute("L") == [76u8, 0u8]; + } }