Debian Codegraph @

.cargo/vendor/zerocopy-derive/tests/struct_to_bytes.rs

raw
1 // Copyright 2019 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 include!("include.rs");
14
15 // A struct is `IntoBytes` if:
16 // - all fields are `IntoBytes`
17 // - `repr(C)` or `repr(transparent)` and
18 // - no padding (size of struct equals sum of size of field types)
19 // - `repr(packed)`
20
21 #[derive(imp::IntoBytes)]
22 #[repr(C)]
23 struct CZst;
24
25 util_assert_impl_all!(CZst: imp::IntoBytes);
26
27 #[derive(imp::IntoBytes)]
28 #[repr(C)]
29 struct C {
30 a: u8,
31 b: u8,
32 c: util::AU16,
33 }
34
35 util_assert_impl_all!(C: imp::IntoBytes);
36
37 #[derive(imp::IntoBytes)]
38 #[repr(C)]
39 struct SyntacticUnsized {
40 a: u8,
41 b: u8,
42 c: [util::AU16],
43 }
44
45 util_assert_impl_all!(C: imp::IntoBytes);
46
47 #[derive(imp::IntoBytes)]
48 #[repr(transparent)]
49 struct Transparent {
50 a: u8,
51 b: CZst,
52 }
53
54 util_assert_impl_all!(Transparent: imp::IntoBytes);
55
56 #[derive(imp::IntoBytes)]
57 #[repr(transparent)]
58 struct TransparentGeneric<T: ?imp::Sized> {
59 a: CZst,
60 b: T,
61 }
62
63 util_assert_impl_all!(TransparentGeneric<u64>: imp::IntoBytes);
64 util_assert_impl_all!(TransparentGeneric<[u64]>: imp::IntoBytes);
65
66 #[derive(imp::IntoBytes)]
67 #[repr(C, packed)]
68 struct CZstPacked;
69
70 util_assert_impl_all!(CZstPacked: imp::IntoBytes);
71
72 #[derive(imp::IntoBytes)]
73 #[repr(C, packed)]
74 struct CPacked {
75 a: u8,
76 // NOTE: The `u16` type is not guaranteed to have alignment 2, although it
77 // does on many platforms. However, to fix this would require a custom type
78 // with a `#[repr(align(2))]` attribute, and `#[repr(packed)]` types are not
79 // allowed to transitively contain `#[repr(align(...))]` types. Thus, we
80 // have no choice but to use `u16` here. Luckily, these tests run in CI on
81 // platforms on which `u16` has alignment 2, so this isn't that big of a
82 // deal.
83 b: u16,
84 }
85
86 util_assert_impl_all!(CPacked: imp::IntoBytes);
87
88 #[derive(imp::IntoBytes)]
89 #[repr(C, packed(2))]
90 // The same caveats as for CPacked apply - we're assuming u64 is at least
91 // 4-byte aligned by default. Without packed(2), this should fail, as there
92 // would be padding between a/b assuming u64 is 4+ byte aligned.
93 struct CPacked2 {
94 a: u16,
95 b: u64,
96 }
97
98 util_assert_impl_all!(CPacked2: imp::IntoBytes);
99
100 #[derive(imp::IntoBytes)]
101 #[repr(C, packed)]
102 struct CPackedGeneric<T, U: ?imp::Sized> {
103 t: T,
104 // Unsized types stored in `repr(packed)` structs must not be dropped
105 // because dropping them in-place might be unsound depending on the
106 // alignment of the outer struct. Sized types can be dropped by first being
107 // moved to an aligned stack variable, but this isn't possible with unsized
108 // types.
109 u: imp::ManuallyDrop<U>,
110 }
111
112 util_assert_impl_all!(CPackedGeneric<u8, util::AU16>: imp::IntoBytes);
113 util_assert_impl_all!(CPackedGeneric<u8, [util::AU16]>: imp::IntoBytes);
114
115 #[derive(imp::IntoBytes)]
116 #[repr(packed)]
117 struct PackedGeneric<T, U: ?imp::Sized> {
118 t: T,
119 // Unsized types stored in `repr(packed)` structs must not be dropped
120 // because dropping them in-place might be unsound depending on the
121 // alignment of the outer struct. Sized types can be dropped by first being
122 // moved to an aligned stack variable, but this isn't possible with unsized
123 // types.
124 u: imp::ManuallyDrop<U>,
125 }
126
127 util_assert_impl_all!(PackedGeneric<u8, util::AU16>: imp::IntoBytes);
128 util_assert_impl_all!(PackedGeneric<u8, [util::AU16]>: imp::IntoBytes);
129
130 // This test is non-portable, but works so long as Rust happens to lay this
131 // struct out with no padding.
132 #[derive(imp::IntoBytes)]
133 struct Unpacked {
134 a: u8,
135 b: u8,
136 }
137
138 util_assert_impl_all!(Unpacked: imp::IntoBytes);
139
140 #[derive(imp::IntoBytes)]
141 #[repr(C)]
142 struct ReprCGenericOneField<T: ?imp::Sized> {
143 t: T,
144 }
145
146 // Even though `ReprCGenericOneField` has generic type arguments, since it only
147 // has one field, we don't require that its field types implement `Unaligned`.
148 util_assert_impl_all!(ReprCGenericOneField<util::AU16>: imp::IntoBytes);
149 util_assert_impl_all!(ReprCGenericOneField<[util::AU16]>: imp::IntoBytes);
150
151 #[derive(imp::IntoBytes)]
152 #[repr(C)]
153 struct ReprCGenericMultipleFields<T, U: ?imp::Sized> {
154 t: T,
155 u: U,
156 }
157
158 // Since `ReprCGenericMultipleFields` is generic and has more than one field,
159 // all field types must implement `Unaligned`.
160 util_assert_impl_all!(ReprCGenericMultipleFields<u8, [u8; 2]>: imp::IntoBytes);
161 util_assert_impl_all!(ReprCGenericMultipleFields<u8, [[u8; 2]]>: imp::IntoBytes);
162 util_assert_not_impl_any!(ReprCGenericMultipleFields<u8, util::AU16>: imp::IntoBytes);
163 util_assert_not_impl_any!(ReprCGenericMultipleFields<u8, [util::AU16]>: imp::IntoBytes);
164
165 #[derive(imp::IntoBytes)]
166 #[repr(transparent)]
167 struct Unsized {
168 a: [u8],
169 }
170
171 util_assert_impl_all!(Unsized: imp::IntoBytes);
172
173 // Deriving `IntoBytes` should work if the struct has bounded parameters.
174
175 #[derive(imp::IntoBytes)]
176 #[repr(transparent)]
177 struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::IntoBytes, const N: usize>(
178 [T; N],
179 imp::PhantomData<&'a &'b ()>,
180 )
181 where
182 'a: 'b,
183 'b: 'a,
184 T: 'a + 'b + imp::IntoBytes;
185
186 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::IntoBytes);
187
188 // Test for the failure reported in #1182.
189
190 #[derive(imp::IntoBytes)]
191 #[repr(C, packed)]
192 pub struct IndexEntryFlags(u8);
193
194 #[derive(imp::IntoBytes)]
195 #[repr(C, packed)]
196 pub struct IndexEntry<const SIZE_BLOCK_ID: usize> {
197 block_number: imp::native_endian::U64,
198 flags: IndexEntryFlags,
199 block_id: [u8; SIZE_BLOCK_ID],
200 }
201
202 util_assert_impl_all!(IndexEntry<0>: imp::IntoBytes);
203 util_assert_impl_all!(IndexEntry<1>: imp::IntoBytes);