]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/dst-raw.rs
rollup merge of #20306: alexcrichton/second-pass-string
[rust.git] / src / test / run-pass / dst-raw.rs
1 // Copyright 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 // Test DST raw pointers
12
13 trait Trait {
14     fn foo(&self) -> int;
15 }
16
17 struct A {
18     f: int
19 }
20 impl Trait for A {
21     fn foo(&self) -> int {
22         self.f
23     }
24 }
25
26 struct Foo<T: ?Sized> {
27     f: T
28 }
29
30 pub fn main() {
31     // raw trait object
32     let x = A { f: 42 };
33     let z: *const Trait = &x;
34     let r = unsafe {
35         (&*z).foo()
36     };
37     assert!(r == 42);
38
39     // raw DST struct
40     let p = Foo {f: A { f: 42 }};
41     let o: *const Foo<Trait> = &p;
42     let r = unsafe {
43         (&*o).f.foo()
44     };
45     assert!(r == 42);
46
47     // raw slice
48     let a: *const [_] = &[1i, 2, 3];
49     unsafe {
50         let b = (*a)[2];
51         assert!(b == 3);
52         let len = (*a).len();
53         assert!(len == 3);
54     }
55
56     // raw slice with explicit cast
57     let a = &[1i, 2, 3] as *const [_];
58     unsafe {
59         let b = (*a)[2];
60         assert!(b == 3);
61         let len = (*a).len();
62         assert!(len == 3);
63     }
64
65     // raw DST struct with slice
66     let c: *const Foo<[_]> = &Foo {f: [1i, 2, 3]};
67     unsafe {
68         let b = (&*c).f[0];
69         assert!(b == 1);
70         let len = (&*c).f.len();
71         assert!(len == 3);
72     }
73
74     // all of the above with *mut
75     let mut x = A { f: 42 };
76     let z: *mut Trait = &mut x;
77     let r = unsafe {
78         (&*z).foo()
79     };
80     assert!(r == 42);
81
82     let mut p = Foo {f: A { f: 42 }};
83     let o: *mut Foo<Trait> = &mut p;
84     let r = unsafe {
85         (&*o).f.foo()
86     };
87     assert!(r == 42);
88
89     let a: *mut [_] = &mut [1i, 2, 3];
90     unsafe {
91         let b = (*a)[2];
92         assert!(b == 3);
93         let len = (*a).len();
94         assert!(len == 3);
95     }
96
97     let a = &mut [1i, 2, 3] as *mut [_];
98     unsafe {
99         let b = (*a)[2];
100         assert!(b == 3);
101         let len = (*a).len();
102         assert!(len == 3);
103     }
104
105     let c: *mut Foo<[_]> = &mut Foo {f: [1i, 2, 3]};
106     unsafe {
107         let b = (&*c).f[0];
108         assert!(b == 1);
109         let len = (&*c).f.len();
110         assert!(len == 3);
111     }
112 }