]> git.lizzy.rs Git - rust.git/blob - src/libstd/cast.rs
auto merge of #10977 : brson/rust/androidtest, r=brson
[rust.git] / src / libstd / cast.rs
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.
4 //
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.
10
11 //! Unsafe casting functions
12
13 use ptr::RawPtr;
14 use mem;
15 use unstable::intrinsics;
16 use ptr::copy_nonoverlapping_memory;
17
18 /// Casts the value at `src` to U. The two types must have the same length.
19 #[inline]
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>());
25     dest
26 }
27
28 /**
29  * Move a thing into the void
30  *
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.
34  */
35 #[inline]
36 pub unsafe fn forget<T>(thing: T) { intrinsics::forget(thing); }
37
38 /**
39  * Force-increment the reference count on a shared box. If used
40  * carelessly, this can leak the box.
41  */
42 #[inline]
43 pub unsafe fn bump_box_refcount<T>(t: @T) { forget(t); }
44
45 /**
46  * Transform a value of one type into a value of another type.
47  * Both types must have the same size and alignment.
48  *
49  * # Example
50  *
51  * ```rust
52  * let v: &[u8] = transmute("L");
53  * assert!(v == [76u8]);
54  * ```
55  */
56 #[inline]
57 pub unsafe fn transmute<L, G>(thing: L) -> G {
58     intrinsics::transmute(thing)
59 }
60
61 /// Coerce an immutable reference to be mutable.
62 #[inline]
63 pub unsafe fn transmute_mut<'a,T>(ptr: &'a T) -> &'a mut T { transmute(ptr) }
64
65 /// Coerce a borrowed pointer to have an arbitrary associated region.
66 #[inline]
67 pub unsafe fn transmute_region<'a,'b,T>(ptr: &'a T) -> &'b T {
68     transmute(ptr)
69 }
70
71 /// Coerce an immutable reference to be mutable.
72 #[inline]
73 pub unsafe fn transmute_mut_unsafe<T,P:RawPtr<T>>(ptr: P) -> *mut T {
74     transmute(ptr)
75 }
76
77 /// Coerce an immutable reference to be mutable.
78 #[inline]
79 pub unsafe fn transmute_immut_unsafe<T,P:RawPtr<T>>(ptr: P) -> *T {
80     transmute(ptr)
81 }
82
83 /// Coerce a borrowed mutable pointer to have an arbitrary associated region.
84 #[inline]
85 pub unsafe fn transmute_mut_region<'a,'b,T>(ptr: &'a mut T) -> &'b mut T {
86     transmute(ptr)
87 }
88
89 /// Transforms lifetime of the second pointer to match the first.
90 #[inline]
91 pub unsafe fn copy_lifetime<'a,S,T>(_ptr: &'a S, ptr: &T) -> &'a T {
92     transmute_region(ptr)
93 }
94
95 /// Transforms lifetime of the second pointer to match the first.
96 #[inline]
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)
99 }
100
101 /// Transforms lifetime of the second pointer to match the first.
102 #[inline]
103 pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
104     transmute_region(ptr)
105 }
106
107
108 /****************************************************************************
109  * Tests
110  ****************************************************************************/
111
112 #[cfg(test)]
113 mod tests {
114     use cast::{bump_box_refcount, transmute};
115     use unstable::raw;
116
117     #[test]
118     fn test_transmute_copy() {
119         assert_eq!(1u, unsafe { ::cast::transmute_copy(&1) });
120     }
121
122     #[test]
123     fn test_bump_managed_refcount() {
124         unsafe {
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.
134         }
135     }
136
137     #[test]
138     fn test_transmute() {
139         unsafe {
140             let x = @100u8;
141             let x: *raw::Box<u8> = transmute(x);
142             assert!((*x).data == 100);
143             let _x: @int = transmute(x);
144         }
145     }
146
147     #[test]
148     fn test_transmute2() {
149         unsafe {
150             assert_eq!(~[76u8], transmute(~"L"));
151         }
152     }
153 }