]> git.lizzy.rs Git - rust.git/blob - src/libstd/cast.rs
auto merge of #10519 : nikomatsakis/rust/issue-8624-borrowck-overly-permissive, r...
[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 mutable reference to be immutable.
66 #[inline]
67 pub unsafe fn transmute_immut<'a,T>(ptr: &'a mut T) -> &'a T {
68     transmute(ptr)
69 }
70
71 /// Coerce a borrowed pointer to have an arbitrary associated region.
72 #[inline]
73 pub unsafe fn transmute_region<'a,'b,T>(ptr: &'a T) -> &'b T {
74     transmute(ptr)
75 }
76
77 /// Coerce an immutable reference to be mutable.
78 #[inline]
79 pub unsafe fn transmute_mut_unsafe<T,P:RawPtr<T>>(ptr: P) -> *mut T {
80     transmute(ptr)
81 }
82
83 /// Coerce an immutable reference to be mutable.
84 #[inline]
85 pub unsafe fn transmute_immut_unsafe<T,P:RawPtr<T>>(ptr: P) -> *T {
86     transmute(ptr)
87 }
88
89 /// Coerce a borrowed mutable pointer to have an arbitrary associated region.
90 #[inline]
91 pub unsafe fn transmute_mut_region<'a,'b,T>(ptr: &'a mut T) -> &'b mut T {
92     transmute(ptr)
93 }
94
95 /// Transforms lifetime of the second pointer to match the first.
96 #[inline]
97 pub unsafe fn copy_lifetime<'a,S,T>(_ptr: &'a S, ptr: &T) -> &'a T {
98     transmute_region(ptr)
99 }
100
101 /// Transforms lifetime of the second pointer to match the first.
102 #[inline]
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)
105 }
106
107 /// Transforms lifetime of the second pointer to match the first.
108 #[inline]
109 pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
110     transmute_region(ptr)
111 }
112
113
114 /****************************************************************************
115  * Tests
116  ****************************************************************************/
117
118 #[cfg(test)]
119 mod tests {
120     use cast::{bump_box_refcount, transmute};
121     use unstable::raw;
122
123     #[test]
124     fn test_transmute_copy() {
125         assert_eq!(1u, unsafe { ::cast::transmute_copy(&1) });
126     }
127
128     #[test]
129     fn test_bump_box_refcount() {
130         unsafe {
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.
140         }
141     }
142
143     #[test]
144     fn test_transmute() {
145         unsafe {
146             let x = @100u8;
147             let x: *raw::Box<u8> = transmute(x);
148             assert!((*x).data == 100);
149             let _x: @int = transmute(x);
150         }
151     }
152
153     #[test]
154     fn test_transmute2() {
155         unsafe {
156             assert_eq!(~[76u8], transmute(~"L"));
157         }
158     }
159 }