2012-07-04 16:53:12 -05:00
|
|
|
//! Unsafe operations
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-06-27 12:11:57 -05:00
|
|
|
export reinterpret_cast, forget, bump_box_refcount, transmute;
|
2012-01-17 19:28:21 -06:00
|
|
|
|
2012-03-23 09:05:16 -05:00
|
|
|
#[abi = "rust-intrinsic"]
|
2012-07-03 18:11:00 -05:00
|
|
|
extern mod rusti {
|
2012-03-22 06:30:10 -05:00
|
|
|
fn forget<T>(-x: T);
|
|
|
|
fn reinterpret_cast<T, U>(e: T) -> U;
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Casts the value at `src` to U. The two types must have the same length.
|
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 {
|
2012-03-22 06:30:10 -05:00
|
|
|
rusti::reinterpret_cast(src)
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2012-03-22 06:30:10 -05:00
|
|
|
unsafe fn forget<T>(-thing: T) { rusti::forget(thing); }
|
2012-01-17 19:28:21 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Force-increment the reference count on a shared box. If used
|
|
|
|
* uncarefully, 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-06-27 12:11:57 -05:00
|
|
|
unsafe fn bump_box_refcount<T>(+t: @T) { forget(t); }
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Transform a value of one type into a value of another type.
|
|
|
|
* Both types must have the same size and alignment.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
2012-07-11 18:49:02 -05:00
|
|
|
* assert transmute("L") == ~[76u8, 0u8];
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-06-08 01:36:34 -05:00
|
|
|
unsafe fn transmute<L, G>(-thing: L) -> G {
|
|
|
|
let newthing = reinterpret_cast(thing);
|
|
|
|
forget(thing);
|
|
|
|
ret newthing;
|
|
|
|
}
|
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
#[test]
|
2012-06-24 22:18:18 -05:00
|
|
|
fn test_reinterpret_cast() {
|
|
|
|
assert unsafe { reinterpret_cast(1) } == 1u;
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
2012-06-08 01:36:34 -05:00
|
|
|
|
2012-06-27 12:11:57 -05:00
|
|
|
#[test]
|
|
|
|
fn test_bump_box_refcount() {
|
|
|
|
unsafe {
|
2012-07-14 00:57:48 -05:00
|
|
|
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-07-14 00:57:48 -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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 01:36:34 -05:00
|
|
|
#[test]
|
2012-06-24 22:18:18 -05:00
|
|
|
fn test_transmute() {
|
|
|
|
unsafe {
|
|
|
|
let x = @1;
|
|
|
|
let x: *int = transmute(x);
|
|
|
|
assert *x == 1;
|
|
|
|
let _x: @int = transmute(x);
|
|
|
|
}
|
2012-06-08 01:36:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-06-24 22:18:18 -05:00
|
|
|
fn test_transmute2() {
|
|
|
|
unsafe {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert transmute(~"L") == ~[76u8, 0u8];
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-06-08 01:36:34 -05:00
|
|
|
}
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|