Debian Codegraph @

debian/missing-sources/zerocopy-derive/tests/union_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 // - any of its fields are `imp::TryFromBytes`
17
18 #[derive(imp::Immutable, imp::TryFromBytes)]
19 #[zerocopy(crate = "zerocopy_renamed")]
20 union One {
21 a: u8,
22 }
23
24 util_assert_impl_all!(One: imp::TryFromBytes);
25
26 #[test]
27 fn one() {
28 crate::util::test_is_bit_valid::<One, _>([42u8], true);
29 crate::util::test_is_bit_valid::<One, _>([43u8], true);
30 }
31
32 #[derive(imp::Immutable, imp::TryFromBytes, imp::IntoBytes)]
33 #[zerocopy(crate = "zerocopy_renamed")]
34 #[repr(C)]
35 union Two {
36 a: bool,
37 b: bool,
38 }
39
40 util_assert_impl_all!(Two: imp::TryFromBytes);
41
42 #[test]
43 fn two() {
44 crate::util::test_is_bit_valid::<Two, _>(Two { a: false }, true);
45 crate::util::test_is_bit_valid::<Two, _>(Two { b: true }, true);
46 crate::util::test_is_bit_valid::<Two, _>([2u8], false);
47 }
48
49 #[derive(imp::Immutable, imp::TryFromBytes)]
50 #[zerocopy(crate = "zerocopy_renamed")]
51 #[repr(C)]
52 union BoolAndZst {
53 a: bool,
54 b: (),
55 }
56
57 #[test]
58 fn bool_and_zst() {
59 crate::util::test_is_bit_valid::<BoolAndZst, _>([0u8], true);
60 crate::util::test_is_bit_valid::<BoolAndZst, _>([1u8], true);
61 crate::util::test_is_bit_valid::<BoolAndZst, _>([2u8], true);
62 }
63
64 #[derive(imp::FromBytes)]
65 #[zerocopy(crate = "zerocopy_renamed")]
66 #[repr(C)]
67 union MaybeFromBytes<T: imp::Copy> {
68 t: T,
69 }
70
71 #[test]
72 fn test_maybe_from_bytes() {
73 // When deriving `FromBytes` on a type with no generic parameters, we emit a
74 // trivial `is_bit_valid` impl that always returns true. This test confirms
75 // that we *don't* spuriously do that when generic parameters are present.
76
77 crate::util::test_is_bit_valid::<MaybeFromBytes<bool>, _>([2u8], false);
78 }
79
80 #[derive(imp::Immutable, imp::TryFromBytes)]
81 #[zerocopy(crate = "zerocopy_renamed")]
82 #[repr(C)]
83 union TypeParams<'a, T: imp::Copy, I: imp::Iterator>
84 where
85 I::Item: imp::Copy,
86 {
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; 2], imp::IntoIter<()>>: imp::TryFromBytes);
98
99 // Deriving `imp::TryFromBytes` should work if the union has bounded parameters.
100
101 #[derive(imp::Immutable, imp::TryFromBytes)]
102 #[zerocopy(crate = "zerocopy_renamed")]
103 #[repr(C)]
104 union WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::TryFromBytes, const N: usize>
105 where
106 'a: 'b,
107 'b: 'a,
108 T: 'a + 'b + imp::TryFromBytes + imp::Copy,
109 {
110 a: imp::PhantomData<&'a &'b ()>,
111 b: T,
112 }
113
114 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::TryFromBytes);
115
116 #[derive(Clone, Copy, imp::TryFromBytes, imp::Immutable)]
117 #[zerocopy(crate = "zerocopy_renamed")]
118 struct A;
119
120 #[derive(imp::TryFromBytes)]
121 #[zerocopy(crate = "zerocopy_renamed")]
122 union B {
123 a: A,
124 }
125
126 #[derive(imp::TryFromBytes)]
127 #[zerocopy(crate = "zerocopy_renamed")]
128 #[repr(C)]
129 union UnsafeCellUnion {
130 a: imp::ManuallyDrop<imp::UnsafeCell<bool>>,
131 }
132
133 util_assert_impl_all!(UnsafeCellUnion: imp::TryFromBytes);
134
135 #[test]
136 fn unsafe_cell_union() {
137 crate::util::test_is_bit_valid::<UnsafeCellUnion, _>([0u8], true);
138 crate::util::test_is_bit_valid::<UnsafeCellUnion, _>([1u8], true);
139 crate::util::test_is_bit_valid::<UnsafeCellUnion, _>([2u8], false);
140 }