Debian Codegraph @

debian/missing-sources/zerocopy-derive/tests/struct_try_from_bytes.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 include!("include.rs");
14
15 // A struct is `imp::TryFromBytes` if:
16 // - all fields are `imp::TryFromBytes`
17
18 #[test]
19 fn zst() {
20 crate::util::test_is_bit_valid::<(), _>((), true);
21 }
22
23 #[derive(imp::TryFromBytes, imp::Immutable, imp::IntoBytes)]
24 #[zerocopy(crate = "zerocopy_renamed")]
25 #[repr(C)]
26 struct One {
27 a: u8,
28 }
29
30 util_assert_impl_all!(One: imp::TryFromBytes);
31
32 #[test]
33 fn one() {
34 crate::util::test_is_bit_valid::<One, _>(One { a: 42 }, true);
35 crate::util::test_is_bit_valid::<One, _>(One { a: 43 }, true);
36 }
37
38 #[derive(imp::TryFromBytes, imp::Immutable, imp::IntoBytes)]
39 #[zerocopy(crate = "zerocopy_renamed")]
40 #[repr(C)]
41 struct Two {
42 a: bool,
43 b: (),
44 }
45
46 util_assert_impl_all!(Two: imp::TryFromBytes);
47
48 #[test]
49 fn two() {
50 crate::util::test_is_bit_valid::<Two, _>(Two { a: false, b: () }, true);
51 crate::util::test_is_bit_valid::<Two, _>(Two { a: true, b: () }, true);
52 crate::util::test_is_bit_valid::<Two, _>([2u8], false);
53 }
54
55 #[derive(imp::KnownLayout, imp::TryFromBytes)]
56 #[zerocopy(crate = "zerocopy_renamed")]
57 #[repr(C)]
58 struct Unsized {
59 a: [u8],
60 }
61
62 util_assert_impl_all!(Unsized: imp::TryFromBytes);
63
64 #[test]
65 fn un_sized() {
66 // FIXME(#5): Use `try_transmute` in this test once it's available.
67 let mut buf = [16u8, 12, 42];
68 let candidate = ::zerocopy_renamed::Ptr::from_mut(&mut buf[..]);
69 // SAFETY: `&Unsized` consists entirely of initialized bytes.
70 let candidate = unsafe { candidate.assume_initialized() };
71
72 let mut candidate = {
73 use imp::pointer::{cast::CastUnsized, BecauseExclusive};
74 candidate.cast::<_, CastUnsized, (_, BecauseExclusive)>()
75 };
76
77 // SAFETY: `candidate`'s referent is as-initialized as `Two`.
78 let mut candidate = unsafe { candidate.assume_initialized() };
79 let is_bit_valid = <Unsized as imp::TryFromBytes>::is_bit_valid(candidate.reborrow_shared());
80 imp::assert!(is_bit_valid);
81 }
82
83 #[derive(imp::TryFromBytes)]
84 #[zerocopy(crate = "zerocopy_renamed")]
85 #[repr(C)]
86 struct TypeParams<'a, T: ?imp::Sized, I: imp::Iterator> {
87 a: I::Item,
88 b: u8,
89 c: imp::PhantomData<&'a [u8]>,
90 d: imp::PhantomData<&'static str>,
91 e: imp::PhantomData<imp::String>,
92 f: T,
93 }
94
95 util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::TryFromBytes);
96 util_assert_impl_all!(TypeParams<'static, util::AU16, imp::IntoIter<()>>: imp::TryFromBytes);
97 util_assert_impl_all!(TypeParams<'static, [util::AU16], imp::IntoIter<()>>: imp::TryFromBytes);
98
99 // Deriving `imp::TryFromBytes` should work if the struct has bounded
100 // parameters.
101
102 #[derive(imp::TryFromBytes)]
103 #[zerocopy(crate = "zerocopy_renamed")]
104 #[repr(transparent)]
105 struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::TryFromBytes, const N: usize>(
106 imp::PhantomData<&'a &'b ()>,
107 [T],
108 )
109 where
110 'a: 'b,
111 'b: 'a,
112 T: 'a + 'b + imp::TryFromBytes;
113
114 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::TryFromBytes);
115
116 #[derive(imp::FromBytes, imp::IntoBytes)]
117 #[zerocopy(crate = "zerocopy_renamed")]
118 #[repr(C)]
119 struct MaybeFromBytes<T>(T);
120
121 #[test]
122 fn test_maybe_from_bytes() {
123 // When deriving `FromBytes` on a type with no generic parameters, we emit a
124 // trivial `is_bit_valid` impl that always returns true. This test confirms
125 // that we *don't* spuriously do that when generic parameters are present.
126
127 crate::util::test_is_bit_valid::<MaybeFromBytes<bool>, _>(MaybeFromBytes(false), true);
128 crate::util::test_is_bit_valid::<MaybeFromBytes<bool>, _>(MaybeFromBytes(true), true);
129 crate::util::test_is_bit_valid::<MaybeFromBytes<bool>, _>([2u8], false);
130 }
131
132 #[derive(Debug, PartialEq, Eq, imp::TryFromBytes, imp::Immutable, imp::KnownLayout)]
133 #[zerocopy(crate = "zerocopy_renamed")]
134 #[repr(C, packed)]
135 struct CPacked {
136 a: u8,
137 // NOTE: The `u32` type is not guaranteed to have alignment 4, although it
138 // does on many platforms. However, to fix this would require a custom type
139 // with a `#[repr(align(4))]` attribute, and `#[repr(packed)]` types are not
140 // allowed to transitively contain `#[repr(align(...))]` types. Thus, we
141 // have no choice but to use `u32` here. Luckily, these tests run in CI on
142 // platforms on which `u32` has alignment 4, so this isn't that big of a
143 // deal.
144 b: u32,
145 }
146
147 #[test]
148 fn c_packed() {
149 let candidate = &[42u8, 0xFF, 0xFF, 0xFF, 0xFF];
150 let converted = <CPacked as imp::TryFromBytes>::try_ref_from_bytes(candidate);
151 imp::assert_eq!(converted, imp::Ok(&CPacked { a: 42, b: u32::MAX }));
152 }
153
154 #[derive(imp::TryFromBytes, imp::KnownLayout, imp::Immutable)]
155 #[zerocopy(crate = "zerocopy_renamed")]
156 #[repr(C, packed)]
157 struct CPackedUnsized {
158 a: u8,
159 // NOTE: The `u32` type is not guaranteed to have alignment 4, although it
160 // does on many platforms. However, to fix this would require a custom type
161 // with a `#[repr(align(4))]` attribute, and `#[repr(packed)]` types are not
162 // allowed to transitively contain `#[repr(align(...))]` types. Thus, we
163 // have no choice but to use `u32` here. Luckily, these tests run in CI on
164 // platforms on which `u32` has alignment 4, so this isn't that big of a
165 // deal.
166 b: [u32],
167 }
168
169 #[test]
170 fn c_packed_unsized() {
171 let candidate = &[42u8, 0xFF, 0xFF, 0xFF, 0xFF];
172 let converted = <CPackedUnsized as imp::TryFromBytes>::try_ref_from_bytes(candidate);
173 imp::assert!(converted.is_ok());
174 }
175
176 #[derive(imp::TryFromBytes)]
177 #[zerocopy(crate = "zerocopy_renamed")]
178 #[repr(packed)]
179 struct PackedUnsized {
180 a: u8,
181 // NOTE: The `u32` type is not guaranteed to have alignment 4, although it
182 // does on many platforms. However, to fix this would require a custom type
183 // with a `#[repr(align(4))]` attribute, and `#[repr(packed)]` types are not
184 // allowed to transitively contain `#[repr(align(...))]` types. Thus, we
185 // have no choice but to use `u32` here. Luckily, these tests run in CI on
186 // platforms on which `u32` has alignment 4, so this isn't that big of a
187 // deal.
188 b: [u32],
189 }
190
191 #[test]
192 fn packed_unsized() {
193 let candidate = &[42u8, 0xFF, 0xFF, 0xFF, 0xFF];
194 let converted = <CPackedUnsized as imp::TryFromBytes>::try_ref_from_bytes(candidate);
195 imp::assert!(converted.is_ok());
196
197 let candidate = &[42u8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
198 let converted = <CPackedUnsized as imp::TryFromBytes>::try_ref_from_bytes(candidate);
199 imp::assert!(converted.is_err());
200
201 let candidate = &[42u8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
202 let converted = <CPackedUnsized as imp::TryFromBytes>::try_ref_from_bytes(candidate);
203 imp::assert!(converted.is_ok());
204 }
205
206 #[derive(imp::TryFromBytes)]
207 #[zerocopy(crate = "zerocopy_renamed")]
208 struct A;
209
210 #[derive(imp::TryFromBytes)]
211 #[zerocopy(crate = "zerocopy_renamed")]
212 struct B {
213 a: A,
214 }
215
216 #[derive(imp::TryFromBytes)]
217 #[zerocopy(crate = "zerocopy_renamed")]
218 #[repr(C)]
219 struct RawIdent {
220 r#type: u8,
221 }
222
223 util_assert_impl_all!(RawIdent: imp::TryFromBytes);