Debian Codegraph @

debian/rust-vendor/zerocopy-derive/tests/union_known_layout.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 #[derive(Clone, Copy, imp::KnownLayout)]
16 #[zerocopy(crate = "zerocopy_renamed")]
17 union Zst {
18 a: (),
19 }
20
21 util_assert_impl_all!(Zst: imp::KnownLayout);
22
23 #[derive(imp::KnownLayout)]
24 #[zerocopy(crate = "zerocopy_renamed")]
25 union One {
26 a: bool,
27 }
28
29 util_assert_impl_all!(One: imp::KnownLayout);
30
31 #[derive(imp::KnownLayout)]
32 #[zerocopy(crate = "zerocopy_renamed")]
33 union Two {
34 a: bool,
35 b: Zst,
36 }
37
38 util_assert_impl_all!(Two: imp::KnownLayout);
39
40 #[derive(imp::KnownLayout)]
41 #[zerocopy(crate = "zerocopy_renamed")]
42 union TypeParams<'a, T: imp::Copy, I: imp::Iterator>
43 where
44 I::Item: imp::Copy,
45 {
46 a: T,
47 c: I::Item,
48 d: u8,
49 e: imp::PhantomData<&'a [u8]>,
50 f: imp::PhantomData<&'static str>,
51 g: imp::PhantomData<imp::String>,
52 }
53
54 util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::KnownLayout);
55
56 // Deriving `imp::KnownLayout` should work if the union has bounded parameters.
57
58 #[derive(imp::KnownLayout)]
59 #[zerocopy(crate = "zerocopy_renamed")]
60 #[repr(C)]
61 union WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::KnownLayout, const N: usize>
62 where
63 'a: 'b,
64 'b: 'a,
65 T: 'a + 'b + imp::Copy + imp::KnownLayout,
66 {
67 a: [T; N],
68 b: imp::PhantomData<&'a &'b ()>,
69 }
70
71 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::KnownLayout);