]> git.lizzy.rs Git - rust.git/blob - src/test/ui/dynamically-sized-types/dst-trait-tuple.rs
Rollup merge of #95376 - WaffleLapkin:drain_keep_rest, r=dtolnay
[rust.git] / src / test / ui / dynamically-sized-types / dst-trait-tuple.rs
1 // run-pass
2 #![allow(type_alias_bounds)]
3
4 #![allow(unused_features)]
5 #![feature(unsized_tuple_coercion)]
6
7 type Fat<T: ?Sized> = (isize, &'static str, T);
8
9 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
10 struct Bar;
11
12 #[derive(Copy, Clone, PartialEq, Eq)]
13 struct Bar1 {
14     f: isize
15 }
16
17 trait ToBar {
18     fn to_bar(&self) -> Bar;
19     fn to_val(&self) -> isize;
20 }
21
22 impl ToBar for Bar {
23     fn to_bar(&self) -> Bar {
24         *self
25     }
26     fn to_val(&self) -> isize {
27         0
28     }
29 }
30 impl ToBar for Bar1 {
31     fn to_bar(&self) -> Bar {
32         Bar
33     }
34     fn to_val(&self) -> isize {
35         self.f
36     }
37 }
38
39 // x is a fat pointer
40 fn foo(x: &Fat<dyn ToBar>) {
41     assert_eq!(x.0, 5);
42     assert_eq!(x.1, "some str");
43     assert_eq!(x.2.to_bar(), Bar);
44     assert_eq!(x.2.to_val(), 42);
45
46     let y = &x.2;
47     assert_eq!(y.to_bar(), Bar);
48     assert_eq!(y.to_val(), 42);
49 }
50
51 fn bar(x: &dyn ToBar) {
52     assert_eq!(x.to_bar(), Bar);
53     assert_eq!(x.to_val(), 42);
54 }
55
56 fn baz(x: &Fat<Fat<dyn ToBar>>) {
57     assert_eq!(x.0, 5);
58     assert_eq!(x.1, "some str");
59     assert_eq!((x.2).0, 8);
60     assert_eq!((x.2).1, "deep str");
61     assert_eq!((x.2).2.to_bar(), Bar);
62     assert_eq!((x.2).2.to_val(), 42);
63
64     let y = &(x.2).2;
65     assert_eq!(y.to_bar(), Bar);
66     assert_eq!(y.to_val(), 42);
67
68 }
69
70 pub fn main() {
71     let f1 = (5, "some str", Bar1 {f :42});
72     foo(&f1);
73     let f2 = &f1;
74     foo(f2);
75     let f3: &Fat<dyn ToBar> = f2;
76     foo(f3);
77     let f4: &Fat<dyn ToBar> = &f1;
78     foo(f4);
79     let f5: &Fat<dyn ToBar> = &(5, "some str", Bar1 {f :42});
80     foo(f5);
81
82     // Zero size object.
83     let f6: &Fat<dyn ToBar> = &(5, "some str", Bar);
84     assert_eq!(f6.2.to_bar(), Bar);
85
86     // &*
87     //
88     let f7: Box<dyn ToBar> = Box::new(Bar1 {f :42});
89     bar(&*f7);
90
91     // Deep nesting
92     let f1 = (5, "some str", (8, "deep str", Bar1 {f :42}));
93     baz(&f1);
94     let f2 = &f1;
95     baz(f2);
96     let f3: &Fat<Fat<dyn ToBar>> = f2;
97     baz(f3);
98     let f4: &Fat<Fat<dyn ToBar>> = &f1;
99     baz(f4);
100     let f5: &Fat<Fat<dyn ToBar>> = &(5, "some str", (8, "deep str", Bar1 {f :42}));
101     baz(f5);
102 }