Debian Codegraph @

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