Debian Codegraph @

debian/rust-vendor/zerocopy-derive/tests/struct_unaligned.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 `Unaligned` if:
16 // - `repr(align)` is no more than 1 and either
17 // - `repr(C)` or `repr(transparent)` and
18 // - all fields Unaligned
19 // - `repr(packed)`
20
21 #[derive(imp::Unaligned)]
22 #[zerocopy(crate = "zerocopy_renamed")]
23 #[repr(C)]
24 struct Foo {
25 a: u8,
26 }
27
28 util_assert_impl_all!(Foo: imp::Unaligned);
29
30 #[derive(imp::Unaligned)]
31 #[zerocopy(crate = "zerocopy_renamed")]
32 #[repr(transparent)]
33 struct Bar {
34 a: u8,
35 }
36
37 util_assert_impl_all!(Bar: imp::Unaligned);
38
39 #[derive(imp::Unaligned)]
40 #[zerocopy(crate = "zerocopy_renamed")]
41 #[repr(packed)]
42 struct Baz {
43 // NOTE: The `u16` type is not guaranteed to have alignment 2, although it
44 // does on many platforms. However, to fix this would require a custom type
45 // with a `#[repr(align(2))]` attribute, and `#[repr(packed)]` types are not
46 // allowed to transitively contain `#[repr(align(...))]` types. Thus, we
47 // have no choice but to use `u16` here. Luckily, these tests run in CI on
48 // platforms on which `u16` has alignment 2, so this isn't that big of a
49 // deal.
50 a: u16,
51 }
52
53 util_assert_impl_all!(Baz: imp::Unaligned);
54
55 #[derive(imp::Unaligned)]
56 #[zerocopy(crate = "zerocopy_renamed")]
57 #[repr(C, align(1))]
58 struct FooAlign {
59 a: u8,
60 }
61
62 util_assert_impl_all!(FooAlign: imp::Unaligned);
63
64 #[derive(imp::Unaligned)]
65 #[zerocopy(crate = "zerocopy_renamed")]
66 #[repr(transparent)]
67 struct Unsized {
68 a: [u8],
69 }
70
71 util_assert_impl_all!(Unsized: imp::Unaligned);
72
73 #[derive(imp::Unaligned)]
74 #[zerocopy(crate = "zerocopy_renamed")]
75 #[repr(C)]
76 struct TypeParams<'a, T: ?imp::Sized, I: imp::Iterator> {
77 a: I::Item,
78 b: u8,
79 c: imp::PhantomData<&'a [::core::primitive::u8]>,
80 d: imp::PhantomData<&'static ::core::primitive::str>,
81 e: imp::PhantomData<imp::String>,
82 f: T,
83 }
84
85 util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::Unaligned);
86 util_assert_impl_all!(TypeParams<'static, ::core::primitive::u8, imp::IntoIter<()>>: imp::Unaligned);
87 util_assert_impl_all!(TypeParams<'static, [::core::primitive::u8], imp::IntoIter<()>>: imp::Unaligned);
88
89 // Deriving `Unaligned` should work if the struct has bounded parameters.
90
91 #[derive(imp::Unaligned)]
92 #[zerocopy(crate = "zerocopy_renamed")]
93 #[repr(transparent)]
94 struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::Unaligned, const N: usize>(
95 [T; N],
96 imp::PhantomData<&'a &'b ()>,
97 )
98 where
99 'a: 'b,
100 'b: 'a,
101 T: 'a + 'b + imp::Unaligned;
102
103 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::Unaligned);