Debian Codegraph @

debian/missing-sources/zerocopy-derive/tests/struct_no_cell.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(imp::Immutable)]
16 #[zerocopy(crate = "zerocopy_renamed")]
17 struct Zst;
18
19 util_assert_impl_all!(Zst: imp::Immutable);
20
21 #[derive(imp::Immutable)]
22 #[zerocopy(crate = "zerocopy_renamed")]
23 struct One {
24 a: bool,
25 }
26
27 util_assert_impl_all!(One: imp::Immutable);
28
29 #[derive(imp::Immutable)]
30 #[zerocopy(crate = "zerocopy_renamed")]
31 struct Two {
32 a: bool,
33 b: Zst,
34 }
35
36 util_assert_impl_all!(Two: imp::Immutable);
37
38 #[derive(imp::Immutable)]
39 #[zerocopy(crate = "zerocopy_renamed")]
40 struct Three {
41 a: [u8],
42 }
43
44 util_assert_impl_all!(Three: imp::Immutable);
45
46 #[derive(imp::Immutable)]
47 #[zerocopy(crate = "zerocopy_renamed")]
48 struct Four<'a> {
49 field: &'a imp::UnsafeCell<u8>,
50 }
51
52 util_assert_impl_all!(Four<'static>: imp::Immutable);
53
54 #[derive(imp::Immutable)]
55 #[zerocopy(crate = "zerocopy_renamed")]
56 struct TypeParams<'a, T, U, I: imp::Iterator> {
57 a: I::Item,
58 b: u8,
59 c: imp::PhantomData<&'a [::core::primitive::u8]>,
60 d: imp::PhantomData<&'static ::core::primitive::str>,
61 e: imp::PhantomData<imp::String>,
62 f: imp::PhantomData<U>,
63 g: T,
64 }
65
66 util_assert_impl_all!(TypeParams<'static, (), (), imp::IntoIter<()>>: imp::Immutable);
67 util_assert_impl_all!(TypeParams<'static, util::AU16, util::AU16, imp::IntoIter<()>>: imp::Immutable);
68 util_assert_impl_all!(TypeParams<'static, util::AU16, imp::UnsafeCell<u8>, imp::IntoIter<()>>: imp::Immutable);
69 util_assert_not_impl_any!(TypeParams<'static, imp::UnsafeCell<()>, (), imp::IntoIter<()>>: imp::Immutable);
70 util_assert_not_impl_any!(TypeParams<'static, [imp::UnsafeCell<u8>; 0], (), imp::IntoIter<()>>: imp::Immutable);
71 util_assert_not_impl_any!(TypeParams<'static, (), (), imp::IntoIter<imp::UnsafeCell<()>>>: imp::Immutable);
72
73 trait Trait {
74 type Assoc;
75 }
76
77 impl<T> Trait for imp::UnsafeCell<T> {
78 type Assoc = T;
79 }
80
81 #[derive(imp::Immutable)]
82 #[zerocopy(crate = "zerocopy_renamed")]
83 struct WithAssocType<T: Trait> {
84 field: <T as Trait>::Assoc,
85 }
86
87 util_assert_impl_all!(WithAssocType<imp::UnsafeCell<u8>>: imp::Immutable);
88
89 // Deriving `Immutable` should work if the struct has bounded parameters.
90
91 #[derive(imp::Immutable)]
92 #[zerocopy(crate = "zerocopy_renamed")]
93 #[repr(C)]
94 struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::Immutable, const N: usize>(
95 [T; N],
96 imp::PhantomData<&'a &'b ()>,
97 imp::PhantomData<imp::UnsafeCell<()>>,
98 &'a imp::UnsafeCell<()>,
99 )
100 where
101 'a: 'b,
102 'b: 'a,
103 T: 'a + 'b + imp::Immutable;
104
105 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::Immutable);