Debian Codegraph @

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