Debian Codegraph @

third_party/rust/zerocopy-derive/tests/struct_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 {
13 static_assertions::assert_impl_all,
14 zerocopy::{DstLayout, KnownLayout},
15 };
16
17 use crate::util::AU16;
18
19 #[derive(KnownLayout)]
20 struct Zst;
21
22 assert_impl_all!(Zst: KnownLayout);
23
24 #[derive(KnownLayout)]
25 struct One {
26 a: bool,
27 }
28
29 assert_impl_all!(One: KnownLayout);
30
31 #[derive(KnownLayout)]
32 struct Two {
33 a: bool,
34 b: Zst,
35 }
36
37 assert_impl_all!(Two: KnownLayout);
38
39 #[derive(KnownLayout)]
40 struct TypeParams<'a, T, I: Iterator> {
41 a: I::Item,
42 b: u8,
43 c: PhantomData<&'a [u8]>,
44 d: PhantomData<&'static str>,
45 e: PhantomData<String>,
46 f: T,
47 }
48
49 assert_impl_all!(TypeParams<'static, (), IntoIter<()>>: KnownLayout);
50 assert_impl_all!(TypeParams<'static, AU16, IntoIter<()>>: KnownLayout);
51
52 // Deriving `KnownLayout` should work if the struct has bounded parameters.
53
54 #[derive(KnownLayout)]
55 #[repr(C)]
56 struct WithParams<'a: 'b, 'b: 'a, const N: usize, T: 'a + 'b + KnownLayout>(
57 [T; N],
58 PhantomData<&'a &'b ()>,
59 )
60 where
61 'a: 'b,
62 'b: 'a,
63 T: 'a + 'b + KnownLayout;
64
65 assert_impl_all!(WithParams<'static, 'static, 42, u8>: KnownLayout);