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