core: Add unsafe::transmute
Like reinterpret_cast + forget
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#[doc = "Unsafe operations"];
|
#[doc = "Unsafe operations"];
|
||||||
|
|
||||||
export reinterpret_cast, forget;
|
export reinterpret_cast, forget, transmute;
|
||||||
|
|
||||||
#[abi = "rust-intrinsic"]
|
#[abi = "rust-intrinsic"]
|
||||||
native mod rusti {
|
native mod rusti {
|
||||||
@@ -27,6 +27,20 @@ reinterpret_cast on managed pointer types.
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
unsafe fn forget<T>(-thing: T) { rusti::forget(thing); }
|
unsafe fn forget<T>(-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<L, G>(-thing: L) -> G {
|
||||||
|
let newthing = reinterpret_cast(thing);
|
||||||
|
forget(thing);
|
||||||
|
ret newthing;
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
@@ -34,4 +48,17 @@ mod tests {
|
|||||||
fn test_reinterpret_cast() unsafe {
|
fn test_reinterpret_cast() unsafe {
|
||||||
assert reinterpret_cast(1) == 1u;
|
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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user