zerocopy-derive 0.8.56

Custom derive for traits from the zerocopy crate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2019 The Fuchsia Authors
//
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
// This file may not be copied, modified, or distributed except according to
// those terms.

#[macro_use]
extern crate zerocopy_renamed;

#[path = "../include.rs"]
mod util;

use zerocopy_renamed::{IntoBytes, KnownLayout};

use self::util::util::AU16;

fn main() {}

//
// KnownLayout errors
//

struct NotKnownLayout;

struct NotKnownLayoutDst([u8]);

// -| `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name |
// -|          N |        N |              N |        N |      KL00 |
#[derive(KnownLayout)]
//~[msrv, stable, nightly]^ ERROR: the size for values of type `[u8]` cannot be known at compilation time
#[zerocopy(crate = "zerocopy_renamed")]
struct KL00(u8, NotKnownLayoutDst);

// -| `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name |
// -|          N |        N |              Y |        N |      KL02 |
#[derive(KnownLayout)]
//~[msrv, stable, nightly]^ ERROR: the size for values of type `[u8]` cannot be known at compilation time
#[zerocopy(crate = "zerocopy_renamed")]
struct KL02(u8, [u8]);

// -| `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name |
// -|          Y |        N |              N |        N |      KL08 |
#[derive(KnownLayout)]
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotKnownLayoutDst: zerocopy_renamed::KnownLayout` is not satisfied
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct KL08(u8, NotKnownLayoutDst);

// -| `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name |
// -|          Y |        N |              N |        Y |      KL09 |
#[derive(KnownLayout)]
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotKnownLayout: zerocopy_renamed::KnownLayout` is not satisfied
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct KL09(NotKnownLayout, NotKnownLayout);

//
// Immutable errors
//

#[derive(Immutable)]
//~[msrv, stable, nightly]^ ERROR: the trait bound `UnsafeCell<()>: zerocopy_renamed::Immutable` is not satisfied
#[zerocopy(crate = "zerocopy_renamed")]
struct Immutable1 {
    a: core::cell::UnsafeCell<()>,
}

#[derive(Immutable)]
//~[msrv, stable, nightly]^ ERROR: the trait bound `UnsafeCell<u8>: zerocopy_renamed::Immutable` is not satisfied
#[zerocopy(crate = "zerocopy_renamed")]
struct Immutable2 {
    a: [core::cell::UnsafeCell<u8>; 0],
}

//
// TryFromBytes errors
//

#[derive(TryFromBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(packed)]
struct TryFromBytesPacked {
    //~[stable, nightly]^ ERROR: packed type cannot transitively contain a `#[repr(align)]` type
    foo: AU16,
}

#[derive(TryFromBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(packed(1))]
struct TryFromBytesPackedN {
    //~[stable, nightly]^ ERROR: packed type cannot transitively contain a `#[repr(align)]` type
    foo: AU16,
}

#[derive(TryFromBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed)]
struct TryFromBytesCPacked {
    //~[stable, nightly]^ ERROR: packed type cannot transitively contain a `#[repr(align)]` type
    foo: AU16,
}

#[derive(TryFromBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed(1))]
struct TryFromBytesCPackedN {
    //~[stable, nightly]^ ERROR: packed type cannot transitively contain a `#[repr(align)]` type
    foo: AU16,
}

//
// IntoBytes errors
//

// Since `IntoBytes1` has at least one generic parameter, an `IntoBytes` impl is
// emitted in which each field type is given an `Unaligned` bound. Since `foo`'s
// type doesn't implement `Unaligned`, this should fail.
#[derive(IntoBytes)]
//~[msrv, stable, nightly]^ ERROR: the trait bound `AU16: zerocopy_renamed::Unaligned` is not satisfied
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct IntoBytes1<T> {
    foo: AU16,
    bar: T,
}

#[derive(IntoBytes)]
//~[stable, nightly]^ ERROR: `IntoBytes2` has 1 total byte(s) of padding
//~[msrv]^^ ERROR: the trait bound `(): PaddingFree<IntoBytes2, 1_usize>` is not satisfied
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct IntoBytes2 {
    foo: u8,
    bar: AU16,
}

#[derive(IntoBytes)]
//~[stable, nightly]^ ERROR: `IntoBytes3` has 1 total byte(s) of padding
//~[msrv]^^ ERROR: the trait bound `(): PaddingFree<IntoBytes3, 1_usize>` is not satisfied
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed(2))]
struct IntoBytes3 {
    foo: u8,
    // We'd prefer to use AU64 here, but you can't use aligned types in
    // packed structs.
    bar: u64,
}

type SliceU8 = [u8];

// Padding between `u8` and `SliceU8`. `SliceU8` doesn't syntactically look like
// a slice, so this case is handled by our `Sized` support.
//
// NOTE(#1708): This exists to ensure that our error messages are good when a
// field is unsized.
#[derive(IntoBytes)]
//~[stable, nightly]^ ERROR: the size for values of type `[u8]` cannot be known at compilation time
//~[msrv]^^ ERROR: the size for values of type `[u8]` cannot be known at compilation time
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct IntoBytes4 {
    a: u8,
    b: SliceU8,
    //~[stable, nightly]^ ERROR: `[u8]` is unsized
    //~[msrv]^^ ERROR: the size for values of type `[u8]` cannot be known at compilation time
}

// Padding between `u8` and `[u16]`. `[u16]` is syntactically identifiable as a
// slice, so this case is handled by our `repr(C)` slice DST support.
#[derive(IntoBytes)]
//~[msrv]^ ERROR: the trait bound `(): DynamicPaddingFree<IntoBytes5, true>` is not satisfied
//~[stable, nightly]^^ ERROR: `IntoBytes5` has one or more padding bytes
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct IntoBytes5 {
    a: u8,
    b: [u16],
}

// Trailing padding after `[u8]`. `[u8]` is syntactically identifiable as a
// slice, so this case is handled by our `repr(C)` slice DST support.
#[derive(IntoBytes)]
//~[msrv]^ ERROR: the trait bound `(): DynamicPaddingFree<IntoBytes6, true>` is not satisfied
//~[stable, nightly]^^ ERROR: `IntoBytes6` has one or more padding bytes
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct IntoBytes6 {
    a: u16,
    b: [u8],
}

// Padding between `u8` and `u16` and also trailing padding after `[u8]`. `[u8]`
// is syntactically identifiable as a slice, so this case is handled by our
// `repr(C)` slice DST support.
#[derive(IntoBytes)]
//~[msrv]^ ERROR: the trait bound `(): DynamicPaddingFree<IntoBytes7, true>` is not satisfied
//~[stable, nightly]^^ ERROR: `IntoBytes7` has one or more padding bytes
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct IntoBytes7 {
    a: u8,
    b: u16,
    c: [u8],
}

#[derive(IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, C)] // zerocopy-derive conservatively treats these as conflicting reprs
              //~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint
struct IntoBytes8 {
    a: u8,
}

#[derive(IntoBytes)]
//~[msrv, stable, nightly]^ ERROR: must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout
#[zerocopy(crate = "zerocopy_renamed")]
struct IntoBytes9<T> {
    t: T,
}

#[derive(IntoBytes)]
//~[msrv, stable, nightly]^ ERROR: must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(packed(2))]
struct IntoBytes10<T> {
    t: T,
}

// `repr(C, packed(2))` can still have padding between heterogeneous fields.
#[derive(IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed(2))]
struct IntoBytes11<T> {
    byte: u8,
    // For `T = AU16`, this field remains 2-aligned and leaves a padding byte
    // after `byte`.
    t: T,
}

fn is_into_bytes_11<T: IntoBytes>() {
    if false {
        is_into_bytes_11::<IntoBytes11<AU16>>();
        //~[stable, nightly]^ ERROR: the trait bound `AU16: zerocopy_renamed::Unaligned` is not satisfied
    }
}

// `repr(C, align(2))` is not sufficient to guarantee the layout of this type.
#[derive(IntoBytes)]
//~[msrv, stable, nightly]^ ERROR: must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, align(2))]
struct IntoBytes12<T> {
    t: T,
}

// NOTE(#3063): This exists to ensure that our analysis for structs with
// syntactic DSTs accounts for `align(N)`.
#[derive(IntoBytes)]
//~[msrv]^ ERROR: the trait bound `(): DynamicPaddingFree<IntoBytes13, true>` is not satisfied
//~[stable, nightly]^^ ERROR: `IntoBytes13` has one or more padding bytes
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, align(2))]
struct IntoBytes13([u8]);

// A homogeneous multi-field optimization must still account for an explicit
// outer alignment. For `T = u8` and `N = 0`, odd-length tails have trailing
// padding.
#[derive(IntoBytes)]
//~[msrv, stable, nightly]^ ERROR: must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, align(2))]
struct IntoBytes14<T, const N: usize>([T; N], [T]);

// A type captured at a macro invocation site can have the same spelling as a
// type parameter introduced by the macro while resolving to a different type.
// The homogeneous `repr(C)` optimization must not treat them as the same type.
#[cfg(nightly)]
mod into_bytes_15 {
    use super::IntoBytes;

    type T = u8;

    zerocopy_derive::__test_hygienically_mixed_into_bytes!();

    fn assert_into_bytes<T: IntoBytes>() {
        if false {
            assert_into_bytes::<IntoBytes15<u8>>();
            assert_into_bytes::<IntoBytes15<u16>>();
            //~[nightly]^ ERROR: type mismatch resolving `<u8 as Identity>::Type == u16`
        }
    }
}

//
// Unaligned errors
//

#[derive(Unaligned)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, align(2))]
//~[msrv, stable, nightly]^ ERROR: cannot derive `Unaligned` on type with alignment greater than 1
struct Unaligned1;

#[derive(Unaligned)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(transparent, align(2))]
//~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint
//~[msrv, stable, nightly]^^ ERROR: transparent struct cannot have other repr hints
struct Unaligned2 {
    foo: u8,
}

#[derive(Unaligned)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(packed, align(2))]
//~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint
struct Unaligned3;
//~[stable, nightly]^ ERROR: type has conflicting packed and align representation hints

#[derive(Unaligned)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(align(1), align(2))]
//~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint
struct Unaligned4;

#[derive(Unaligned)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(align(2), align(4))]
//~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint
struct Unaligned5;

#[derive(Unaligned)]
//~[msrv, stable, nightly]^ ERROR: must have #[repr(C)], #[repr(transparent)], or #[repr(packed)] attribute in order to guarantee this type's alignment
#[zerocopy(crate = "zerocopy_renamed")]
struct Unaligned6;

#[derive(Unaligned)]
//~[msrv, stable, nightly]^ ERROR: must have #[repr(C)], #[repr(transparent)], or #[repr(packed)] attribute in order to guarantee this type's alignment
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(packed(2))]
struct Unaligned7;

// Test the error message emitted when conflicting reprs appear on different
// lines. On the nightly compiler, this emits a "joint span" that spans both
// problematic repr token trees and everything in between.
#[derive(Copy, Clone)]
#[repr(packed(2), C)]
//~[nightly]^ ERROR: this conflicts with another representation hint
#[derive(Unaligned)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed(2))]
//~[msrv, stable]^ ERROR: this conflicts with another representation hint
struct WeirdReprSpan;

#[derive(SplitAt)]
//~[msrv, stable, nightly]^ ERROR: the trait bound `SplitAtNotKnownLayout: zerocopy_renamed::KnownLayout` is not satisfied
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct SplitAtNotKnownLayout([u8]);

#[derive(SplitAt, KnownLayout)]
//~[msrv]^ ERROR: type mismatch resolving `<u8 as zerocopy_renamed::KnownLayout>::PointerMetadata == usize`
//~[msrv, stable, nightly]^^ ERROR: the trait bound `u8: SplitAt` is not satisfied
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct SplitAtSized(u8);

#[derive(KnownLayout)]
#[zerocopy(crate = "zerocopy_renamed"typo)]
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a path as the value
#[repr(C)]
struct InvalidCrateSuffix(u8);

#[derive(KnownLayout)]
#[zerocopy(crate = "Self::foo")]
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path
#[repr(C)]
struct InvalidCrateSelf(u8);

#[derive(KnownLayout)]
#[zerocopy(crate = "::crate::foo")]
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path
#[repr(C)]
struct InvalidCrateLeadingColonRelative(u8);

#[derive(KnownLayout)]
#[zerocopy(crate = "foo::crate::bar")]
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path
#[repr(C)]
struct InvalidCrateNonLeadingCrate(u8);

#[derive(KnownLayout)]
#[zerocopy(crate = "foo::super::bar")]
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path
#[repr(C)]
struct InvalidCrateNonLeadingSuper(u8);

#[derive(KnownLayout)]
#[zerocopy(crate = "foo::Self::bar")]
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path
#[repr(C)]
struct InvalidCrateMiddleSelf(u8);

#[derive(KnownLayout)]
#[zerocopy(crate = "foo::self")]
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path
#[repr(C)]
struct InvalidCrateTrailingSelf(u8);