]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/dst-struct-sole.rs
74f4b9e923301732b7786d14adae54d5601dd307
[rust.git] / src / test / run-pass / dst-struct-sole.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 // As dst-struct.rs, but the unsized field is the only field in the struct.
12
13 struct Fat<T: ?Sized> {
14     ptr: T
15 }
16
17 // x is a fat pointer
18 fn foo(x: &Fat<[int]>) {
19     let y = &x.ptr;
20     assert!(x.ptr.len() == 3);
21     assert!(y[0] == 1);
22     assert!(x.ptr[1] == 2);
23 }
24
25 fn foo2<T:ToBar>(x: &Fat<[T]>) {
26     let y = &x.ptr;
27     let bar = Bar;
28     assert!(x.ptr.len() == 3);
29     assert!(y[0].to_bar() == bar);
30     assert!(x.ptr[1].to_bar() == bar);
31 }
32
33 #[derive(Copy, PartialEq, Eq)]
34 struct Bar;
35
36 trait ToBar {
37     fn to_bar(&self) -> Bar;
38 }
39
40 impl ToBar for Bar {
41     fn to_bar(&self) -> Bar {
42         *self
43     }
44 }
45
46 pub fn main() {
47     // With a vec of ints.
48     let f1 = Fat { ptr: [1, 2, 3] };
49     foo(&f1);
50     let f2 = &f1;
51     foo(f2);
52     let f3: &Fat<[int]> = f2;
53     foo(f3);
54     let f4: &Fat<[int]> = &f1;
55     foo(f4);
56     let f5: &Fat<[int]> = &Fat { ptr: [1, 2, 3] };
57     foo(f5);
58
59     // With a vec of Bars.
60     let bar = Bar;
61     let f1 = Fat { ptr: [bar, bar, bar] };
62     foo2(&f1);
63     let f2 = &f1;
64     foo2(f2);
65     let f3: &Fat<[Bar]> = f2;
66     foo2(f3);
67     let f4: &Fat<[Bar]> = &f1;
68     foo2(f4);
69     let f5: &Fat<[Bar]> = &Fat { ptr: [bar, bar, bar] };
70     foo2(f5);
71
72     // Assignment.
73     let f5: &mut Fat<[int]> = &mut Fat { ptr: [1, 2, 3] };
74     f5.ptr[1] = 34;
75     assert!(f5.ptr[0] == 1);
76     assert!(f5.ptr[1] == 34);
77     assert!(f5.ptr[2] == 3);
78
79     // Zero size vec.
80     let f5: &Fat<[int]> = &Fat { ptr: [] };
81     assert!(f5.ptr.len() == 0);
82     let f5: &Fat<[Bar]> = &Fat { ptr: [] };
83     assert!(f5.ptr.len() == 0);
84 }