]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/fat-ptr-cast.rs
New upstream version 1.19.0+dfsg1
[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(raw)]
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_eq!(*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_eq!(a as usize, b as *const () 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 *const () as usize;
41 let d = unsafe {
42 let r: raw::TraitObject = mem::transmute(a);
43 r.data
44 };
45
46 assert_eq!(b, d);
47 assert_eq!(c, d as usize);
48
49 }