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 borrowed pointer to have an arbitrary associated region.
67 pub unsafe fn transmute_region<'a,'b,T>(ptr: &'a T) -> &'b T {
71 /// Coerce an immutable reference to be mutable.
73 pub unsafe fn transmute_mut_unsafe<T,P:RawPtr<T>>(ptr: P) -> *mut T {
77 /// Coerce an immutable reference to be mutable.
79 pub unsafe fn transmute_immut_unsafe<T,P:RawPtr<T>>(ptr: P) -> *T {
83 /// Coerce a borrowed mutable pointer to have an arbitrary associated region.
85 pub unsafe fn transmute_mut_region<'a,'b,T>(ptr: &'a mut T) -> &'b mut T {
89 /// Transforms lifetime of the second pointer to match the first.
91 pub unsafe fn copy_lifetime<'a,S,T>(_ptr: &'a S, ptr: &T) -> &'a T {
95 /// Transforms lifetime of the second pointer to match the first.
97 pub unsafe fn copy_mut_lifetime<'a,S,T>(_ptr: &'a mut S, ptr: &mut T) -> &'a mut T {
98 transmute_mut_region(ptr)
101 /// Transforms lifetime of the second pointer to match the first.
103 pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
104 transmute_region(ptr)
108 /****************************************************************************
110 ****************************************************************************/
114 use cast::{bump_box_refcount, transmute};
118 fn test_transmute_copy() {
119 assert_eq!(1u, unsafe { ::cast::transmute_copy(&1) });
123 fn test_bump_managed_refcount() {
125 let managed = @~"box box box"; // refcount 1
126 bump_box_refcount(managed); // refcount 2
127 let ptr: *int = transmute(managed); // refcount 2
128 let _box1: @~str = ::cast::transmute_copy(&ptr);
129 let _box2: @~str = ::cast::transmute_copy(&ptr);
130 assert!(*_box1 == ~"box box box");
131 assert!(*_box2 == ~"box box box");
132 // Will destroy _box1 and _box2. Without the bump, this would
133 // use-after-free. With too many bumps, it would leak.
138 fn test_transmute() {
141 let x: *raw::Box<u8> = transmute(x);
142 assert!((*x).data == 100);
143 let _x: @int = transmute(x);
148 fn test_transmute2() {
150 assert_eq!(~[76u8], transmute(~"L"));