#![allow(missing_debug_implementations)]
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
#[cfg(not(target_pointer_width = "16"))]
use core::ptr::{self, NonNull};
use core::{
marker::PhantomData,
mem::{self, ManuallyDrop},
};
use crate::{
pointer::{
invariant::{self, BecauseExclusive, BecauseImmutable, Invariants},
BecauseInvariantsEq, InvariantsEq, SizeEq, TryTransmuteFromPtr,
},
FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout, Ptr, TryFromBytes, ValidityError,
};
pub unsafe trait Field<Index> {
type Type: ?Sized;
}
#[cfg_attr(
not(no_zerocopy_diagnostic_on_unimplemented_1_78_0),
diagnostic::on_unimplemented(
message = "`{T}` has {PADDING_BYTES} total byte(s) of padding",
label = "types with padding cannot implement `IntoBytes`",
note = "consider using `zerocopy::Unalign` to lower the alignment of individual fields",
note = "consider adding explicit fields where padding would be",
note = "consider using `#[repr(packed)]` to remove padding"
)
)]
pub trait PaddingFree<T: ?Sized, const PADDING_BYTES: usize> {}
impl<T: ?Sized> PaddingFree<T, 0> for () {}
#[cfg_attr(
not(no_zerocopy_diagnostic_on_unimplemented_1_78_0),
diagnostic::on_unimplemented(
message = "`{T}` has one or more padding bytes",
label = "types with padding cannot implement `IntoBytes`",
note = "consider using `zerocopy::Unalign` to lower the alignment of individual fields",
note = "consider adding explicit fields where padding would be",
note = "consider using `#[repr(packed)]` to remove padding"
)
)]
pub trait DynamicPaddingFree<T: ?Sized, const HAS_PADDING: bool> {}
impl<T: ?Sized> DynamicPaddingFree<T, false> for () {}
#[repr(C)]
pub struct AlignOf<T> {
_u: u8,
_a: [T; 0],
}
impl<T> AlignOf<T> {
#[inline(never)] #[cfg_attr(
all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS),
coverage(off)
)]
pub fn into_t(self) -> T {
unreachable!()
}
}
#[repr(C)]
pub union MaxAlignsOf<T, U> {
_t: ManuallyDrop<AlignOf<T>>,
_u: ManuallyDrop<AlignOf<U>>,
}
impl<T, U> MaxAlignsOf<T, U> {
#[inline(never)] #[cfg_attr(
all(coverage_nightly, __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS),
coverage(off)
)]
pub fn new(_t: T, _u: U) -> MaxAlignsOf<T, U> {
unreachable!()
}
}
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
#[cfg(not(target_pointer_width = "16"))]
const _64K: usize = 1 << 16;
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
#[cfg(not(target_pointer_width = "16"))]
#[repr(C, align(65536))]
struct Aligned64kAllocation([u8; _64K]);
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
#[cfg(not(target_pointer_width = "16"))]
pub const ALIGNED_64K_ALLOCATION: NonNull<[u8]> = {
const REF: &Aligned64kAllocation = &Aligned64kAllocation([0; _64K]);
let ptr: *const Aligned64kAllocation = REF;
let ptr: *const [u8] = ptr::slice_from_raw_parts(ptr.cast(), _64K);
#[allow(clippy::as_conversions)]
unsafe {
NonNull::new_unchecked(ptr as *mut _)
}
};
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
#[doc(hidden)] #[macro_export]
macro_rules! trailing_field_offset {
($ty:ty, $trailing_field_name:tt) => {{
let min_size = {
let zero_elems: *const [()] =
$crate::util::macro_util::core_reexport::ptr::slice_from_raw_parts(
$crate::util::macro_util::core_reexport::ptr::NonNull::<()>::dangling()
.as_ptr()
.cast_const(),
0,
);
unsafe {
#[allow(clippy::as_conversions)]
$crate::util::macro_util::core_reexport::mem::size_of_val_raw(
zero_elems as *const $ty,
)
}
};
assert!(min_size <= _64K);
#[allow(clippy::as_conversions)]
let ptr = ALIGNED_64K_ALLOCATION.as_ptr() as *const $ty;
let field = unsafe {
$crate::util::macro_util::core_reexport::ptr::addr_of!((*ptr).$trailing_field_name)
};
let offset = unsafe { field.cast::<u8>().offset_from(ptr.cast::<u8>()) };
assert!(offset >= 0);
Some(
#[allow(clippy::as_conversions)]
{
offset as usize
},
)
}};
}
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
#[doc(hidden)] #[macro_export]
macro_rules! align_of {
($ty:ty) => {{
#[repr(C)]
struct OffsetOfTrailingIsAlignment {
_byte: u8,
_trailing: $ty,
}
trailing_field_offset!(OffsetOfTrailingIsAlignment, _trailing)
}};
}
mod size_to_tag {
pub trait SizeToTag<const SIZE: usize> {
type Tag;
}
impl SizeToTag<1> for () {
type Tag = u8;
}
impl SizeToTag<2> for () {
type Tag = u16;
}
impl SizeToTag<4> for () {
type Tag = u32;
}
impl SizeToTag<8> for () {
type Tag = u64;
}
impl SizeToTag<16> for () {
type Tag = u128;
}
}
#[doc(hidden)]
pub type SizeToTag<const SIZE: usize> = <() as size_to_tag::SizeToTag<SIZE>>::Tag;
#[cfg(not(no_zerocopy_diagnostic_on_unimplemented_1_78_0))]
mod __size_of {
#[diagnostic::on_unimplemented(
message = "`{Self}` is unsized",
label = "`IntoBytes` needs all field types to be `Sized` in order to determine whether there is padding",
note = "consider using `#[repr(packed)]` to remove padding",
note = "`IntoBytes` does not require the fields of `#[repr(packed)]` types to be `Sized`"
)]
pub trait Sized: core::marker::Sized {}
impl<T: core::marker::Sized> Sized for T {}
#[inline(always)]
#[must_use]
#[allow(clippy::needless_maybe_sized)]
pub const fn size_of<T: Sized + ?core::marker::Sized>() -> usize {
core::mem::size_of::<T>()
}
}
#[cfg(no_zerocopy_diagnostic_on_unimplemented_1_78_0)]
pub use core::mem::size_of;
#[cfg(not(no_zerocopy_diagnostic_on_unimplemented_1_78_0))]
pub use __size_of::size_of;
#[doc(hidden)] #[macro_export]
macro_rules! struct_padding {
($t:ty, [$($ts:ty),*]) => {
$crate::util::macro_util::size_of::<$t>() - (0 $(+ $crate::util::macro_util::size_of::<$ts>())*)
};
}
#[doc(hidden)] #[macro_export]
macro_rules! repr_c_struct_has_padding {
($t:ty, [$($ts:tt),*]) => {{
let layout = $crate::DstLayout::for_repr_c_struct(
$crate::util::macro_util::core_reexport::option::Option::None,
$crate::util::macro_util::core_reexport::option::Option::None,
&[$($crate::repr_c_struct_has_padding!(@field $ts),)*]
);
layout.requires_static_padding() || layout.requires_dynamic_padding()
}};
(@field ([$t:ty])) => {
<[$t] as $crate::KnownLayout>::LAYOUT
};
(@field ($t:ty)) => {
$crate::DstLayout::for_unpadded_type::<$t>()
};
(@field [$t:ty]) => {
<[$t] as $crate::KnownLayout>::LAYOUT
};
(@field $t:ty) => {
$crate::DstLayout::for_unpadded_type::<$t>()
};
}
#[doc(hidden)] #[macro_export]
macro_rules! union_padding {
($t:ty, [$($ts:ty),*]) => {{
let mut max = 0;
$({
let padding = $crate::util::macro_util::size_of::<$t>() - $crate::util::macro_util::size_of::<$ts>();
if padding > max {
max = padding;
}
})*
max
}};
}
#[doc(hidden)] #[macro_export]
macro_rules! enum_padding {
($t:ty, $disc:ty, $([$($ts:ty),*]),*) => {{
let mut max = 0;
$({
let padding = $crate::util::macro_util::size_of::<$t>()
- (
$crate::util::macro_util::size_of::<$disc>()
$(+ $crate::util::macro_util::size_of::<$ts>())*
);
if padding > max {
max = padding;
}
})*
max
}};
}
#[doc(hidden)] #[macro_export]
macro_rules! assert_align_gt_eq {
($t:ident, $u: ident) => {{
if false {
let align_of: $crate::util::macro_util::AlignOf<_> = unreachable!();
$t = align_of.into_t();
let mut max_aligns = $crate::util::macro_util::MaxAlignsOf::new($t, $u);
max_aligns = unsafe {
#[allow(clippy::missing_transmute_annotations)]
$crate::util::macro_util::core_reexport::mem::transmute(align_of)
};
} else {
loop {}
}
}};
}
#[doc(hidden)] #[macro_export]
macro_rules! assert_size_eq {
($t:ident, $u: ident) => {{
if false {
$u = unsafe {
#[allow(clippy::useless_transmute, clippy::missing_transmute_annotations)]
$crate::util::macro_util::core_reexport::mem::transmute($t)
};
} else {
loop {}
}
}};
}
#[doc(hidden)]
#[inline]
fn try_cast_or_pme<Src, Dst, I, R, S>(
src: Ptr<'_, Src, I>,
) -> Result<
Ptr<'_, Dst, (I::Aliasing, invariant::Unaligned, invariant::Valid)>,
ValidityError<Ptr<'_, Src, I>, Dst>,
>
where
Src: invariant::Read<I::Aliasing, R>,
Dst: TryFromBytes
+ invariant::Read<I::Aliasing, R>
+ TryTransmuteFromPtr<Dst, I::Aliasing, invariant::Initialized, invariant::Valid, S>,
I: Invariants<Validity = invariant::Initialized>,
I::Aliasing: invariant::Reference,
{
static_assert!(Src, Dst => mem::size_of::<Dst>() == mem::size_of::<Src>());
#[allow(clippy::multiple_unsafe_ops_per_block)]
let c_ptr = unsafe { src.cast_unsized(|p| cast!(p)) };
match c_ptr.try_into_valid() {
Ok(ptr) => Ok(ptr),
Err(err) => {
let ptr = err.into_src();
#[allow(clippy::multiple_unsafe_ops_per_block)]
let ptr = unsafe { ptr.cast_unsized(|p| cast!(p)) };
let ptr = unsafe { ptr.assume_alignment::<I::Alignment>() };
let ptr = unsafe { ptr.assume_validity::<I::Validity>() };
Err(ValidityError::new(ptr.unify_invariants()))
}
}
}
#[inline(always)]
pub fn try_transmute<Src, Dst>(src: Src) -> Result<Dst, ValidityError<Src, Dst>>
where
Src: IntoBytes,
Dst: TryFromBytes,
{
static_assert!(Src, Dst => mem::size_of::<Dst>() == mem::size_of::<Src>());
let mu_src = mem::MaybeUninit::new(src);
let mu_src_copy = unsafe { core::ptr::read(&mu_src) };
let mut mu_dst: mem::MaybeUninit<Dst> =
unsafe { crate::util::transmute_unchecked(mu_src_copy) };
let ptr = Ptr::from_mut(&mut mu_dst);
let ptr = unsafe { ptr.assume_validity::<invariant::Initialized>() };
let ptr: Ptr<'_, Dst, _> = unsafe {
ptr.cast_unsized(|ptr: crate::pointer::PtrInner<'_, mem::MaybeUninit<Dst>>| {
ptr.cast_sized()
})
};
if Dst::is_bit_valid(ptr.forget_aligned()) {
Ok(unsafe { mu_dst.assume_init() })
} else {
Err(ValidityError::new(unsafe { mu_src.assume_init() }))
}
}
#[inline(always)]
pub fn try_transmute_ref<Src, Dst>(src: &Src) -> Result<&Dst, ValidityError<&Src, Dst>>
where
Src: IntoBytes + Immutable,
Dst: TryFromBytes + Immutable,
{
let ptr = Ptr::from_ref(src);
let ptr = ptr.bikeshed_recall_initialized_immutable();
match try_cast_or_pme::<Src, Dst, _, BecauseImmutable, _>(ptr) {
Ok(ptr) => {
static_assert!(Src, Dst => mem::align_of::<Dst>() <= mem::align_of::<Src>());
let ptr = unsafe { ptr.assume_alignment::<invariant::Aligned>() };
Ok(ptr.as_ref())
}
Err(err) => Err(err.map_src(|ptr| {
let ptr = unsafe { ptr.assume_valid() };
ptr.as_ref()
})),
}
}
#[inline(always)]
pub fn try_transmute_mut<Src, Dst>(src: &mut Src) -> Result<&mut Dst, ValidityError<&mut Src, Dst>>
where
Src: FromBytes + IntoBytes,
Dst: TryFromBytes + IntoBytes,
{
let ptr = Ptr::from_mut(src);
let ptr = ptr.bikeshed_recall_initialized_from_bytes();
match try_cast_or_pme::<Src, Dst, _, BecauseExclusive, _>(ptr) {
Ok(ptr) => {
static_assert!(Src, Dst => mem::align_of::<Dst>() <= mem::align_of::<Src>());
let ptr = unsafe { ptr.assume_alignment::<invariant::Aligned>() };
Ok(ptr.as_mut())
}
Err(err) => {
Err(err.map_src(|ptr| ptr.recall_validity::<_, (_, BecauseInvariantsEq)>().as_mut()))
}
}
}
#[derive(Copy, Clone)]
pub struct Wrap<Src, Dst>(pub Src, pub PhantomData<Dst>);
impl<Src, Dst> Wrap<Src, Dst> {
#[inline(always)]
pub const fn new(src: Src) -> Self {
Wrap(src, PhantomData)
}
}
impl<'a, Src, Dst> Wrap<&'a Src, &'a Dst> {
#[inline(always)]
#[must_use]
pub const unsafe fn transmute_ref(self) -> &'a Dst {
static_assert!(Src, Dst => mem::size_of::<Dst>() == mem::size_of::<Src>());
static_assert!(Src, Dst => mem::align_of::<Dst>() <= mem::align_of::<Src>());
let src: *const Src = self.0;
let dst = src.cast::<Dst>();
#[allow(clippy::transmute_ptr_to_ref)]
unsafe {
mem::transmute(dst)
}
}
}
impl<'a, Src, Dst> Wrap<&'a mut Src, &'a mut Dst> {
#[inline(always)]
#[must_use]
pub fn transmute_mut(self) -> &'a mut Dst
where
Src: FromBytes + IntoBytes,
Dst: FromBytes + IntoBytes,
{
static_assert!(Src, Dst => mem::size_of::<Dst>() == mem::size_of::<Src>());
static_assert!(Src, Dst => mem::align_of::<Dst>() <= mem::align_of::<Src>());
let src: *mut Src = self.0;
let dst = src.cast::<Dst>();
unsafe { &mut *dst }
}
}
pub trait TransmuteRefDst<'a> {
type Dst: ?Sized;
#[must_use]
fn transmute_ref(self) -> &'a Self::Dst;
}
impl<'a, Src: ?Sized, Dst: ?Sized> TransmuteRefDst<'a> for Wrap<&'a Src, &'a Dst>
where
Src: KnownLayout<PointerMetadata = usize> + IntoBytes + Immutable,
Dst: KnownLayout<PointerMetadata = usize> + FromBytes + Immutable,
{
type Dst = Dst;
#[inline(always)]
fn transmute_ref(self) -> &'a Dst {
static_assert!(Src: ?Sized + KnownLayout, Dst: ?Sized + KnownLayout => {
Src::LAYOUT.align.get() >= Dst::LAYOUT.align.get()
}, "cannot transmute reference when destination type has higher alignment than source type");
#[allow(clippy::multiple_unsafe_ops_per_block)]
unsafe {
unsafe_with_size_eq!(<S<Src>, D<Dst>> {
let ptr = Ptr::from_ref(self.0)
.transmute::<S<Src>, invariant::Valid, BecauseImmutable>()
.recall_validity::<invariant::Initialized, _>()
.transmute::<D<Dst>, invariant::Initialized, (crate::pointer::BecauseMutationCompatible, _)>()
.recall_validity::<invariant::Valid, _>();
#[allow(unused_unsafe)]
let ptr = unsafe { ptr.assume_alignment() };
&ptr.as_ref().0
})
}
}
}
pub trait TransmuteMutDst<'a> {
type Dst: ?Sized;
#[must_use]
fn transmute_mut(self) -> &'a mut Self::Dst;
}
impl<'a, Src: ?Sized, Dst: ?Sized> TransmuteMutDst<'a> for Wrap<&'a mut Src, &'a mut Dst>
where
Src: KnownLayout<PointerMetadata = usize> + FromBytes + IntoBytes,
Dst: KnownLayout<PointerMetadata = usize> + FromBytes + IntoBytes,
{
type Dst = Dst;
#[inline(always)]
fn transmute_mut(self) -> &'a mut Dst {
static_assert!(Src: ?Sized + KnownLayout, Dst: ?Sized + KnownLayout => {
Src::LAYOUT.align.get() >= Dst::LAYOUT.align.get()
}, "cannot transmute reference when destination type has higher alignment than source type");
#[allow(clippy::multiple_unsafe_ops_per_block)]
unsafe {
unsafe_with_size_eq!(<S<Src>, D<Dst>> {
let ptr = Ptr::from_mut(self.0)
.transmute::<S<Src>, invariant::Valid, _>()
.recall_validity::<invariant::Initialized, (_, (_, _))>()
.transmute::<D<Dst>, invariant::Initialized, _>()
.recall_validity::<invariant::Valid, (_, (_, _))>();
#[allow(unused_unsafe)]
let ptr = unsafe { ptr.assume_alignment() };
&mut ptr.as_mut().0
})
}
}
}
#[must_use]
#[inline(always)]
pub const fn must_use<T>(t: T) -> T {
t
}
pub mod core_reexport {
pub use core::*;
pub mod mem {
pub use core::mem::*;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::testutil::*;
#[test]
fn test_align_of() {
macro_rules! test {
($ty:ty) => {
assert_eq!(mem::size_of::<AlignOf<$ty>>(), mem::align_of::<$ty>());
};
}
test!(());
test!(u8);
test!(AU64);
test!([AU64; 2]);
}
#[test]
fn test_max_aligns_of() {
macro_rules! test {
($t:ty, $u:ty) => {
assert_eq!(
mem::size_of::<MaxAlignsOf<$t, $u>>(),
core::cmp::max(mem::align_of::<$t>(), mem::align_of::<$u>())
);
};
}
test!(u8, u8);
test!(u8, AU64);
test!(AU64, u8);
}
#[test]
fn test_typed_align_check() {
macro_rules! assert_t_align_gteq_u_align {
($t:ty, $u:ty, $gteq:expr) => {
assert_eq!(
mem::size_of::<MaxAlignsOf<$t, $u>>() == mem::size_of::<AlignOf<$t>>(),
$gteq
);
};
}
assert_t_align_gteq_u_align!(u8, u8, true);
assert_t_align_gteq_u_align!(AU64, AU64, true);
assert_t_align_gteq_u_align!(AU64, u8, true);
assert_t_align_gteq_u_align!(u8, AU64, false);
}
#[allow(clippy::decimal_literal_representation)]
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
#[test]
fn test_trailing_field_offset() {
assert_eq!(mem::align_of::<Aligned64kAllocation>(), _64K);
macro_rules! test {
(#[$cfg:meta] ($($ts:ty),* ; $trailing_field_ty:ty) => $expect:expr) => {{
#[$cfg]
struct Test($(#[allow(dead_code)] $ts,)* #[allow(dead_code)] $trailing_field_ty);
assert_eq!(test!(@offset $($ts),* ; $trailing_field_ty), $expect);
}};
(#[$cfg:meta] $(#[$cfgs:meta])* ($($ts:ty),* ; $trailing_field_ty:ty) => $expect:expr) => {
test!(#[$cfg] ($($ts),* ; $trailing_field_ty) => $expect);
test!($(#[$cfgs])* ($($ts),* ; $trailing_field_ty) => $expect);
};
(@offset ; $_trailing:ty) => { trailing_field_offset!(Test, 0) };
(@offset $_t:ty ; $_trailing:ty) => { trailing_field_offset!(Test, 1) };
}
test!(#[repr(C)] #[repr(transparent)] #[repr(packed)](; u8) => Some(0));
test!(#[repr(C)] #[repr(transparent)] #[repr(packed)](; [u8]) => Some(0));
test!(#[repr(C)] #[repr(C, packed)] (u8; u8) => Some(1));
test!(#[repr(C)] (; AU64) => Some(0));
test!(#[repr(C)] (; [AU64]) => Some(0));
test!(#[repr(C)] (u8; AU64) => Some(8));
test!(#[repr(C)] (u8; [AU64]) => Some(8));
#[derive(
Immutable, FromBytes, Eq, PartialEq, Ord, PartialOrd, Default, Debug, Copy, Clone,
)]
#[repr(C)]
pub(crate) struct Nested<T, U: ?Sized> {
_t: T,
_u: U,
}
test!(#[repr(C)] (; Nested<u8, AU64>) => Some(0));
test!(#[repr(C)] (; Nested<u8, [AU64]>) => Some(0));
test!(#[repr(C)] (u8; Nested<u8, AU64>) => Some(8));
test!(#[repr(C)] (u8; Nested<u8, [AU64]>) => Some(8));
test!(#[repr(C, packed( 1))] (u8; elain::Align< 2>) => Some( 1));
test!(#[repr(C, packed( 2))] (u8; elain::Align< 4>) => Some( 2));
test!(#[repr(C, packed( 4))] (u8; elain::Align< 8>) => Some( 4));
test!(#[repr(C, packed( 8))] (u8; elain::Align< 16>) => Some( 8));
test!(#[repr(C, packed( 16))] (u8; elain::Align< 32>) => Some( 16));
test!(#[repr(C, packed( 32))] (u8; elain::Align< 64>) => Some( 32));
test!(#[repr(C, packed( 64))] (u8; elain::Align< 128>) => Some( 64));
test!(#[repr(C, packed( 128))] (u8; elain::Align< 256>) => Some( 128));
test!(#[repr(C, packed( 256))] (u8; elain::Align< 512>) => Some( 256));
test!(#[repr(C, packed( 512))] (u8; elain::Align< 1024>) => Some( 512));
test!(#[repr(C, packed( 1024))] (u8; elain::Align< 2048>) => Some( 1024));
test!(#[repr(C, packed( 2048))] (u8; elain::Align< 4096>) => Some( 2048));
test!(#[repr(C, packed( 4096))] (u8; elain::Align< 8192>) => Some( 4096));
test!(#[repr(C, packed( 8192))] (u8; elain::Align< 16384>) => Some( 8192));
test!(#[repr(C, packed( 16384))] (u8; elain::Align< 32768>) => Some( 16384));
test!(#[repr(C, packed( 32768))] (u8; elain::Align< 65536>) => Some( 32768));
test!(#[repr(C, packed( 65536))] (u8; elain::Align< 131072>) => Some( 65536));
test!(#[repr(C, align( 1))] (u8; elain::Align< 2>) => Some( 2));
test!(#[repr(C, align( 2))] (u8; elain::Align< 4>) => Some( 4));
test!(#[repr(C, align( 4))] (u8; elain::Align< 8>) => Some( 8));
test!(#[repr(C, align( 8))] (u8; elain::Align< 16>) => Some( 16));
test!(#[repr(C, align( 16))] (u8; elain::Align< 32>) => Some( 32));
test!(#[repr(C, align( 32))] (u8; elain::Align< 64>) => Some( 64));
test!(#[repr(C, align( 64))] (u8; elain::Align< 128>) => Some( 128));
test!(#[repr(C, align( 128))] (u8; elain::Align< 256>) => Some( 256));
test!(#[repr(C, align( 256))] (u8; elain::Align< 512>) => Some( 512));
test!(#[repr(C, align( 512))] (u8; elain::Align< 1024>) => Some( 1024));
test!(#[repr(C, align( 1024))] (u8; elain::Align< 2048>) => Some( 2048));
test!(#[repr(C, align( 2048))] (u8; elain::Align< 4096>) => Some( 4096));
test!(#[repr(C, align( 4096))] (u8; elain::Align< 8192>) => Some( 8192));
test!(#[repr(C, align( 8192))] (u8; elain::Align< 16384>) => Some( 16384));
test!(#[repr(C, align( 16384))] (u8; elain::Align< 32768>) => Some( 32768));
test!(#[repr(C, align( 32768))] (u8; elain::Align< 65536>) => Some( 65536));
}
#[allow(clippy::decimal_literal_representation)]
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
#[test]
fn test_align_of_dst() {
assert_eq!(align_of!([elain::Align<1>]), Some(1));
assert_eq!(align_of!([elain::Align<2>]), Some(2));
assert_eq!(align_of!([elain::Align<4>]), Some(4));
assert_eq!(align_of!([elain::Align<8>]), Some(8));
assert_eq!(align_of!([elain::Align<16>]), Some(16));
assert_eq!(align_of!([elain::Align<32>]), Some(32));
assert_eq!(align_of!([elain::Align<64>]), Some(64));
assert_eq!(align_of!([elain::Align<128>]), Some(128));
assert_eq!(align_of!([elain::Align<256>]), Some(256));
assert_eq!(align_of!([elain::Align<512>]), Some(512));
assert_eq!(align_of!([elain::Align<1024>]), Some(1024));
assert_eq!(align_of!([elain::Align<2048>]), Some(2048));
assert_eq!(align_of!([elain::Align<4096>]), Some(4096));
assert_eq!(align_of!([elain::Align<8192>]), Some(8192));
assert_eq!(align_of!([elain::Align<16384>]), Some(16384));
assert_eq!(align_of!([elain::Align<32768>]), Some(32768));
assert_eq!(align_of!([elain::Align<65536>]), Some(65536));
}
#[test]
fn test_enum_casts() {
#[repr(i8)]
enum ReprI8 {
MinusOne = -1,
Zero = 0,
Min = i8::MIN,
Max = i8::MAX,
}
#[allow(clippy::as_conversions)]
let x = ReprI8::MinusOne as u8;
assert_eq!(x, u8::MAX);
#[allow(clippy::as_conversions)]
let x = ReprI8::Zero as u8;
assert_eq!(x, 0);
#[allow(clippy::as_conversions)]
let x = ReprI8::Min as u8;
assert_eq!(x, 128);
#[allow(clippy::as_conversions)]
let x = ReprI8::Max as u8;
assert_eq!(x, 127);
}
#[test]
fn test_struct_padding() {
macro_rules! test {
(#[$cfg:meta] ($($ts:ty),*) => $expect:expr) => {{
#[$cfg]
#[allow(dead_code)]
struct Test($($ts),*);
assert_eq!(struct_padding!(Test, [$($ts),*]), $expect);
}};
(#[$cfg:meta] $(#[$cfgs:meta])* ($($ts:ty),*) => $expect:expr) => {
test!(#[$cfg] ($($ts),*) => $expect);
test!($(#[$cfgs])* ($($ts),*) => $expect);
};
}
test!(#[repr(C)] #[repr(transparent)] #[repr(packed)] () => 0);
test!(#[repr(C)] #[repr(transparent)] #[repr(packed)] (u8) => 0);
test!(#[repr(C)] #[repr(transparent)] #[repr(packed)] (u8, ()) => 0);
test!(#[repr(C)] #[repr(packed)] (u8, u8) => 0);
test!(#[repr(C)] (u8, AU64) => 7);
test!(#[repr(packed)] (u8, u64) => 0);
}
#[test]
fn test_repr_c_struct_padding() {
macro_rules! test {
(($($ts:tt),*) => $expect:expr) => {{
#[repr(C)]
#[allow(dead_code)]
struct Test($($ts),*);
assert_eq!(repr_c_struct_has_padding!(Test, [$($ts),*]), $expect);
}};
}
test!(() => false);
test!(([u8]) => false);
test!((u8) => false);
test!((u8, [u8]) => false);
test!((u8, ()) => false);
test!((u8, (), [u8]) => false);
test!((u8, u8) => false);
test!((u8, u8, [u8]) => false);
test!((u8, AU64) => true);
test!((u8, AU64, [u8]) => true);
test!((AU64, [AU64]) => false);
test!((u8, [AU64]) => true);
#[repr(align(4))]
struct AU32(#[allow(unused)] u32);
test!((AU64, [AU64]) => false);
test!((AU64, [AU32]) => true);
}
#[test]
fn test_union_padding() {
macro_rules! test {
(#[$cfg:meta] {$($fs:ident: $ts:ty),*} => $expect:expr) => {{
#[$cfg]
#[allow(unused)] union Test{ $($fs: $ts),* }
assert_eq!(union_padding!(Test, [$($ts),*]), $expect);
}};
(#[$cfg:meta] $(#[$cfgs:meta])* {$($fs:ident: $ts:ty),*} => $expect:expr) => {
test!(#[$cfg] {$($fs: $ts),*} => $expect);
test!($(#[$cfgs])* {$($fs: $ts),*} => $expect);
};
}
test!(#[repr(C)] #[repr(packed)] {a: u8} => 0);
test!(#[repr(C)] #[repr(packed)] {a: u8, b: u8} => 0);
test!(#[repr(C)] #[repr(packed)] {a: u8, b: u64} => 7);
}
#[test]
fn test_enum_padding() {
macro_rules! test {
(#[repr($disc:ident $(, $c:ident)?)] { $($vs:ident ($($ts:ty),*),)* } => $expect:expr) => {
test!(@case #[repr($disc $(, $c)?)] { $($vs ($($ts),*),)* } => $expect);
};
(#[repr($disc:ident $(, $c:ident)?)] #[$cfg:meta] $(#[$cfgs:meta])* { $($vs:ident ($($ts:ty),*),)* } => $expect:expr) => {
test!(@case #[repr($disc $(, $c)?)] #[$cfg] { $($vs ($($ts),*),)* } => $expect);
test!(#[repr($disc $(, $c)?)] $(#[$cfgs])* { $($vs ($($ts),*),)* } => $expect);
};
(@case #[repr($disc:ident $(, $c:ident)?)] $(#[$cfg:meta])? { $($vs:ident ($($ts:ty),*),)* } => $expect:expr) => {{
#[repr($disc $(, $c)?)]
$(#[$cfg])?
#[allow(unused)] enum Test {
$($vs ($($ts),*),)*
}
assert_eq!(
enum_padding!(Test, $disc, $([$($ts),*]),*),
$expect
);
}};
}
#[allow(unused)]
#[repr(align(2))]
struct U16(u16);
#[allow(unused)]
#[repr(align(4))]
struct U32(u32);
test!(#[repr(u8)] #[repr(C)] {
A(u8),
} => 0);
test!(#[repr(u16)] #[repr(C)] {
A(u8, u8),
B(U16),
} => 0);
test!(#[repr(u32)] #[repr(C)] {
A(u8, u8, u8, u8),
B(U16, u8, u8),
C(u8, u8, U16),
D(U16, U16),
E(U32),
} => 0);
test!(#[repr(u8)] {
A(u8, U16),
} => 0);
test!(#[repr(u8)] {
A(u8, U16, U32),
} => 0);
test!(#[repr(u8, C)] {
A(u8, U16),
} => 2);
test!(#[repr(u8, C)] {
A(u8, u8, u8, U32),
} => 4);
test!(#[repr(u8)] #[repr(C)] {
A(U16, u8),
} => 2);
test!(#[repr(u8)] #[repr(C)] {
A(U32, u8, u8, u8),
} => 4);
}
}