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