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