]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/fat-ptr-cast.rs
b7513da99c806eb26372abe6a77c6d1ae1e419f0
[rustc.git] / src / test / run-pass / fat-ptr-cast.rs
1 // Copyright 2015 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 #![feature(core)]
12
13 use std::mem;
14 use std::raw;
15
16 trait Foo {
17 fn foo(&self) {}
18 }
19
20 struct Bar;
21
22 impl Foo for Bar {}
23
24 fn main() {
25 // Test we can turn a fat pointer to array back into a thin pointer.
26 let a: *const [i32] = &[1, 2, 3];
27 let b = a as *const [i32; 2];
28 unsafe {
29 assert!(*b == [1, 2]);
30 }
31
32 // Test conversion to an address (usize).
33 let a: *const [i32; 3] = &[1, 2, 3];
34 let b: *const [i32] = a;
35 assert!(a as usize == b as usize);
36
37 // And conversion to a void pointer/address for trait objects too.
38 let a: *mut Foo = &mut Bar;
39 let b = a as *mut ();
40 let c = a as usize;
41
42 let d = unsafe {
43 let r: raw::TraitObject = mem::transmute(a);
44 r.data
45 };
46
47 assert!(b == d);
48 assert!(c == d as usize);
49 }