rust/src/libcore/cast.rs

137 lines
4.0 KiB
Rust
Raw Normal View History

//! Unsafe operations
2012-06-27 12:11:57 -05:00
export reinterpret_cast, forget, bump_box_refcount, transmute;
export transmute_mut, transmute_immut, transmute_region, transmute_mut_region;
export transmute_mut_unsafe, transmute_immut_unsafe;
2012-01-17 19:28:21 -06:00
export copy_lifetime;
#[abi = "rust-intrinsic"]
extern mod rusti {
fn forget<T>(-x: T);
fn reinterpret_cast<T, U>(e: T) -> U;
}
/// Casts the value at `src` to U. The two types must have the same length.
#[inline(always)]
2012-08-29 18:00:36 -05:00
unsafe fn reinterpret_cast<T, U>(src: &T) -> U {
rusti::reinterpret_cast(*src)
}
/**
* Move a thing into the void
*
* The forget function will take ownership of the provided value but neglect
* 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.
*/
#[inline(always)]
2012-09-11 19:17:54 -05:00
unsafe fn forget<T>(-thing: T) { rusti::forget(move thing); }
2012-01-17 19:28:21 -06:00
/**
* Force-increment the reference count on a shared box. If used
2012-09-11 19:17:54 -05:00
* carelessly, this can leak the box. Use this in conjunction with transmute
* and/or reinterpret_cast when such calls would otherwise scramble a box's
* reference count
*/
2012-09-11 19:17:54 -05:00
unsafe fn bump_box_refcount<T>(+t: @T) { forget(move t); }
2012-06-27 12:11:57 -05:00
/**
* 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];
*/
2012-09-11 17:57:32 -05:00
#[inline(always)]
unsafe fn transmute<L, G>(-thing: L) -> G {
2012-09-11 19:17:54 -05:00
let newthing: G = reinterpret_cast(&thing);
forget(move thing);
move newthing
}
/// Coerce an immutable reference to be mutable.
2012-09-11 17:57:32 -05:00
#[inline(always)]
2012-09-11 19:17:54 -05:00
unsafe fn transmute_mut<T>(+ptr: &a/T) -> &a/mut T { transmute(move ptr) }
/// Coerce a mutable reference to be immutable.
2012-09-11 17:57:32 -05:00
#[inline(always)]
2012-09-11 19:17:54 -05:00
unsafe fn transmute_immut<T>(+ptr: &a/mut T) -> &a/T { transmute(move ptr) }
/// Coerce a borrowed pointer to have an arbitrary associated region.
2012-09-11 17:57:32 -05:00
#[inline(always)]
2012-09-11 19:17:54 -05:00
unsafe fn transmute_region<T>(+ptr: &a/T) -> &b/T { transmute(move ptr) }
/// Coerce an immutable reference to be mutable.
2012-09-11 17:57:32 -05:00
#[inline(always)]
unsafe fn transmute_mut_unsafe<T>(+ptr: *const T) -> *mut T { transmute(ptr) }
/// Coerce an immutable reference to be mutable.
2012-09-11 17:57:32 -05:00
#[inline(always)]
unsafe fn transmute_immut_unsafe<T>(+ptr: *const T) -> *T { transmute(ptr) }
/// Coerce a borrowed mutable pointer to have an arbitrary associated region.
2012-09-11 17:57:32 -05:00
#[inline(always)]
unsafe fn transmute_mut_region<T>(+ptr: &a/mut T) -> &b/mut T {
2012-09-11 19:17:54 -05:00
transmute(move ptr)
}
/// Transforms lifetime of the second pointer to match the first.
2012-09-11 17:57:32 -05:00
#[inline(always)]
unsafe fn copy_lifetime<S,T>(_ptr: &a/S, ptr: &T) -> &a/T {
transmute_region(ptr)
}
/// Transforms lifetime of the second pointer to match the first.
unsafe fn copy_lifetime_to_unsafe<S,T>(_ptr: &a/S, +ptr: *T) -> &a/T {
transmute(ptr)
}
/****************************************************************************
* Tests
****************************************************************************/
2012-01-17 19:28:21 -06:00
#[cfg(test)]
mod tests {
#[test]
fn test_reinterpret_cast() {
assert 1u == unsafe { reinterpret_cast(&1) };
2012-01-17 19:28:21 -06:00
}
2012-06-27 12:11:57 -05:00
#[test]
fn test_bump_box_refcount() {
unsafe {
let box = @~"box box box"; // refcount 1
2012-06-27 12:11:57 -05:00
bump_box_refcount(box); // refcount 2
let ptr: *int = transmute(box); // refcount 2
2012-08-29 18:00:36 -05:00
let _box1: @~str = reinterpret_cast(&ptr);
let _box2: @~str = reinterpret_cast(&ptr);
assert *_box1 == ~"box box box";
assert *_box2 == ~"box box box";
2012-06-27 12:11:57 -05:00
// Will destroy _box1 and _box2. Without the bump, this would
// use-after-free. With too many bumps, it would leak.
}
}
#[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 ~[76u8, 0u8] == transmute(~"L");
}
}
2012-01-17 19:28:21 -06:00
}