1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Unsafe casting functions
15 use unstable::intrinsics;
16 use ptr::copy_nonoverlapping_memory;
18 /// Casts the value at `src` to U. The two types must have the same length.
20 pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
21 let mut dest: U = intrinsics::uninit();
22 let dest_ptr: *mut u8 = transmute(&mut dest);
23 let src_ptr: *u8 = transmute(src);
24 copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::<U>());
29 * Move a thing into the void
31 * The forget function will take ownership of the provided value but neglect
32 * to run any required cleanup or memory-management operations on it. This
33 * can be used for various acts of magick.
36 pub unsafe fn forget<T>(thing: T) { intrinsics::forget(thing); }
39 * Force-increment the reference count on a shared box. If used
40 * carelessly, this can leak the box.
43 pub unsafe fn bump_box_refcount<T>(t: @T) { forget(t); }
46 * Transform a value of one type into a value of another type.
47 * Both types must have the same size and alignment.
52 * let v: &[u8] = transmute("L");
53 * assert!(v == [76u8]);
57 pub unsafe fn transmute<L, G>(thing: L) -> G {
58 intrinsics::transmute(thing)
61 /// Coerce an immutable reference to be mutable.
63 pub unsafe fn transmute_mut<'a,T>(ptr: &'a T) -> &'a mut T { transmute(ptr) }
65 /// Coerce a mutable reference to be immutable.
67 pub unsafe fn transmute_immut<'a,T>(ptr: &'a mut T) -> &'a T {
71 /// Coerce a borrowed pointer to have an arbitrary associated region.
73 pub unsafe fn transmute_region<'a,'b,T>(ptr: &'a T) -> &'b T {
77 /// Coerce an immutable reference to be mutable.
79 pub unsafe fn transmute_mut_unsafe<T,P:RawPtr<T>>(ptr: P) -> *mut T {
83 /// Coerce an immutable reference to be mutable.
85 pub unsafe fn transmute_immut_unsafe<T,P:RawPtr<T>>(ptr: P) -> *T {
89 /// Coerce a borrowed mutable pointer to have an arbitrary associated region.
91 pub unsafe fn transmute_mut_region<'a,'b,T>(ptr: &'a mut T) -> &'b mut T {
95 /// Transforms lifetime of the second pointer to match the first.
97 pub unsafe fn copy_lifetime<'a,S,T>(_ptr: &'a S, ptr: &T) -> &'a T {
101 /// Transforms lifetime of the second pointer to match the first.
103 pub unsafe fn copy_mut_lifetime<'a,S,T>(_ptr: &'a mut S, ptr: &mut T) -> &'a mut T {
104 transmute_mut_region(ptr)
107 /// Transforms lifetime of the second pointer to match the first.
109 pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
110 transmute_region(ptr)
114 /****************************************************************************
116 ****************************************************************************/
120 use cast::{bump_box_refcount, transmute};
124 fn test_transmute_copy() {
125 assert_eq!(1u, unsafe { ::cast::transmute_copy(&1) });
129 fn test_bump_box_refcount() {
131 let box = @~"box box box"; // refcount 1
132 bump_box_refcount(box); // refcount 2
133 let ptr: *int = transmute(box); // refcount 2
134 let _box1: @~str = ::cast::transmute_copy(&ptr);
135 let _box2: @~str = ::cast::transmute_copy(&ptr);
136 assert!(*_box1 == ~"box box box");
137 assert!(*_box2 == ~"box box box");
138 // Will destroy _box1 and _box2. Without the bump, this would
139 // use-after-free. With too many bumps, it would leak.
144 fn test_transmute() {
147 let x: *raw::Box<u8> = transmute(x);
148 assert!((*x).data == 100);
149 let _x: @int = transmute(x);
154 fn test_transmute2() {
156 assert_eq!(~[76u8], transmute(~"L"));