zerocopy-derive 0.8.55

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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// 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.

// See comment in `include.rs` for why we disable the prelude.
#![no_implicit_prelude]
#![allow(warnings)]

include!("include.rs");

// A struct can derive `IntoBytes` if all of its fields implement `IntoBytes`
// and its representation lets the derive prove that the struct introduces no
// uninitialized outer padding. The cases below are grouped by representation
// and by the proof strategy used by the derive.

//
// Non-generic `repr(C)`
//

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct CZst;

util_assert_impl_all!(CZst: imp::IntoBytes);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, align(8))]
struct ReprCAlignedZst;

util_assert_impl_all!(ReprCAlignedZst: imp::IntoBytes);

#[derive(imp::IntoBytes, Copy, Clone)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, align(8))]
struct ReprCAligned8([u8; 8]);

util_assert_impl_all!(ReprCAligned8: imp::IntoBytes);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct C {
    a: u8,
    b: u8,
    c: util::AU16,
}

util_assert_impl_all!(C: imp::IntoBytes);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCSyntacticDst {
    a: u8,
    b: u8,
    c: [util::AU16],
}

util_assert_impl_all!(ReprCSyntacticDst: imp::IntoBytes);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, align(2))]
struct ReprCSyntacticDstAligned {
    a: [[u8; 2]],
}

util_assert_impl_all!(ReprCSyntacticDstAligned: imp::IntoBytes);

//
// Generic `repr(C)`
//

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCGenericOneField<T: ?imp::Sized> {
    t: T,
}

// Even though `ReprCGenericOneField` has generic type arguments, its one-field
// layout never requires an `Unaligned` bound. Sized, array, and slice
// instantiations are asserted in `assert_repr_c_homogeneous` below.

// Given `T: Sized + IntoBytes`, a plain `repr(C)` struct whose fields are all
// `T`, `[T; N]`, or a final `[T]` introduces no outer padding. `T` and all of
// its arrays and slices have the same alignment. The size of `T`, every array,
// and every slice value is a multiple of that alignment; bytes within each `T`
// are covered by `T`'s `IntoBytes` implementation.
//
// The following six declarations exhaustively enumerate the direct two-field
// shape combinations in this grammar. A slice may only be the final field, so
// the first field is either `T` or `[T; N]`, while the second is `T`, `[T; M]`,
// or `[T]`. The one-field case is covered by `ReprCGenericOneField` above.

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCHomogeneousTThenT<T>(T, T);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCHomogeneousTThenArray<T, const N: usize>(T, [T; N]);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCHomogeneousArrayThenT<T, const N: usize>([T; N], T);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCHomogeneousArrayThenArray<T, const N: usize, const M: usize>([T; N], [T; M]);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCHomogeneousTThenSlice<T>(T, [T]);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCHomogeneousArrayThenSlice<T, const N: usize>([T; N], [T]);

// Exercise longer named-field structs containing every allowed sized field
// shape, both with and without a trailing slice. Together with the two-field
// matrix above, these ensure that the derive handles repeated transitions and
// both of its sized/DST paths rather than special-casing a particular arity.
// The literal-length field also ensures that recognition does not depend on
// the array length being a const parameter.
#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCHomogeneousMixedSized<T, const N: usize, const M: usize> {
    literal: [T; 0],
    array0: [T; N],
    t0: T,
    array1: [T; M],
    t1: T,
}

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCHomogeneousMixedDst<T, const N: usize, const M: usize> {
    t0: T,
    array0: [T; N],
    t1: T,
    array1: [T; M],
    tail: [T],
}

// `align(1)` cannot increase a type's alignment, so it preserves the same
// no-padding argument as an unmodified `repr(C)`. Test both the sized and DST
// paths.
#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, align(1))]
struct ReprCHomogeneousAlign1Sized<T, const N: usize>(T, [T; N], T);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, align(1))]
struct ReprCHomogeneousAlign1Dst<T, const N: usize>(T, [T; N], [T]);

// `packed(2)` caps every field's alignment at the same value. Since every
// field's size is still a multiple of that capped alignment, it likewise
// preserves the no-padding argument. A packed DST may only contain elements
// which do not need drop, so the DST declaration additionally requires
// `Copy`.
#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed(2))]
struct ReprCHomogeneousPacked2Sized<T, const N: usize>(T, [T; N], T);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed(2))]
struct ReprCHomogeneousPacked2Dst<T: imp::Copy, const N: usize>(T, [T; N], [T]);

// This generic assertion is stronger than checking a finite set of concrete
// element types and array lengths: its body only assumes `T: IntoBytes`, and
// is type-checked for every `N` and `M`.
fn assert_repr_c_homogeneous<T: imp::IntoBytes, const N: usize, const M: usize>() {
    fn assert_into_bytes<T: ?imp::Sized + imp::IntoBytes>() {}

    assert_into_bytes::<ReprCGenericOneField<T>>();
    assert_into_bytes::<ReprCGenericOneField<[T; N]>>();
    assert_into_bytes::<ReprCGenericOneField<[T]>>();
    assert_into_bytes::<ReprCHomogeneousTThenT<T>>();
    assert_into_bytes::<ReprCHomogeneousTThenArray<T, N>>();
    assert_into_bytes::<ReprCHomogeneousArrayThenT<T, N>>();
    assert_into_bytes::<ReprCHomogeneousArrayThenArray<T, N, M>>();
    assert_into_bytes::<ReprCHomogeneousTThenSlice<T>>();
    assert_into_bytes::<ReprCHomogeneousArrayThenSlice<T, N>>();
    assert_into_bytes::<ReprCHomogeneousMixedSized<T, N, M>>();
    assert_into_bytes::<ReprCHomogeneousMixedDst<T, N, M>>();
    assert_into_bytes::<ReprCHomogeneousAlign1Sized<T, N>>();
    assert_into_bytes::<ReprCHomogeneousAlign1Dst<T, N>>();
    assert_into_bytes::<ReprCHomogeneousPacked2Sized<T, N>>();
}

fn assert_repr_c_homogeneous_packed_dst<T: imp::Copy + imp::IntoBytes, const N: usize>() {
    fn assert_into_bytes<T: ?imp::Sized + imp::IntoBytes>() {}

    assert_into_bytes::<ReprCHomogeneousPacked2Dst<T, N>>();
}

// `AU16` is guaranteed to have alignment 2 and not implement `Unaligned`, so
// these assertions distinguish the homogeneous case from the existing
// `Unaligned` fallback. The witnesses cover every field shape, zero and
// nonzero arrays in both positions, and both the sized and DST paths.
util_assert_impl_all!(ReprCHomogeneousTThenT<util::AU16>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousTThenArray<util::AU16, 0>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousArrayThenT<util::AU16, 3>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousArrayThenArray<util::AU16, 0, 3>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousTThenSlice<util::AU16>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousArrayThenSlice<util::AU16, 0>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousArrayThenSlice<util::AU16, 3>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousMixedSized<util::AU16, 0, 3>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousMixedDst<util::AU16, 0, 3>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousAlign1Sized<util::AU16, 0>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousAlign1Dst<util::AU16, 3>: imp::IntoBytes);
// This exercises the case in which `packed(2)` actually lowers the element
// alignment rather than merely leaving it unchanged.
util_assert_impl_all!(ReprCHomogeneousPacked2Sized<ReprCAligned8, 0>: imp::IntoBytes);
util_assert_impl_all!(ReprCHomogeneousPacked2Dst<ReprCAligned8, 3>: imp::IntoBytes);

// Zero-sized element types still carry alignment. In particular, a derive
// must not assume that an aligned field occupies any bytes.
util_assert_impl_all!(ReprCHomogeneousArrayThenSlice<ReprCAlignedZst, 0>: imp::IntoBytes);

// `IntoBytes` does not imply `Immutable`; interior-mutable element types are
// therefore part of the supported set too.
util_assert_impl_all!(
    ReprCHomogeneousArrayThenSlice<imp::UnsafeCell<util::AU16>, 3>: imp::IntoBytes
);

// A zero-length array does not eliminate the `T: IntoBytes` requirement: the
// trailing slice can still contain elements. `MaybeUninit<u8>` implements
// `Unaligned` but not `IntoBytes`, isolating the bound under test.
util_assert_not_impl_any!(
    ReprCHomogeneousArrayThenSlice<imp::MaybeUninit<u8>, 0>: imp::IntoBytes
);

// Generic `repr(C)` structs outside of the homogeneous family fall back to
// requiring every field type to be `Unaligned`.
#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCIndependentFields<T, U: ?imp::Sized> {
    t: T,
    u: U,
}

util_assert_impl_all!(ReprCIndependentFields<u8, [u8; 2]>: imp::IntoBytes);
util_assert_impl_all!(ReprCIndependentFields<u8, [[u8; 2]]>: imp::IntoBytes);
util_assert_not_impl_any!(ReprCIndependentFields<u8, util::AU16>: imp::IntoBytes);
util_assert_not_impl_any!(ReprCIndependentFields<u8, [util::AU16]>: imp::IntoBytes);

// The common element type is essential. Here, the zero-length `AU16` array
// gives the struct alignment 2, while an odd-length `[u8]` tail requires a
// trailing padding byte.
#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C)]
struct ReprCHeterogeneousArrayThenSlice<T, U, const N: usize>([T; N], [U]);

util_assert_not_impl_any!(
    ReprCHeterogeneousArrayThenSlice<util::AU16, u8, 0>: imp::IntoBytes
);

//
// `repr(transparent)`
//

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(transparent)]
struct Transparent {
    a: u8,
    b: (),
}

util_assert_impl_all!(Transparent: imp::IntoBytes);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(transparent)]
struct TransparentGeneric<T: ?imp::Sized> {
    a: (),
    b: T,
}

util_assert_impl_all!(TransparentGeneric<u64>: imp::IntoBytes);
util_assert_impl_all!(TransparentGeneric<[u64]>: imp::IntoBytes);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(transparent)]
struct Unsized {
    a: [u8],
}

util_assert_impl_all!(Unsized: imp::IntoBytes);

// Deriving `IntoBytes` should work if the struct has bounded parameters.

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(transparent)]
struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::IntoBytes, const N: usize>(
    [T; N],
    imp::PhantomData<&'a &'b ()>,
)
where
    'a: 'b,
    'b: 'a,
    T: 'a + 'b + imp::IntoBytes;

util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::IntoBytes);

//
// Packed representations
//

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed)]
struct CZstPacked;

util_assert_impl_all!(CZstPacked: imp::IntoBytes);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed)]
struct CPacked {
    a: u8,
    // NOTE: The `u16` type is not guaranteed to have alignment 2, although it
    // does on many platforms. However, to fix this would require a custom type
    // with a `#[repr(align(2))]` attribute, and `#[repr(packed)]` types are not
    // allowed to transitively contain `#[repr(align(...))]` types. Thus, we
    // have no choice but to use `u16` here. Luckily, these tests run in CI on
    // platforms on which `u16` has alignment 2, so this isn't that big of a
    // deal.
    b: u16,
}

util_assert_impl_all!(CPacked: imp::IntoBytes);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed(2))]
// The same caveats as for CPacked apply - we're assuming u64 is at least
// 4-byte aligned by default. Without packed(2), this should fail, as there
// would be padding between a/b assuming u64 is 4+ byte aligned.
struct CPacked2 {
    a: u16,
    b: u64,
}

util_assert_impl_all!(CPacked2: imp::IntoBytes);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed)]
struct CPackedGeneric<T, U: ?imp::Sized> {
    t: T,
    // Unsized types stored in `repr(packed)` structs must not be dropped
    // because dropping them in-place might be unsound depending on the
    // alignment of the outer struct. Sized types can be dropped by first being
    // moved to an aligned stack variable, but this isn't possible with unsized
    // types.
    u: imp::ManuallyDrop<U>,
}

util_assert_impl_all!(CPackedGeneric<u8, util::AU16>: imp::IntoBytes);
util_assert_impl_all!(CPackedGeneric<u8, [util::AU16]>: imp::IntoBytes);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(packed)]
struct PackedGeneric<T, U: ?imp::Sized> {
    t: T,
    // Unsized types stored in `repr(packed)` structs must not be dropped
    // because dropping them in-place might be unsound depending on the
    // alignment of the outer struct. Sized types can be dropped by first being
    // moved to an aligned stack variable, but this isn't possible with unsized
    // types.
    u: imp::ManuallyDrop<U>,
}

util_assert_impl_all!(PackedGeneric<u8, util::AU16>: imp::IntoBytes);
util_assert_impl_all!(PackedGeneric<u8, [util::AU16]>: imp::IntoBytes);

// Test for the failure reported in #1182.

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed)]
pub struct IndexEntryFlags(u8);

#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
#[repr(C, packed)]
pub struct IndexEntry<const SIZE_BLOCK_ID: usize> {
    block_number: imp::native_endian::U64,
    flags: IndexEntryFlags,
    block_id: [u8; SIZE_BLOCK_ID],
}

util_assert_impl_all!(IndexEntry<0>: imp::IntoBytes);
util_assert_impl_all!(IndexEntry<1>: imp::IntoBytes);

//
// Rust representation
//

// This test is non-portable, but works so long as Rust happens to lay this
// struct out with no padding.
#[derive(imp::IntoBytes)]
#[zerocopy(crate = "zerocopy_renamed")]
struct Unpacked {
    a: u8,
    b: u8,
}

util_assert_impl_all!(Unpacked: imp::IntoBytes);