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