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