Debian Codegraph @

third_party/rust/zerocopy-derive/tests/union_known_layout.rs

raw
1 // Copyright 2022 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #![allow(warnings)]
6
7 #[macro_use]
8 mod util;
9
10 use std::{marker::PhantomData, option::IntoIter};
11
12 use {static_assertions::assert_impl_all, zerocopy::KnownLayout};
13
14 #[derive(Clone, Copy, KnownLayout)]
15 union Zst {
16 a: (),
17 }
18
19 assert_impl_all!(Zst: KnownLayout);
20
21 #[derive(KnownLayout)]
22 union One {
23 a: bool,
24 }
25
26 assert_impl_all!(One: KnownLayout);
27
28 #[derive(KnownLayout)]
29 union Two {
30 a: bool,
31 b: Zst,
32 }
33
34 assert_impl_all!(Two: KnownLayout);
35
36 #[derive(KnownLayout)]
37 union TypeParams<'a, T: Copy, I: Iterator>
38 where
39 I::Item: Copy,
40 {
41 a: T,
42 c: I::Item,
43 d: u8,
44 e: PhantomData<&'a [u8]>,
45 f: PhantomData<&'static str>,
46 g: PhantomData<String>,
47 }
48
49 assert_impl_all!(TypeParams<'static, (), IntoIter<()>>: KnownLayout);
50
51 // Deriving `KnownLayout` should work if the union has bounded parameters.
52
53 #[derive(KnownLayout)]
54 #[repr(C)]
55 union WithParams<'a: 'b, 'b: 'a, const N: usize, T: 'a + 'b + KnownLayout>
56 where
57 'a: 'b,
58 'b: 'a,
59 T: 'a + 'b + Copy + KnownLayout,
60 {
61 a: [T; N],
62 b: PhantomData<&'a &'b ()>,
63 }
64
65 assert_impl_all!(WithParams<'static, 'static, 42, u8>: KnownLayout);