Debian Codegraph @

debian/rust-vendor/zerocopy-derive/tests/struct_known_layout.rs

raw
1 // Copyright 2023 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 // See comment in `include.rs` for why we disable the prelude.
10 #![no_implicit_prelude]
11 #![allow(warnings)]
12
13 extern crate rustversion;
14
15 include!("include.rs");
16
17 #[derive(imp::KnownLayout)]
18 #[zerocopy(crate = "zerocopy_renamed")]
19 struct Zst;
20
21 util_assert_impl_all!(Zst: imp::KnownLayout);
22
23 #[derive(imp::KnownLayout)]
24 #[zerocopy(crate = "zerocopy_renamed")]
25 struct One {
26 a: bool,
27 }
28
29 util_assert_impl_all!(One: imp::KnownLayout);
30
31 #[derive(imp::KnownLayout)]
32 #[zerocopy(crate = "zerocopy_renamed")]
33 struct Two {
34 a: bool,
35 b: Zst,
36 }
37
38 util_assert_impl_all!(Two: imp::KnownLayout);
39
40 #[derive(imp::KnownLayout)]
41 #[zerocopy(crate = "zerocopy_renamed")]
42 struct TypeParams<'a, T, I: imp::Iterator> {
43 a: I::Item,
44 b: u8,
45 c: imp::PhantomData<&'a [::core::primitive::u8]>,
46 d: imp::PhantomData<&'static ::core::primitive::str>,
47 e: imp::PhantomData<imp::String>,
48 f: T,
49 }
50
51 util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::KnownLayout);
52 util_assert_impl_all!(TypeParams<'static, util::AU16, imp::IntoIter<()>>: imp::KnownLayout);
53
54 // Deriving `KnownLayout` should work if the struct has bounded parameters.
55 //
56 // N.B. We limit this test to rustc >= 1.62, since earlier versions of rustc ICE
57 // when `KnownLayout` is derived on a `repr(C)` struct whose trailing field
58 // contains non-static lifetimes.
59 #[rustversion::since(1.62)]
60 const _: () = {
61 #[derive(imp::KnownLayout)]
62 #[zerocopy(crate = "zerocopy_renamed")]
63 #[repr(C)]
64 struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::KnownLayout, const N: usize>(
65 [T; N],
66 imp::PhantomData<&'a &'b ()>,
67 )
68 where
69 'a: 'b,
70 'b: 'a,
71 T: 'a + 'b + imp::KnownLayout;
72
73 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::KnownLayout);
74 };
75
76 const _: () = {
77 // Similar to the previous test, except that the trailing field contains
78 // only static lifetimes. This is exercisable on all supported toolchains.
79
80 #[derive(imp::KnownLayout)]
81 #[zerocopy(crate = "zerocopy_renamed")]
82 #[repr(C)]
83 struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::KnownLayout, const N: usize>(
84 &'a &'b [T; N],
85 imp::PhantomData<&'static ()>,
86 )
87 where
88 'a: 'b,
89 'b: 'a,
90 T: 'a + 'b + imp::KnownLayout;
91
92 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::KnownLayout);
93 };
94
95 // Deriving `KnownLayout` should work if the struct references `Self`. See
96 // #2116.
97
98 #[derive(imp::KnownLayout)]
99 #[zerocopy(crate = "zerocopy_renamed")]
100 #[repr(C)]
101 struct WithSelfReference {
102 leading: [u8; Self::N],
103 trailing: [[u8; Self::N]],
104 }
105
106 impl WithSelfReference {
107 const N: usize = 42;
108 }
109
110 util_assert_impl_all!(WithSelfReference: imp::KnownLayout);
111
112 // Deriving `KnownLayout` should work with generic `repr(packed)` types. See
113 // #2302.
114
115 #[derive(imp::KnownLayout)]
116 #[zerocopy(crate = "zerocopy_renamed")]
117 #[repr(C, packed)]
118 struct Packet<P> {
119 payload: P,
120 }
121
122 util_assert_impl_all!(Packet<imp::u8>: imp::KnownLayout);
123
124 #[derive(imp::KnownLayout)]
125 #[zerocopy(crate = "zerocopy_renamed")]
126 #[repr(C)]
127 struct RawIdentifier {
128 r#type: u8,
129 }
130
131 util_assert_impl_all!(RawIdentifier: imp::KnownLayout);