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