2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-03-24 20:59:04 -05:00
|
|
|
//! Unsafe casting functions
|
|
|
|
|
2013-04-22 18:22:36 -05:00
|
|
|
use sys;
|
|
|
|
use unstable;
|
|
|
|
|
2013-03-05 13:57:50 -06:00
|
|
|
pub mod rusti {
|
|
|
|
#[abi = "rust-intrinsic"]
|
|
|
|
#[link_name = "rusti"]
|
2013-03-13 21:25:28 -05:00
|
|
|
pub extern "rust-intrinsic" {
|
2013-05-07 16:20:56 -05:00
|
|
|
fn forget<T>(x: T);
|
2013-04-22 18:22:36 -05:00
|
|
|
|
|
|
|
fn transmute<T,U>(e: T) -> U;
|
2013-03-05 13:57:50 -06:00
|
|
|
}
|
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.
|
2013-05-09 06:05:17 -05:00
|
|
|
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
|
|
|
|
let mut dest: U = unstable::intrinsics::init();
|
|
|
|
{
|
|
|
|
let dest_ptr: *mut u8 = rusti::transmute(&mut dest);
|
2013-04-22 18:22:36 -05:00
|
|
|
let src_ptr: *u8 = rusti::transmute(src);
|
|
|
|
unstable::intrinsics::memmove64(dest_ptr,
|
|
|
|
src_ptr,
|
|
|
|
sys::size_of::<U>() as u64);
|
|
|
|
}
|
|
|
|
dest
|
|
|
|
}
|
|
|
|
|
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
|
2012-10-05 02:59:37 -05:00
|
|
|
* reinterpret_cast on pointer types.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2013-02-15 02:51:28 -06:00
|
|
|
pub 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
|
2012-09-11 19:17:54 -05:00
|
|
|
* carelessly, this can leak the box. Use this in conjunction with transmute
|
2012-07-04 16:53:12 -05:00
|
|
|
* and/or reinterpret_cast when such calls would otherwise scramble a box's
|
|
|
|
* reference count
|
|
|
|
*/
|
2013-02-15 02:51:28 -06:00
|
|
|
pub unsafe fn bump_box_refcount<T>(t: @T) { forget(t); }
|
2012-06-27 12:11:57 -05:00
|
|
|
|
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
|
|
|
|
*
|
2013-03-28 20:39:09 -05:00
|
|
|
* assert!(transmute("L") == ~[76u8, 0u8]);
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-09-11 17:57:32 -05:00
|
|
|
#[inline(always)]
|
2013-04-22 18:22:36 -05:00
|
|
|
pub unsafe fn transmute<L, G>(thing: L) -> G {
|
|
|
|
rusti::transmute(thing)
|
|
|
|
}
|
|
|
|
|
2012-08-14 12:32:41 -05:00
|
|
|
/// Coerce an immutable reference to be mutable.
|
2012-09-11 17:57:32 -05:00
|
|
|
#[inline(always)]
|
2013-03-25 15:21:04 -05:00
|
|
|
pub unsafe fn transmute_mut<'a,T>(ptr: &'a T) -> &'a mut T { transmute(ptr) }
|
2012-08-27 18:08:17 -05:00
|
|
|
|
2012-08-14 12:32:41 -05:00
|
|
|
/// Coerce a mutable reference to be immutable.
|
2012-09-11 17:57:32 -05:00
|
|
|
#[inline(always)]
|
2013-03-25 15:21:04 -05:00
|
|
|
pub unsafe fn transmute_immut<'a,T>(ptr: &'a mut T) -> &'a T {
|
2013-02-15 02:51:28 -06:00
|
|
|
transmute(ptr)
|
2012-09-26 17:24:31 -05:00
|
|
|
}
|
2012-08-27 18:08:17 -05:00
|
|
|
|
2012-08-14 12:32:41 -05:00
|
|
|
/// Coerce a borrowed pointer to have an arbitrary associated region.
|
2012-09-11 17:57:32 -05:00
|
|
|
#[inline(always)]
|
2013-03-25 15:21:04 -05:00
|
|
|
pub unsafe fn transmute_region<'a,'b,T>(ptr: &'a T) -> &'b T {
|
|
|
|
transmute(ptr)
|
|
|
|
}
|
2012-08-27 18:08:17 -05:00
|
|
|
|
2012-09-12 12:38:17 -05:00
|
|
|
/// Coerce an immutable reference to be mutable.
|
2012-09-11 17:57:32 -05:00
|
|
|
#[inline(always)]
|
2012-10-02 13:37:37 -05:00
|
|
|
pub unsafe fn transmute_mut_unsafe<T>(ptr: *const T) -> *mut T {
|
2012-09-26 17:24:31 -05:00
|
|
|
transmute(ptr)
|
|
|
|
}
|
2012-09-12 12:38:17 -05:00
|
|
|
|
|
|
|
/// Coerce an immutable reference to be mutable.
|
2012-09-11 17:57:32 -05:00
|
|
|
#[inline(always)]
|
2012-10-02 13:37:37 -05:00
|
|
|
pub unsafe fn transmute_immut_unsafe<T>(ptr: *const T) -> *T {
|
2012-09-26 17:24:31 -05:00
|
|
|
transmute(ptr)
|
|
|
|
}
|
2012-09-12 12:38:17 -05:00
|
|
|
|
2012-08-14 12:32:41 -05:00
|
|
|
/// Coerce a borrowed mutable pointer to have an arbitrary associated region.
|
2012-09-11 17:57:32 -05:00
|
|
|
#[inline(always)]
|
2013-03-25 15:21:04 -05:00
|
|
|
pub unsafe fn transmute_mut_region<'a,'b,T>(ptr: &'a mut T) -> &'b mut T {
|
2013-02-15 02:51:28 -06:00
|
|
|
transmute(ptr)
|
2012-08-14 12:32:41 -05:00
|
|
|
}
|
|
|
|
|
2012-08-27 18:08:17 -05:00
|
|
|
/// Transforms lifetime of the second pointer to match the first.
|
2012-09-11 17:57:32 -05:00
|
|
|
#[inline(always)]
|
2013-03-25 15:21:04 -05:00
|
|
|
pub unsafe fn copy_lifetime<'a,S,T>(_ptr: &'a S, ptr: &T) -> &'a T {
|
2012-08-27 18:08:17 -05:00
|
|
|
transmute_region(ptr)
|
|
|
|
}
|
|
|
|
|
2013-05-08 14:03:39 -05:00
|
|
|
/// Transforms lifetime of the second pointer to match the first.
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn copy_mut_lifetime<'a,S,T>(_ptr: &'a mut S, ptr: &mut T) -> &'a mut T {
|
|
|
|
transmute_mut_region(ptr)
|
|
|
|
}
|
|
|
|
|
2012-09-12 12:38:17 -05:00
|
|
|
/// Transforms lifetime of the second pointer to match the first.
|
2012-09-18 23:41:37 -05:00
|
|
|
#[inline(always)]
|
2013-03-25 15:21:04 -05:00
|
|
|
pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
|
2012-09-18 23:41:37 -05:00
|
|
|
transmute_region(ptr)
|
2012-09-12 12:38:17 -05:00
|
|
|
}
|
|
|
|
|
2012-08-27 18:08:17 -05:00
|
|
|
|
2012-08-10 17:20:03 -05:00
|
|
|
/****************************************************************************
|
|
|
|
* Tests
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[cfg(test)]
|
2013-04-15 10:08:52 -05:00
|
|
|
mod tests {
|
2013-04-22 19:34:42 -05:00
|
|
|
use cast::{bump_box_refcount, transmute};
|
2013-01-08 21:37:25 -06:00
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[test]
|
2013-04-22 19:34:42 -05:00
|
|
|
fn test_transmute_copy() {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(1u, unsafe { ::cast::transmute_copy(&1) });
|
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]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_bump_box_refcount() {
|
2012-06-27 12:11:57 -05:00
|
|
|
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
|
2013-04-26 16:04:39 -05:00
|
|
|
let _box1: @~str = ::cast::transmute_copy(&ptr);
|
|
|
|
let _box2: @~str = ::cast::transmute_copy(&ptr);
|
2013-03-28 20:39:09 -05:00
|
|
|
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]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_transmute() {
|
2012-12-12 20:41:30 -06:00
|
|
|
use managed::raw::BoxRepr;
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
2012-12-12 20:41:30 -06:00
|
|
|
let x = @100u8;
|
2013-02-15 02:51:28 -06:00
|
|
|
let x: *BoxRepr = transmute(x);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((*x).data == 100);
|
2013-02-15 02:51:28 -06:00
|
|
|
let _x: @int = transmute(x);
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-06-08 01:36:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_transmute2() {
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(~[76u8, 0u8], transmute(~"L"));
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-06-08 01:36:34 -05:00
|
|
|
}
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|