Debian Codegraph @

debian/missing-sources/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 // Transparent unions are unstable; see issue #60405
41 // <https://github.com/rust-lang/rust/issues/60405> for more information.
42
43 // #[derive(imp::IntoBytes)]
44 // #[repr(transparent)]
45 // union Transparent {
46 // a: u8,
47 // b: CZst,
48 // }
49
50 // is_as_bytes!(Transparent);
51
52 #[derive(imp::IntoBytes)]
53 #[zerocopy(crate = "zerocopy_renamed")]
54 #[repr(C, packed)]
55 union CZstPacked {
56 a: (),
57 }
58
59 util_assert_impl_all!(CZstPacked: imp::IntoBytes);
60
61 #[derive(imp::IntoBytes)]
62 #[zerocopy(crate = "zerocopy_renamed")]
63 #[repr(C, packed)]
64 union CPacked {
65 a: u8,
66 b: i8,
67 }
68
69 util_assert_impl_all!(CPacked: imp::IntoBytes);
70
71 #[derive(imp::IntoBytes)]
72 #[zerocopy(crate = "zerocopy_renamed")]
73 #[repr(C, packed)]
74 union CMultibytePacked {
75 a: i32,
76 b: u32,
77 c: f32,
78 }
79
80 util_assert_impl_all!(CMultibytePacked: imp::IntoBytes);