(index<- ) ./libcore/cast.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Fri May 9 13:02:28 2014
1 // Copyright 2012-2014 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 mem;
14 use intrinsics;
15 use ptr::copy_nonoverlapping_memory;
16
17 /**
18 * Transform a value of one type into a value of another type.
19 * Both types must have the same size and alignment.
20 *
21 * # Example
22 *
23 * ```rust
24 * use std::cast;
25 *
26 * let v: &[u8] = unsafe { cast::transmute("L") };
27 * assert!(v == [76u8]);
28 * ```
29 */
30 #[inline]
31 pub unsafe fn transmute<T, U>(thing: T) -> U {
32 intrinsics::transmute(thing)
33 }
34
35 /**
36 * Move a thing into the void
37 *
38 * The forget function will take ownership of the provided value but neglect
39 * to run any required cleanup or memory-management operations on it.
40 */
41 #[inline]
42 pub unsafe fn forget<T>(thing: T) { intrinsics::forget(thing); }
43
44 /// Casts the value at `src` to U. The two types must have the same length.
45 #[inline]
46 pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
47 let mut dest: U = mem::uninit();
48 let dest_ptr: *mut u8 = transmute(&mut dest);
49 let src_ptr: *u8 = transmute(src);
50 copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::<U>());
51 dest
52 }
53
54 /// Coerce an immutable reference to be mutable.
55 #[inline]
56 #[deprecated="casting &T to &mut T is undefined behaviour: use Cell<T>, RefCell<T> or Unsafe<T>"]
57 pub unsafe fn transmute_mut<'a,T>(ptr: &'a T) -> &'a mut T { transmute(ptr) }
58
59 /// Coerce a reference to have an arbitrary associated lifetime.
60 #[inline]
61 pub unsafe fn transmute_lifetime<'a,'b,T>(ptr: &'a T) -> &'b T {
62 transmute(ptr)
63 }
64
65 /// Coerce an immutable reference to be mutable.
66 #[inline]
67 pub unsafe fn transmute_mut_unsafe<T>(ptr: *T) -> *mut T {
68 transmute(ptr)
69 }
70
71 /// Coerce a mutable reference to have an arbitrary associated lifetime.
72 #[inline]
73 pub unsafe fn transmute_mut_lifetime<'a,'b,T>(ptr: &'a mut T) -> &'b mut T {
74 transmute(ptr)
75 }
76
77 /// Transforms lifetime of the second pointer to match the first.
78 #[inline]
79 pub unsafe fn copy_lifetime<'a,S,T>(_ptr: &'a S, ptr: &T) -> &'a T {
80 transmute_lifetime(ptr)
81 }
82
83 /// Transforms lifetime of the second pointer to match the first.
84 #[inline]
85 pub unsafe fn copy_mut_lifetime<'a,S,T>(_ptr: &'a mut S, ptr: &mut T) -> &'a mut T {
86 transmute_mut_lifetime(ptr)
87 }
88
89 /// Transforms lifetime of the second pointer to match the first.
90 #[inline]
91 pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
92 transmute_lifetime(ptr)
93 }
94
95
96 /****************************************************************************
97 * Tests
98 ****************************************************************************/
99
100 #[cfg(test)]
101 mod tests {
102 use cast::transmute;
103 use raw;
104 use realstd::str::StrAllocating;
105
106 #[test]
107 fn test_transmute_copy() {
108 assert_eq!(1u, unsafe { ::cast::transmute_copy(&1) });
109 }
110
111 #[test]
112 fn test_transmute() {
113 unsafe {
114 let x = @100u8;
115 let x: *raw::Box<u8> = transmute(x);
116 assert!((*x).data == 100);
117 let _x: @int = transmute(x);
118 }
119 }
120
121 #[test]
122 fn test_transmute2() {
123 unsafe {
124 assert_eq!(box [76u8], transmute("L".to_owned()));
125 }
126 }
127 }
libcore/cast.rs:41:10-41:10 -fn- definition:
pub unsafe fn forget<T>(thing: T) { intrinsics::forget(thing); }
/// Casts the value at `src` to U. The two types must have the same length.
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
references:- 3libcore/ptr.rs:
258: // because it's no longer relevant.
259: cast::forget(tmp);
260: }
libcore/should_not_exist.rs:
105: free(ptr as *u8);
106: cast::forget(ret);
107: ret = cast::transmute(ptr2);
libcore/mem.rs:
241: // because it's no longer relevant.
242: cast::forget(t);
243: }
libcore/cast.rs:45:10-45:10 -fn- definition:
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
let mut dest: U = mem::uninit();
let dest_ptr: *mut u8 = transmute(&mut dest);
references:- 5libcore/any.rs:
108: // Get the raw representation of the trait object
109: let to: TraitObject = transmute_copy(&self);
libcore/raw.rs:
76: #[inline]
77: fn repr(&self) -> T { unsafe { cast::transmute_copy(self) } }
78: }
libcore/slice.rs:
1014: let len = self.len();
1015: let self2: &'a mut [T] = cast::transmute_copy(&self);
1016: (self.mut_slice(0, mid), self2.mut_slice(mid, len))
libcore/any.rs:
132: // Get the raw representation of the trait object
133: let to: TraitObject = transmute_copy(&self);
libcore/cast.rs:30:10-30:10 -fn- definition:
pub unsafe fn transmute<T, U>(thing: T) -> U {
intrinsics::transmute(thing)
}
references:- 69libcore/ptr.rs:
libcore/any.rs:
libcore/char.rs:
libcore/slice.rs:
libcore/str.rs:
libcore/should_not_exist.rs:
libcore/slice.rs:
libcore/cast.rs:60:10-60:10 -fn- definition:
pub unsafe fn transmute_lifetime<'a,'b,T>(ptr: &'a T) -> &'b T {
transmute(ptr)
}
references:- 279: pub unsafe fn copy_lifetime<'a,S,T>(_ptr: &'a S, ptr: &T) -> &'a T {
80: transmute_lifetime(ptr)
81: }
--
91: pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
92: transmute_lifetime(ptr)
93: }