| 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 |
#![allow(warnings)] |
| 10 |
|
| 11 |
mod util; |
| 12 |
|
| 13 |
use std::{marker::PhantomData, option::IntoIter}; |
| 14 |
|
| 15 |
use {static_assertions::assert_impl_all, zerocopy::Unaligned}; |
| 16 |
|
| 17 |
use crate::util::AU16; |
| 18 |
|
| 19 |
// A struct is `Unaligned` if: |
| 20 |
// - `repr(align)` is no more than 1 and either |
| 21 |
// - `repr(C)` or `repr(transparent)` and |
| 22 |
// - all fields Unaligned |
| 23 |
// - `repr(packed)` |
| 24 |
|
| 25 |
#[derive(Unaligned)] |
| 26 |
#[repr(C)] |
| 27 |
struct Foo { |
| 28 |
a: u8, |
| 29 |
} |
| 30 |
|
| 31 |
assert_impl_all!(Foo: Unaligned); |
| 32 |
|
| 33 |
#[derive(Unaligned)] |
| 34 |
#[repr(transparent)] |
| 35 |
struct Bar { |
| 36 |
a: u8, |
| 37 |
} |
| 38 |
|
| 39 |
assert_impl_all!(Bar: Unaligned); |
| 40 |
|
| 41 |
#[derive(Unaligned)] |
| 42 |
#[repr(packed)] |
| 43 |
struct 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 |
assert_impl_all!(Baz: Unaligned); |
| 55 |
|
| 56 |
#[derive(Unaligned)] |
| 57 |
#[repr(C, align(1))] |
| 58 |
struct FooAlign { |
| 59 |
a: u8, |
| 60 |
} |
| 61 |
|
| 62 |
assert_impl_all!(FooAlign: Unaligned); |
| 63 |
|
| 64 |
#[derive(Unaligned)] |
| 65 |
#[repr(transparent)] |
| 66 |
struct Unsized { |
| 67 |
a: [u8], |
| 68 |
} |
| 69 |
|
| 70 |
assert_impl_all!(Unsized: Unaligned); |
| 71 |
|
| 72 |
#[derive(Unaligned)] |
| 73 |
#[repr(C)] |
| 74 |
struct TypeParams<'a, T: ?Sized, I: Iterator> { |
| 75 |
a: I::Item, |
| 76 |
b: u8, |
| 77 |
c: PhantomData<&'a [u8]>, |
| 78 |
d: PhantomData<&'static str>, |
| 79 |
e: PhantomData<String>, |
| 80 |
f: T, |
| 81 |
} |
| 82 |
|
| 83 |
assert_impl_all!(TypeParams<'static, (), IntoIter<()>>: Unaligned); |
| 84 |
assert_impl_all!(TypeParams<'static, u8, IntoIter<()>>: Unaligned); |
| 85 |
assert_impl_all!(TypeParams<'static, [u8], IntoIter<()>>: Unaligned); |
| 86 |
|
| 87 |
// Deriving `Unaligned` should work if the struct has bounded parameters. |
| 88 |
|
| 89 |
#[derive(Unaligned)] |
| 90 |
#[repr(transparent)] |
| 91 |
struct WithParams<'a: 'b, 'b: 'a, const N: usize, T: 'a + 'b + Unaligned>( |
| 92 |
[T; N], |
| 93 |
PhantomData<&'a &'b ()>, |
| 94 |
) |
| 95 |
where |
| 96 |
'a: 'b, |
| 97 |
'b: 'a, |
| 98 |
T: 'a + 'b + Unaligned; |
| 99 |
|
| 100 |
assert_impl_all!(WithParams<'static, 'static, 42, u8>: Unaligned); |