Debian Codegraph @

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