Debian Codegraph @

third_party/rust/zerocopy-derive/tests/union_as_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 #![allow(warnings)]
10
11 use std::{marker::PhantomData, option::IntoIter};
12
13 use {static_assertions::assert_impl_all, zerocopy::AsBytes};
14
15 // A union is `AsBytes` if:
16 // - all fields are `AsBytes`
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(AsBytes, Clone, Copy)]
22 #[repr(C)]
23 union CZst {
24 a: (),
25 }
26
27 assert_impl_all!(CZst: AsBytes);
28
29 #[derive(AsBytes)]
30 #[repr(C)]
31 union C {
32 a: u8,
33 b: u8,
34 }
35
36 assert_impl_all!(C: AsBytes);
37
38 // Transparent unions are unstable; see issue #60405
39 // <https://github.com/rust-lang/rust/issues/60405> for more information.
40
41 // #[derive(AsBytes)]
42 // #[repr(transparent)]
43 // union Transparent {
44 // a: u8,
45 // b: CZst,
46 // }
47
48 // is_as_bytes!(Transparent);
49
50 #[derive(AsBytes)]
51 #[repr(C, packed)]
52 union CZstPacked {
53 a: (),
54 }
55
56 assert_impl_all!(CZstPacked: AsBytes);
57
58 #[derive(AsBytes)]
59 #[repr(C, packed)]
60 union CPacked {
61 a: u8,
62 b: i8,
63 }
64
65 assert_impl_all!(CPacked: AsBytes);
66
67 #[derive(AsBytes)]
68 #[repr(C, packed)]
69 union CMultibytePacked {
70 a: i32,
71 b: u32,
72 c: f32,
73 }
74
75 assert_impl_all!(CMultibytePacked: AsBytes);