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