Debian Codegraph @

.cargo/vendor/zerocopy-derive/tests/struct_from_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 `FromBytes` if:
16 // - all fields are `FromBytes`
17
18 #[derive(imp::FromBytes)]
19 struct Zst;
20
21 util_assert_impl_all!(Zst: imp::FromBytes);
22 test_trivial_is_bit_valid!(Zst => test_zst_trivial_is_bit_valid);
23
24 #[derive(imp::FromBytes)]
25 struct One {
26 a: u8,
27 }
28
29 util_assert_impl_all!(One: imp::FromBytes);
30 test_trivial_is_bit_valid!(One => test_one_trivial_is_bit_valid);
31
32 #[derive(imp::FromBytes)]
33 struct Two {
34 a: u8,
35 b: Zst,
36 }
37
38 util_assert_impl_all!(Two: imp::FromBytes);
39 test_trivial_is_bit_valid!(Two => test_two_trivial_is_bit_valid);
40
41 #[derive(imp::FromBytes)]
42 struct Unsized {
43 a: [u8],
44 }
45
46 util_assert_impl_all!(Unsized: imp::FromBytes);
47
48 #[derive(imp::FromBytes)]
49 struct TypeParams<'a, T: ?imp::Sized, I: imp::Iterator> {
50 a: I::Item,
51 b: u8,
52 c: imp::PhantomData<&'a [::core::primitive::u8]>,
53 d: imp::PhantomData<&'static ::core::primitive::str>,
54 e: imp::PhantomData<imp::String>,
55 f: T,
56 }
57
58 util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::FromBytes);
59 util_assert_impl_all!(TypeParams<'static, util::AU16, imp::IntoIter<()>>: imp::FromBytes);
60 util_assert_impl_all!(TypeParams<'static, [util::AU16], imp::IntoIter<()>>: imp::FromBytes);
61 test_trivial_is_bit_valid!(TypeParams<'static, (), imp::IntoIter<()>> => test_type_params_trivial_is_bit_valid);
62
63 // Deriving `FromBytes` should work if the struct has bounded parameters.
64
65 #[derive(imp::FromBytes)]
66 #[repr(transparent)]
67 struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::FromBytes, const N: usize>(
68 [T; N],
69 imp::PhantomData<&'a &'b ()>,
70 )
71 where
72 'a: 'b,
73 'b: 'a,
74 T: 'a + 'b + imp::FromBytes;
75
76 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::FromBytes);
77 test_trivial_is_bit_valid!(WithParams<'static, 'static, u8, 42> => test_with_params_trivial_is_bit_valid);