2012-07-04 22:53:12 +01:00
|
|
|
//! Unsafe operations
|
2012-10-03 14:52:09 -07:00
|
|
|
#[forbid(deprecated_mode)]
|
2011-12-13 16:25:51 -08:00
|
|
|
|
2012-03-23 15:05:16 +01:00
|
|
|
#[abi = "rust-intrinsic"]
|
2012-07-03 16:11:00 -07:00
|
|
|
extern mod rusti {
|
2012-03-22 12:30:10 +01:00
|
|
|
fn forget<T>(-x: T);
|
2012-10-02 12:19:04 -07:00
|
|
|
fn reinterpret_cast<T, U>(&&e: T) -> U;
|
2011-12-13 16:25:51 -08:00
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Casts the value at `src` to U. The two types must have the same length.
|
2012-03-06 11:20:43 -08:00
|
|
|
#[inline(always)]
|
2012-09-26 15:24:31 -07:00
|
|
|
pub unsafe fn reinterpret_cast<T, U>(src: &T) -> U {
|
2012-08-29 16:00:36 -07:00
|
|
|
rusti::reinterpret_cast(*src)
|
2011-12-13 16:25:51 -08:00
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01: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
|
2012-10-05 01:59:37 -06:00
|
|
|
* reinterpret_cast on pointer types.
|
2012-07-04 22:53:12 +01:00
|
|
|
*/
|
2012-03-06 11:20:43 -08:00
|
|
|
#[inline(always)]
|
2012-10-02 11:37:37 -07:00
|
|
|
pub unsafe fn forget<T>(thing: T) { rusti::forget(move thing); }
|
2012-01-17 17:28:21 -08:00
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/**
|
|
|
|
* Force-increment the reference count on a shared box. If used
|
2012-09-11 17:17:54 -07:00
|
|
|
* carelessly, this can leak the box. Use this in conjunction with transmute
|
2012-07-04 22:53:12 +01:00
|
|
|
* and/or reinterpret_cast when such calls would otherwise scramble a box's
|
|
|
|
* reference count
|
|
|
|
*/
|
2012-10-02 11:37:37 -07:00
|
|
|
pub unsafe fn bump_box_refcount<T>(t: @T) { forget(move t); }
|
2012-06-27 13:11:57 -04:00
|
|
|
|
2012-07-04 22:53:12 +01: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 16:49:02 -07:00
|
|
|
* assert transmute("L") == ~[76u8, 0u8];
|
2012-07-04 22:53:12 +01:00
|
|
|
*/
|
2012-09-11 15:57:32 -07:00
|
|
|
#[inline(always)]
|
2012-10-02 11:37:37 -07:00
|
|
|
pub unsafe fn transmute<L, G>(thing: L) -> G {
|
2012-09-11 17:17:54 -07:00
|
|
|
let newthing: G = reinterpret_cast(&thing);
|
|
|
|
forget(move thing);
|
2012-09-10 16:31:00 -07:00
|
|
|
move newthing
|
2012-06-07 23:36:34 -07:00
|
|
|
}
|
|
|
|
|
2012-08-14 13:32:41 -04:00
|
|
|
/// Coerce an immutable reference to be mutable.
|
2012-09-11 15:57:32 -07:00
|
|
|
#[inline(always)]
|
2012-10-02 11:37:37 -07:00
|
|
|
pub unsafe fn transmute_mut<T>(ptr: &a/T) -> &a/mut T { transmute(move ptr) }
|
2012-08-27 16:08:17 -07:00
|
|
|
|
2012-08-14 13:32:41 -04:00
|
|
|
/// Coerce a mutable reference to be immutable.
|
2012-09-11 15:57:32 -07:00
|
|
|
#[inline(always)]
|
2012-10-02 11:37:37 -07:00
|
|
|
pub unsafe fn transmute_immut<T>(ptr: &a/mut T) -> &a/T {
|
2012-09-26 15:24:31 -07:00
|
|
|
transmute(move ptr)
|
|
|
|
}
|
2012-08-27 16:08:17 -07:00
|
|
|
|
2012-08-14 13:32:41 -04:00
|
|
|
/// Coerce a borrowed pointer to have an arbitrary associated region.
|
2012-09-11 15:57:32 -07:00
|
|
|
#[inline(always)]
|
2012-10-02 11:37:37 -07:00
|
|
|
pub unsafe fn transmute_region<T>(ptr: &a/T) -> &b/T { transmute(move ptr) }
|
2012-08-27 16:08:17 -07:00
|
|
|
|
2012-09-12 10:38:17 -07:00
|
|
|
/// Coerce an immutable reference to be mutable.
|
2012-09-11 15:57:32 -07:00
|
|
|
#[inline(always)]
|
2012-10-02 11:37:37 -07:00
|
|
|
pub unsafe fn transmute_mut_unsafe<T>(ptr: *const T) -> *mut T {
|
2012-09-26 15:24:31 -07:00
|
|
|
transmute(ptr)
|
|
|
|
}
|
2012-09-12 10:38:17 -07:00
|
|
|
|
|
|
|
/// Coerce an immutable reference to be mutable.
|
2012-09-11 15:57:32 -07:00
|
|
|
#[inline(always)]
|
2012-10-02 11:37:37 -07:00
|
|
|
pub unsafe fn transmute_immut_unsafe<T>(ptr: *const T) -> *T {
|
2012-09-26 15:24:31 -07:00
|
|
|
transmute(ptr)
|
|
|
|
}
|
2012-09-12 10:38:17 -07:00
|
|
|
|
2012-08-14 13:32:41 -04:00
|
|
|
/// Coerce a borrowed mutable pointer to have an arbitrary associated region.
|
2012-09-11 15:57:32 -07:00
|
|
|
#[inline(always)]
|
2012-10-02 11:37:37 -07:00
|
|
|
pub unsafe fn transmute_mut_region<T>(ptr: &a/mut T) -> &b/mut T {
|
2012-09-11 17:17:54 -07:00
|
|
|
transmute(move ptr)
|
2012-08-14 13:32:41 -04:00
|
|
|
}
|
|
|
|
|
2012-08-27 16:08:17 -07:00
|
|
|
/// Transforms lifetime of the second pointer to match the first.
|
2012-09-11 15:57:32 -07:00
|
|
|
#[inline(always)]
|
2012-09-26 15:24:31 -07:00
|
|
|
pub unsafe fn copy_lifetime<S,T>(_ptr: &a/S, ptr: &T) -> &a/T {
|
2012-08-27 16:08:17 -07:00
|
|
|
transmute_region(ptr)
|
|
|
|
}
|
|
|
|
|
2012-09-12 10:38:17 -07:00
|
|
|
/// Transforms lifetime of the second pointer to match the first.
|
2012-09-18 21:41:37 -07:00
|
|
|
#[inline(always)]
|
2012-09-26 15:24:31 -07:00
|
|
|
pub unsafe fn copy_lifetime_vec<S,T>(_ptr: &a/[S], ptr: &T) -> &a/T {
|
2012-09-18 21:41:37 -07:00
|
|
|
transmute_region(ptr)
|
2012-09-12 10:38:17 -07:00
|
|
|
}
|
|
|
|
|
2012-08-27 16:08:17 -07:00
|
|
|
|
2012-08-10 18:20:03 -04:00
|
|
|
/****************************************************************************
|
|
|
|
* Tests
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-17 17:28:21 -08:00
|
|
|
#[cfg(test)]
|
2012-09-26 15:24:31 -07:00
|
|
|
pub mod tests {
|
2012-01-17 17:28:21 -08:00
|
|
|
#[test]
|
2012-09-26 15:24:31 -07:00
|
|
|
pub fn test_reinterpret_cast() {
|
2012-09-12 15:39:28 -07:00
|
|
|
assert 1u == unsafe { reinterpret_cast(&1) };
|
2012-01-17 17:28:21 -08:00
|
|
|
}
|
2012-06-07 23:36:34 -07:00
|
|
|
|
2012-06-27 13:11:57 -04:00
|
|
|
#[test]
|
2012-09-26 15:24:31 -07:00
|
|
|
pub fn test_bump_box_refcount() {
|
2012-06-27 13:11:57 -04:00
|
|
|
unsafe {
|
2012-07-13 22:57:48 -07:00
|
|
|
let box = @~"box box box"; // refcount 1
|
2012-06-27 13:11:57 -04:00
|
|
|
bump_box_refcount(box); // refcount 2
|
|
|
|
let ptr: *int = transmute(box); // refcount 2
|
2012-08-29 16:00:36 -07:00
|
|
|
let _box1: @~str = reinterpret_cast(&ptr);
|
|
|
|
let _box2: @~str = reinterpret_cast(&ptr);
|
2012-07-13 22:57:48 -07:00
|
|
|
assert *_box1 == ~"box box box";
|
|
|
|
assert *_box2 == ~"box box box";
|
2012-06-27 13:11:57 -04:00
|
|
|
// Will destroy _box1 and _box2. Without the bump, this would
|
|
|
|
// use-after-free. With too many bumps, it would leak.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 23:36:34 -07:00
|
|
|
#[test]
|
2012-09-26 15:24:31 -07:00
|
|
|
pub fn test_transmute() {
|
2012-06-24 20:18:18 -07:00
|
|
|
unsafe {
|
|
|
|
let x = @1;
|
|
|
|
let x: *int = transmute(x);
|
|
|
|
assert *x == 1;
|
|
|
|
let _x: @int = transmute(x);
|
|
|
|
}
|
2012-06-07 23:36:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 15:24:31 -07:00
|
|
|
pub fn test_transmute2() {
|
2012-06-24 20:18:18 -07:00
|
|
|
unsafe {
|
2012-09-12 15:39:28 -07:00
|
|
|
assert ~[76u8, 0u8] == transmute(~"L");
|
2012-06-24 20:18:18 -07:00
|
|
|
}
|
2012-06-07 23:36:34 -07:00
|
|
|
}
|
2012-01-17 17:28:21 -08:00
|
|
|
}
|