File: struct_known_layout.rs

package info (click to toggle)
rust-zerocopy-derive 0.8.26-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 956 kB
  • sloc: makefile: 2
file content (114 lines) | stat: -rw-r--r-- 3,065 bytes parent folder | download | duplicates (15)
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
// Copyright 2023 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)]

extern crate rustversion;

include!("include.rs");

#[derive(imp::KnownLayout)]
struct Zst;

util_assert_impl_all!(Zst: imp::KnownLayout);

#[derive(imp::KnownLayout)]
struct One {
    a: bool,
}

util_assert_impl_all!(One: imp::KnownLayout);

#[derive(imp::KnownLayout)]
struct Two {
    a: bool,
    b: Zst,
}

util_assert_impl_all!(Two: imp::KnownLayout);

#[derive(imp::KnownLayout)]
struct TypeParams<'a, T, I: imp::Iterator> {
    a: I::Item,
    b: u8,
    c: imp::PhantomData<&'a [::core::primitive::u8]>,
    d: imp::PhantomData<&'static ::core::primitive::str>,
    e: imp::PhantomData<imp::String>,
    f: T,
}

util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::KnownLayout);
util_assert_impl_all!(TypeParams<'static, util::AU16, imp::IntoIter<()>>: imp::KnownLayout);

// Deriving `KnownLayout` should work if the struct has bounded parameters.
//
// N.B. We limit this test to rustc >= 1.62, since earlier versions of rustc ICE
// when `KnownLayout` is derived on a `repr(C)` struct whose trailing field
// contains non-static lifetimes.
#[rustversion::since(1.62)]
const _: () = {
    #[derive(imp::KnownLayout)]
    #[repr(C)]
    struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::KnownLayout, const N: usize>(
        [T; N],
        imp::PhantomData<&'a &'b ()>,
    )
    where
        'a: 'b,
        'b: 'a,
        T: 'a + 'b + imp::KnownLayout;

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

const _: () = {
    // Similar to the previous test, except that the trailing field contains
    // only static lifetimes. This is exercisable on all supported toolchains.

    #[derive(imp::KnownLayout)]
    #[repr(C)]
    struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::KnownLayout, const N: usize>(
        &'a &'b [T; N],
        imp::PhantomData<&'static ()>,
    )
    where
        'a: 'b,
        'b: 'a,
        T: 'a + 'b + imp::KnownLayout;

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

// Deriving `KnownLayout` should work if the struct references `Self`. See
// #2116.

#[derive(imp::KnownLayout)]
#[repr(C)]
struct WithSelfReference {
    leading: [u8; Self::N],
    trailing: [[u8; Self::N]],
}

impl WithSelfReference {
    const N: usize = 42;
}

util_assert_impl_all!(WithSelfReference: imp::KnownLayout);

// Deriving `KnownLayout` should work with generic `repr(packed)` types. See
// #2302.

#[derive(imp::KnownLayout)]
#[repr(C, packed)]
struct Packet<P> {
    payload: P,
}

util_assert_impl_all!(Packet<imp::u8>: imp::KnownLayout);