Debian Codegraph @

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