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