Debian Codegraph @

.cargo/vendor/zerocopy-derive/tests/ui-stable/struct.rs

raw
1 // Copyright 2019 The Fuchsia Authors
2 //
3 // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4 // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6 // This file may not be copied, modified, or distributed except according to
7 // those terms.
8
9 #[macro_use]
10 extern crate zerocopy;
11
12 #[path = "../include.rs"]
13 mod util;
14
15 use zerocopy::{IntoBytes, KnownLayout};
16
17 use self::util::util::AU16;
18
19 fn main() {}
20
21 //
22 // KnownLayout errors
23 //
24
25 struct NotKnownLayout;
26
27 struct NotKnownLayoutDst([u8]);
28
29 // | `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name |
30 // | N | N | N | N | KL00 |
31 #[derive(KnownLayout)]
32 struct KL00(u8, NotKnownLayoutDst);
33
34 // | `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name |
35 // | N | N | Y | N | KL02 |
36 #[derive(KnownLayout)]
37 struct KL02(u8, [u8]);
38
39 // | `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name |
40 // | Y | N | N | N | KL08 |
41 #[derive(KnownLayout)]
42 #[repr(C)]
43 struct KL08(u8, NotKnownLayoutDst);
44
45 // | `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name |
46 // | Y | N | N | Y | KL09 |
47 #[derive(KnownLayout)]
48 #[repr(C)]
49 struct KL09(NotKnownLayout, NotKnownLayout);
50
51 //
52 // Immutable errors
53 //
54
55 #[derive(Immutable)]
56 struct Immutable1 {
57 a: core::cell::UnsafeCell<()>,
58 }
59
60 #[derive(Immutable)]
61 struct Immutable2 {
62 a: [core::cell::UnsafeCell<u8>; 0],
63 }
64
65 //
66 // TryFromBytes errors
67 //
68
69 #[derive(TryFromBytes)]
70 #[repr(packed)]
71 struct TryFromBytesPacked {
72 foo: AU16,
73 }
74
75 #[derive(TryFromBytes)]
76 #[repr(packed(1))]
77 struct TryFromBytesPackedN {
78 foo: AU16,
79 }
80
81 #[derive(TryFromBytes)]
82 #[repr(C, packed)]
83 struct TryFromBytesCPacked {
84 foo: AU16,
85 }
86
87 #[derive(TryFromBytes)]
88 #[repr(C, packed(1))]
89 struct TryFromBytesCPackedN {
90 foo: AU16,
91 }
92
93 //
94 // IntoBytes errors
95 //
96
97 // Since `IntoBytes1` has at least one generic parameter, an `IntoBytes` impl is
98 // emitted in which each field type is given an `Unaligned` bound. Since `foo`'s
99 // type doesn't implement `Unaligned`, this should fail.
100 #[derive(IntoBytes)]
101 #[repr(C)]
102 struct IntoBytes1<T> {
103 foo: AU16,
104 bar: T,
105 }
106
107 #[derive(IntoBytes)]
108 #[repr(C)]
109 struct IntoBytes2 {
110 foo: u8,
111 bar: AU16,
112 }
113
114 #[derive(IntoBytes)]
115 #[repr(C, packed(2))]
116 struct IntoBytes3 {
117 foo: u8,
118 // We'd prefer to use AU64 here, but you can't use aligned types in
119 // packed structs.
120 bar: u64,
121 }
122
123 type SliceU8 = [u8];
124
125 // Padding between `u8` and `SliceU8`. `SliceU8` doesn't syntactically look like
126 // a slice, so this case is handled by our `Sized` support.
127 //
128 // NOTE(#1708): This exists to ensure that our error messages are good when a
129 // field is unsized.
130 #[derive(IntoBytes)]
131 #[repr(C)]
132 struct IntoBytes4 {
133 a: u8,
134 b: SliceU8,
135 }
136
137 // Padding between `u8` and `[u16]`. `[u16]` is syntactically identifiable as a
138 // slice, so this case is handled by our `repr(C)` slice DST support.
139 #[derive(IntoBytes)]
140 #[repr(C)]
141 struct IntoBytes5 {
142 a: u8,
143 b: [u16],
144 }
145
146 // Trailing padding after `[u8]`. `[u8]` is syntactically identifiable as a
147 // slice, so this case is handled by our `repr(C)` slice DST support.
148 #[derive(IntoBytes)]
149 #[repr(C)]
150 struct IntoBytes6 {
151 a: u16,
152 b: [u8],
153 }
154
155 // Padding between `u8` and `u16` and also trailing padding after `[u8]`. `[u8]`
156 // is syntactically identifiable as a slice, so this case is handled by our
157 // `repr(C)` slice DST support.
158 #[derive(IntoBytes)]
159 #[repr(C)]
160 struct IntoBytes7 {
161 a: u8,
162 b: u16,
163 c: [u8],
164 }
165
166 #[derive(IntoBytes)]
167 #[repr(C, C)] // zerocopy-derive conservatively treats these as conflicting reprs
168 struct IntoBytes8 {
169 a: u8,
170 }
171
172 #[derive(IntoBytes)]
173 struct IntoBytes9<T> {
174 t: T,
175 }
176
177 #[derive(IntoBytes)]
178 #[repr(packed(2))]
179 struct IntoBytes10<T> {
180 t: T,
181 }
182
183 // `repr(C, packed(2))` is not equivalent to `repr(C, packed)`.
184 #[derive(IntoBytes)]
185 #[repr(C, packed(2))]
186 struct IntoBytes11<T> {
187 t0: T,
188 // Add a second field to avoid triggering the "repr(C) struct with one
189 // field" special case.
190 t1: T,
191 }
192
193 fn is_into_bytes_11<T: IntoBytes>() {
194 if false {
195 is_into_bytes_11::<IntoBytes11<AU16>>();
196 }
197 }
198
199 // `repr(C, align(2))` is not sufficient to guarantee the layout of this type.
200 #[derive(IntoBytes)]
201 #[repr(C, align(2))]
202 struct IntoBytes12<T> {
203 t: T,
204 }
205
206 //
207 // Unaligned errors
208 //
209
210 #[derive(Unaligned)]
211 #[repr(C, align(2))]
212 struct Unaligned1;
213
214 #[derive(Unaligned)]
215 #[repr(transparent, align(2))]
216 struct Unaligned2 {
217 foo: u8,
218 }
219
220 #[derive(Unaligned)]
221 #[repr(packed, align(2))]
222 struct Unaligned3;
223
224 #[derive(Unaligned)]
225 #[repr(align(1), align(2))]
226 struct Unaligned4;
227
228 #[derive(Unaligned)]
229 #[repr(align(2), align(4))]
230 struct Unaligned5;
231
232 #[derive(Unaligned)]
233 struct Unaligned6;
234
235 #[derive(Unaligned)]
236 #[repr(packed(2))]
237 struct Unaligned7;
238
239 // Test the error message emitted when conflicting reprs appear on different
240 // lines. On the nightly compiler, this emits a "joint span" that spans both
241 // problematic repr token trees and everything in between.
242 #[derive(Copy, Clone)]
243 #[repr(packed(2), C)]
244 #[derive(Unaligned)]
245 #[repr(C, packed(2))]
246 struct WeirdReprSpan;
247
248 #[derive(SplitAt)]
249 #[repr(C)]
250 struct SplitAtNotKnownLayout([u8]);
251
252 #[derive(SplitAt, KnownLayout)]
253 #[repr(C)]
254 struct SplitAtSized(u8);