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 ptr::copy_nonoverlapping_memory;
17 /// Casts the value at `src` to U. The two types must have the same length.
19 pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
20 let mut dest: U = mem::uninit();
21 let dest_ptr: *mut u8 = transmute(&mut dest);
22 let src_ptr: *u8 = transmute(src);
23 copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::<U>());
28 * Move a thing into the void
30 * The forget function will take ownership of the provided value but neglect
31 * to run any required cleanup or memory-management operations on it. This
32 * can be used for various acts of magick.
35 pub unsafe fn forget<T>(thing: T) { intrinsics::forget(thing); }
38 * Force-increment the reference count on a shared box. If used
39 * carelessly, this can leak the box.
42 pub unsafe fn bump_box_refcount<T>(t: @T) { forget(t); }
45 * Transform a value of one type into a value of another type.
46 * Both types must have the same size and alignment.
53 * let v: &[u8] = unsafe { cast::transmute("L") };
54 * assert!(v == [76u8]);
58 pub unsafe fn transmute<L, G>(thing: L) -> G {
59 intrinsics::transmute(thing)
62 /// Coerce an immutable reference to be mutable.
64 pub unsafe fn transmute_mut<'a,T>(ptr: &'a T) -> &'a mut T { transmute(ptr) }
66 /// Coerce a reference to have an arbitrary associated lifetime.
68 pub unsafe fn transmute_lifetime<'a,'b,T>(ptr: &'a T) -> &'b T {
72 /// Coerce an immutable reference to be mutable.
74 pub unsafe fn transmute_mut_unsafe<T>(ptr: *T) -> *mut T {
78 /// Coerce a mutable reference to have an arbitrary associated lifetime.
80 pub unsafe fn transmute_mut_lifetime<'a,'b,T>(ptr: &'a mut T) -> &'b mut T {
84 /// Transforms lifetime of the second pointer to match the first.
86 pub unsafe fn copy_lifetime<'a,S,T>(_ptr: &'a S, ptr: &T) -> &'a T {
87 transmute_lifetime(ptr)
90 /// Transforms lifetime of the second pointer to match the first.
92 pub unsafe fn copy_mut_lifetime<'a,S,T>(_ptr: &'a mut S, ptr: &mut T) -> &'a mut T {
93 transmute_mut_lifetime(ptr)
96 /// Transforms lifetime of the second pointer to match the first.
98 pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
99 transmute_lifetime(ptr)
103 /****************************************************************************
105 ****************************************************************************/
109 use cast::{bump_box_refcount, transmute};
113 fn test_transmute_copy() {
114 assert_eq!(1u, unsafe { ::cast::transmute_copy(&1) });
118 fn test_bump_managed_refcount() {
120 let managed = @~"box box box"; // refcount 1
121 bump_box_refcount(managed); // refcount 2
122 let ptr: *int = transmute(managed); // refcount 2
123 let _box1: @~str = ::cast::transmute_copy(&ptr);
124 let _box2: @~str = ::cast::transmute_copy(&ptr);
125 assert!(*_box1 == ~"box box box");
126 assert!(*_box2 == ~"box box box");
127 // Will destroy _box1 and _box2. Without the bump, this would
128 // use-after-free. With too many bumps, it would leak.
133 fn test_transmute() {
136 let x: *raw::Box<u8> = transmute(x);
137 assert!((*x).data == 100);
138 let _x: @int = transmute(x);
143 fn test_transmute2() {
145 assert_eq!(~[76u8], transmute(~"L"));