Debian Codegraph @

debian/rust-vendor/zerocopy-derive/tests/union_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 union is `imp::IntoBytes` if:
16 // - all fields are `imp::IntoBytes`
17 // - `repr(C)` or `repr(transparent)` and
18 // - no padding (size of union equals size of each field type)
19 // - `repr(packed)`
20
21 #[derive(imp::IntoBytes, Clone, Copy)]
22 #[zerocopy(crate = "zerocopy_renamed")]
23 #[repr(C)]
24 union CZst {
25 a: (),
26 }
27
28 util_assert_impl_all!(CZst: imp::IntoBytes);
29
30 #[derive(imp::IntoBytes)]
31 #[zerocopy(crate = "zerocopy_renamed")]
32 #[repr(C)]
33 union C {
34 a: u8,
35 b: u8,
36 }
37
38 util_assert_impl_all!(C: imp::IntoBytes);
39
40 #[derive(imp::IntoBytes)]
41 #[zerocopy(crate = "zerocopy_renamed")]
42 #[repr(C, align(2))]
43 union Aligned {
44 a: [u8; 2],
45 }
46
47 util_assert_impl_all!(Aligned: imp::IntoBytes);
48
49 // Transparent unions are unstable; see issue #60405
50 // <https://github.com/rust-lang/rust/issues/60405> for more information.
51
52 // #[derive(imp::IntoBytes)]
53 // #[repr(transparent)]
54 // union Transparent {
55 // a: u8,
56 // b: CZst,
57 // }
58
59 // is_as_bytes!(Transparent);
60
61 #[derive(imp::IntoBytes)]
62 #[zerocopy(crate = "zerocopy_renamed")]
63 #[repr(C, packed)]
64 union CZstPacked {
65 a: (),
66 }
67
68 util_assert_impl_all!(CZstPacked: imp::IntoBytes);
69
70 #[derive(imp::IntoBytes)]
71 #[zerocopy(crate = "zerocopy_renamed")]
72 #[repr(C, packed)]
73 union CPacked {
74 a: u8,
75 b: i8,
76 }
77
78 util_assert_impl_all!(CPacked: imp::IntoBytes);
79
80 #[derive(imp::IntoBytes)]
81 #[zerocopy(crate = "zerocopy_renamed")]
82 #[repr(C, packed)]
83 union CMultibytePacked {
84 a: i32,
85 b: u32,
86 c: f32,
87 }
88
89 util_assert_impl_all!(CMultibytePacked: imp::IntoBytes);