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