]> git.lizzy.rs Git - rust.git/blob - src/test/ui/dynamically-sized-types/dst-tuple.rs
Merge commit '98e2b9f25b6db4b2680a3d388456d9f95cb28344' into clippyup
[rust.git] / src / test / ui / dynamically-sized-types / dst-tuple.rs
1 // run-pass
2 #![allow(type_alias_bounds)]
3
4 #![feature(box_syntax)]
5 #![feature(unsized_tuple_coercion)]
6
7 type Fat<T: ?Sized> = (isize, &'static str, T);
8
9 // x is a fat pointer
10 fn foo(x: &Fat<[isize]>) {
11     let y = &x.2;
12     assert_eq!(x.2.len(), 3);
13     assert_eq!(y[0], 1);
14     assert_eq!(x.2[1], 2);
15     assert_eq!(x.0, 5);
16     assert_eq!(x.1, "some str");
17 }
18
19 fn foo2<T:ToBar>(x: &Fat<[T]>) {
20     let y = &x.2;
21     let bar = Bar;
22     assert_eq!(x.2.len(), 3);
23     assert_eq!(y[0].to_bar(), bar);
24     assert_eq!(x.2[1].to_bar(), bar);
25     assert_eq!(x.0, 5);
26     assert_eq!(x.1, "some str");
27 }
28
29 fn foo3(x: &Fat<Fat<[isize]>>) {
30     let y = &(x.2).2;
31     assert_eq!(x.0, 5);
32     assert_eq!(x.1, "some str");
33     assert_eq!((x.2).0, 8);
34     assert_eq!((x.2).1, "deep str");
35     assert_eq!((x.2).2.len(), 3);
36     assert_eq!(y[0], 1);
37     assert_eq!((x.2).2[1], 2);
38 }
39
40
41 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
42 struct Bar;
43
44 trait ToBar {
45     fn to_bar(&self) -> Bar;
46 }
47
48 impl ToBar for Bar {
49     fn to_bar(&self) -> Bar {
50         *self
51     }
52 }
53
54 pub fn main() {
55     // With a vec of ints.
56     let f1 = (5, "some str", [1, 2, 3]);
57     foo(&f1);
58     let f2 = &f1;
59     foo(f2);
60     let f3: &Fat<[isize]> = f2;
61     foo(f3);
62     let f4: &Fat<[isize]> = &f1;
63     foo(f4);
64     let f5: &Fat<[isize]> = &(5, "some str", [1, 2, 3]);
65     foo(f5);
66
67     // With a vec of Bars.
68     let bar = Bar;
69     let f1 = (5, "some str", [bar, bar, bar]);
70     foo2(&f1);
71     let f2 = &f1;
72     foo2(f2);
73     let f3: &Fat<[Bar]> = f2;
74     foo2(f3);
75     let f4: &Fat<[Bar]> = &f1;
76     foo2(f4);
77     let f5: &Fat<[Bar]> = &(5, "some str", [bar, bar, bar]);
78     foo2(f5);
79
80     // Assignment.
81     let f5: &mut Fat<[isize]> = &mut (5, "some str", [1, 2, 3]);
82     f5.2[1] = 34;
83     assert_eq!(f5.2[0], 1);
84     assert_eq!(f5.2[1], 34);
85     assert_eq!(f5.2[2], 3);
86
87     // Zero size vec.
88     let f5: &Fat<[isize]> = &(5, "some str", []);
89     assert!(f5.2.is_empty());
90     let f5: &Fat<[Bar]> = &(5, "some str", []);
91     assert!(f5.2.is_empty());
92
93     // Deeply nested.
94     let f1 = (5, "some str", (8, "deep str", [1, 2, 3]));
95     foo3(&f1);
96     let f2 = &f1;
97     foo3(f2);
98     let f3: &Fat<Fat<[isize]>> = f2;
99     foo3(f3);
100     let f4: &Fat<Fat<[isize]>> = &f1;
101     foo3(f4);
102     let f5: &Fat<Fat<[isize]>> = &(5, "some str", (8, "deep str", [1, 2, 3]));
103     foo3(f5);
104
105     // Box.
106     let f1 = Box::new([1, 2, 3]);
107     assert_eq!((*f1)[1], 2);
108     let f2: Box<[isize]> = f1;
109     assert_eq!((*f2)[1], 2);
110
111     // Nested Box.
112     let f1 : Box<Fat<[isize; 3]>> = box (5, "some str", [1, 2, 3]);
113     foo(&*f1);
114     let f2 : Box<Fat<[isize]>> = f1;
115     foo(&*f2);
116
117     let f3 : Box<Fat<[isize]>> =
118         Box::<Fat<[_; 3]>>::new((5, "some str", [1, 2, 3]));
119     foo(&*f3);
120 }