Debian Codegraph @

debian/rust-vendor/zerocopy-derive/tests/union_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 union is `imp::FromBytes` if:
16 // - all fields are `imp::FromBytes`
17
18 #[derive(Clone, Copy, imp::Immutable, imp::FromBytes)]
19 #[zerocopy(crate = "zerocopy_renamed")]
20 union Zst {
21 a: (),
22 }
23
24 util_assert_impl_all!(Zst: imp::FromBytes);
25 test_trivial_is_bit_valid!(Zst => test_zst_trivial_is_bit_valid);
26
27 #[derive(imp::Immutable, imp::FromBytes)]
28 #[zerocopy(crate = "zerocopy_renamed")]
29 union One {
30 a: u8,
31 }
32
33 util_assert_impl_all!(One: imp::FromBytes);
34 test_trivial_is_bit_valid!(One => test_one_trivial_is_bit_valid);
35
36 #[derive(imp::Immutable, imp::FromBytes)]
37 #[zerocopy(crate = "zerocopy_renamed")]
38 union Two {
39 a: u8,
40 b: Zst,
41 }
42
43 util_assert_impl_all!(Two: imp::FromBytes);
44 test_trivial_is_bit_valid!(Two => test_two_trivial_is_bit_valid);
45
46 #[derive(imp::Immutable, imp::FromBytes)]
47 #[zerocopy(crate = "zerocopy_renamed")]
48 union TypeParams<'a, T: imp::Copy, I: imp::Iterator>
49 where
50 I::Item: imp::Copy,
51 {
52 a: T,
53 c: I::Item,
54 d: u8,
55 e: imp::PhantomData<&'a [u8]>,
56 f: imp::PhantomData<&'static str>,
57 g: imp::PhantomData<imp::String>,
58 }
59
60 util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::FromBytes);
61 test_trivial_is_bit_valid!(TypeParams<'static, (), imp::IntoIter<()>> => test_type_params_trivial_is_bit_valid);
62
63 // Deriving `imp::FromBytes` should work if the union has bounded parameters.
64
65 #[derive(imp::Immutable, imp::FromBytes)]
66 #[zerocopy(crate = "zerocopy_renamed")]
67 #[repr(C)]
68 union WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::FromBytes, const N: usize>
69 where
70 'a: 'b,
71 'b: 'a,
72 T: 'a + 'b + imp::Copy + imp::FromBytes,
73 {
74 a: [T; N],
75 b: imp::PhantomData<&'a &'b ()>,
76 }
77
78 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::FromBytes);
79 test_trivial_is_bit_valid!(WithParams<'static, 'static, u8, 42> => test_with_params_trivial_is_bit_valid);
80
81 #[derive(imp::FromBytes)]
82 #[zerocopy(crate = "zerocopy_renamed")]
83 #[repr(C)]
84 union UnsafeCellUnion {
85 a: imp::ManuallyDrop<imp::UnsafeCell<u8>>,
86 }
87
88 util_assert_impl_all!(UnsafeCellUnion: imp::FromBytes);
89 test_trivial_is_bit_valid!(UnsafeCellUnion => test_unsafe_cell_union_trivial_is_bit_valid);