Debian Codegraph @

debian/rust-vendor/zerocopy-derive/tests/union_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 union 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 union Foo {
25 a: imp::u8,
26 }
27
28 util_assert_impl_all!(Foo: imp::Unaligned);
29
30 // Transparent unions are unstable; see issue #60405
31 // <https://github.com/rust-lang/rust/issues/60405> for more information.
32
33 // #[derive(Unaligned)]
34 // #[repr(transparent)]
35 // union Bar {
36 // a: u8,
37 // }
38
39 // is_unaligned!(Bar);
40 #[derive(imp::Unaligned)]
41 #[zerocopy(crate = "zerocopy_renamed")]
42 #[repr(packed)]
43 union Baz {
44 // NOTE: The `u16` type is not guaranteed to have alignment 2, although it
45 // does on many platforms. However, to fix this would require a custom type
46 // with a `#[repr(align(2))]` attribute, and `#[repr(packed)]` types are not
47 // allowed to transitively contain `#[repr(align(...))]` types. Thus, we
48 // have no choice but to use `u16` here. Luckily, these tests run in CI on
49 // platforms on which `u16` has alignment 2, so this isn't that big of a
50 // deal.
51 a: u16,
52 }
53
54 util_assert_impl_all!(Baz: imp::Unaligned);
55
56 #[derive(imp::Unaligned)]
57 #[zerocopy(crate = "zerocopy_renamed")]
58 #[repr(C, align(1))]
59 union FooAlign {
60 a: imp::u8,
61 }
62
63 util_assert_impl_all!(FooAlign: imp::Unaligned);
64
65 #[derive(imp::Unaligned)]
66 #[zerocopy(crate = "zerocopy_renamed")]
67 #[repr(C)]
68 union TypeParams<'a, T: imp::Copy, I: imp::Iterator>
69 where
70 I::Item: imp::Copy,
71 {
72 a: T,
73 c: I::Item,
74 d: u8,
75 e: imp::PhantomData<&'a [imp::u8]>,
76 f: imp::PhantomData<&'static imp::str>,
77 g: imp::PhantomData<imp::String>,
78 }
79
80 util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::Unaligned);