]>
Commit | Line | Data |
---|---|---|
62682a34 | 1 | #![feature(raw)] |
d9579d0f AL |
2 | |
3 | use std::mem; | |
4 | use std::raw; | |
5 | ||
6 | trait Foo { | |
7 | fn foo(&self) {} | |
8 | } | |
9 | ||
10 | struct Bar; | |
11 | ||
12 | impl Foo for Bar {} | |
13 | ||
14 | fn main() { | |
15 | // Test we can turn a fat pointer to array back into a thin pointer. | |
16 | let a: *const [i32] = &[1, 2, 3]; | |
17 | let b = a as *const [i32; 2]; | |
18 | unsafe { | |
62682a34 | 19 | assert_eq!(*b, [1, 2]); |
d9579d0f AL |
20 | } |
21 | ||
22 | // Test conversion to an address (usize). | |
23 | let a: *const [i32; 3] = &[1, 2, 3]; | |
24 | let b: *const [i32] = a; | |
62682a34 | 25 | assert_eq!(a as usize, b as *const () as usize); |
d9579d0f AL |
26 | |
27 | // And conversion to a void pointer/address for trait objects too. | |
dc9dc135 | 28 | let a: *mut dyn Foo = &mut Bar; |
d9579d0f | 29 | let b = a as *mut (); |
62682a34 | 30 | let c = a as *const () as usize; |
d9579d0f AL |
31 | let d = unsafe { |
32 | let r: raw::TraitObject = mem::transmute(a); | |
33 | r.data | |
34 | }; | |
35 | ||
62682a34 SL |
36 | assert_eq!(b, d); |
37 | assert_eq!(c, d as usize); | |
38 | ||
d9579d0f | 39 | } |