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