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