]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/dst-trait.rs
rustdoc: Replace no-pretty-expanded with pretty-expanded
[rust.git] / src / test / run-pass / dst-trait.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 // pretty-expanded FIXME #23616
12
13 #![allow(unknown_features)]
14 #![feature(box_syntax)]
15
16 struct Fat<T: ?Sized> {
17     f1: int,
18     f2: &'static str,
19     ptr: T
20 }
21
22 #[derive(Copy, PartialEq, Eq)]
23 struct Bar;
24
25 #[derive(Copy, PartialEq, Eq)]
26 struct Bar1 {
27     f: int
28 }
29
30 trait ToBar {
31     fn to_bar(&self) -> Bar;
32     fn to_val(&self) -> int;
33 }
34
35 impl ToBar for Bar {
36     fn to_bar(&self) -> Bar {
37         *self
38     }
39     fn to_val(&self) -> int {
40         0
41     }
42 }
43 impl ToBar for Bar1 {
44     fn to_bar(&self) -> Bar {
45         Bar
46     }
47     fn to_val(&self) -> int {
48         self.f
49     }
50 }
51
52 // x is a fat pointer
53 fn foo(x: &Fat<ToBar>) {
54     assert!(x.f1 == 5);
55     assert!(x.f2 == "some str");
56     assert!(x.ptr.to_bar() == Bar);
57     assert!(x.ptr.to_val() == 42);
58
59     let y = &x.ptr;
60     assert!(y.to_bar() == Bar);
61     assert!(y.to_val() == 42);
62 }
63
64 fn bar(x: &ToBar) {
65     assert!(x.to_bar() == Bar);
66     assert!(x.to_val() == 42);
67 }
68
69 fn baz(x: &Fat<Fat<ToBar>>) {
70     assert!(x.f1 == 5);
71     assert!(x.f2 == "some str");
72     assert!(x.ptr.f1 == 8);
73     assert!(x.ptr.f2 == "deep str");
74     assert!(x.ptr.ptr.to_bar() == Bar);
75     assert!(x.ptr.ptr.to_val() == 42);
76
77     let y = &x.ptr.ptr;
78     assert!(y.to_bar() == Bar);
79     assert!(y.to_val() == 42);
80
81 }
82
83 pub fn main() {
84     let f1 = Fat { f1: 5, f2: "some str", ptr: Bar1 {f :42} };
85     foo(&f1);
86     let f2 = &f1;
87     foo(f2);
88     let f3: &Fat<ToBar> = f2;
89     foo(f3);
90     let f4: &Fat<ToBar> = &f1;
91     foo(f4);
92     let f5: &Fat<ToBar> = &Fat { f1: 5, f2: "some str", ptr: Bar1 {f :42} };
93     foo(f5);
94
95     // Zero size object.
96     let f6: &Fat<ToBar> = &Fat { f1: 5, f2: "some str", ptr: Bar };
97     assert!(f6.ptr.to_bar() == Bar);
98
99     // &*
100     //
101     // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
102     let f7: Box<ToBar> = Box::new(Bar1 {f :42});
103     bar(&*f7);
104
105     // Deep nesting
106     let f1 =
107         Fat { f1: 5, f2: "some str", ptr: Fat { f1: 8, f2: "deep str", ptr: Bar1 {f :42}} };
108     baz(&f1);
109     let f2 = &f1;
110     baz(f2);
111     let f3: &Fat<Fat<ToBar>> = f2;
112     baz(f3);
113     let f4: &Fat<Fat<ToBar>> = &f1;
114     baz(f4);
115     let f5: &Fat<Fat<ToBar>> =
116         &Fat { f1: 5, f2: "some str", ptr: Fat { f1: 8, f2: "deep str", ptr: Bar1 {f :42}} };
117     baz(f5);
118 }