#![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, num::Wrapping};
use crate::{
pointer::{
cast::CastSized,
invariant::{Aligned, Initialized, Valid},
BecauseImmutable,
},
FromBytes, Immutable, IntoBytes, KnownLayout, Ptr, ReadOnly, 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 () {}
#[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, $_align:expr, $_packed:expr, [$($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, $align:expr, $packed:expr, [$($ts:tt),*]) => {{
let layout = $crate::DstLayout::for_repr_c_struct(
$align,
$packed,
&[$($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, $_align:expr, $_packed:expr, [$($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, $_align:expr, $packed:expr, $disc:ty, $([$($ts:ty),*]),*) => {{
#[allow(clippy::as_conversions)]
const _: [(); 1] = [(); $packed.is_none() as usize];
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! into_inner {
($e:expr) => {
match $e {
$crate::util::macro_util::core_reexport::result::Result::Ok(e) => e,
$crate::util::macro_util::core_reexport::result::Result::Err(i) => match i {},
}
};
}
#[doc(hidden)] #[macro_export]
macro_rules! ident_id {
($field:ident) => {
$crate::util::macro_util::hash_name(stringify!($field))
};
($field:literal) => {
$field
};
}
#[inline(always)]
#[must_use]
#[allow(clippy::as_conversions, clippy::indexing_slicing, clippy::arithmetic_side_effects)]
pub const fn hash_name(name: &str) -> i128 {
let name = name.as_bytes();
if name.len() <= size_of::<u128>() {
let mut bytes = [0u8; 16];
let mut i = 0;
while i < name.len() {
bytes[i] = name[i];
i += 1;
}
return i128::from_ne_bytes(bytes);
};
let mut hash = 0u128;
let mut i = 0;
while i < name.len() {
const K: u128 = 0x517cc1b727220a95517cc1b727220a95;
hash = (hash.rotate_left(5) ^ (name[i] as u128)).wrapping_mul(K);
i += 1;
}
i128::from_ne_bytes(hash.to_ne_bytes())
}
#[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_dst: mem::MaybeUninit<ReadOnly<Dst>> =
unsafe { crate::util::transmute_unchecked(mu_src) };
let ptr = Ptr::from_ref(&mu_dst);
let ptr = unsafe { ptr.assume_validity::<Initialized>() };
if Dst::is_bit_valid(ptr.cast::<_, CastSized, _>()) {
Ok(ReadOnly::into_inner(unsafe { mu_dst.assume_init() }))
} else {
let mu_src: mem::MaybeUninit<Src> = unsafe { crate::util::transmute_unchecked(mu_dst) };
Err(ValidityError::new(unsafe { mu_src.assume_init() }))
}
}
pub trait TryTransmuteRefDst<'a> {
type Dst: ?Sized;
fn try_transmute_ref(self) -> Result<&'a Self::Dst, ValidityError<&'a Self::Src, Self::Dst>>
where
Self: TryTransmuteRefSrc<'a>,
Self::Src: IntoBytes + Immutable + KnownLayout,
Self::Dst: TryFromBytes + Immutable + KnownLayout;
}
pub trait TryTransmuteRefSrc<'a> {
type Src: ?Sized;
}
impl<'a, Src, Dst> TryTransmuteRefSrc<'a> for Wrap<&'a Src, &'a Dst>
where
Src: ?Sized,
Dst: ?Sized,
{
type Src = Src;
}
impl<'a, Src, Dst> TryTransmuteRefDst<'a> for Wrap<&'a Src, &'a Dst>
where
Src: IntoBytes + Immutable + KnownLayout + ?Sized,
Dst: TryFromBytes + Immutable + KnownLayout + ?Sized,
{
type Dst = Dst;
#[inline(always)]
fn try_transmute_ref(
self,
) -> Result<
&'a Dst,
ValidityError<&'a <Wrap<&'a Src, &'a Dst> as TryTransmuteRefSrc<'a>>::Src, Dst>,
> {
let ptr = Ptr::from_ref(self.0);
#[rustfmt::skip]
let res = ptr.try_with(#[inline(always)] |ptr| {
let ptr = ptr.recall_validity::<Initialized, _>();
let ptr = ptr.cast::<_, crate::layout::CastFrom<Dst>, _>();
ptr.try_into_valid()
});
match res {
Ok(ptr) => {
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");
let ptr = unsafe { ptr.assume_alignment::<Aligned>() };
Ok(ptr.as_ref())
}
Err(err) => Err(err.map_src(Ptr::as_ref)),
}
}
}
pub trait TryTransmuteMutDst<'a> {
type Dst: ?Sized;
fn try_transmute_mut(
self,
) -> Result<&'a mut Self::Dst, ValidityError<&'a mut Self::Src, Self::Dst>>
where
Self: TryTransmuteMutSrc<'a>,
Self::Src: IntoBytes,
Self::Dst: TryFromBytes;
}
pub trait TryTransmuteMutSrc<'a> {
type Src: ?Sized;
}
impl<'a, Src, Dst> TryTransmuteMutSrc<'a> for Wrap<&'a mut Src, &'a mut Dst>
where
Src: ?Sized,
Dst: ?Sized,
{
type Src = Src;
}
impl<'a, Src, Dst> TryTransmuteMutDst<'a> for Wrap<&'a mut Src, &'a mut Dst>
where
Src: FromBytes + IntoBytes + KnownLayout + ?Sized,
Dst: TryFromBytes + IntoBytes + KnownLayout + ?Sized,
{
type Dst = Dst;
#[inline(always)]
fn try_transmute_mut(
self,
) -> Result<
&'a mut Dst,
ValidityError<&'a mut <Wrap<&'a mut Src, &'a mut Dst> as TryTransmuteMutSrc<'a>>::Src, Dst>,
> {
let ptr = Ptr::from_mut(self.0);
#[rustfmt::skip]
let res = unsafe {
ptr.try_with_unchecked(#[inline(always)] |ptr| {
let ptr = ptr.recall_validity::<Initialized, (_, (_, _))>();
let ptr = ptr.cast::<_, crate::layout::CastFrom<Dst>, _>();
ptr.try_into_valid()
})
};
match res {
Ok(ptr) => {
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");
let ptr = unsafe { ptr.assume_alignment::<Aligned>() };
Ok(ptr.as_mut())
}
Err(err) => Err(err.map_src(Ptr::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>
where
Src: ?Sized,
Dst: ?Sized,
{
#[allow(clippy::must_use_candidate, clippy::missing_inline_in_public_items, clippy::empty_loop)]
pub const fn transmute_ref_inference_helper(self) -> &'a Dst {
loop {}
}
}
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)
}
}
#[inline(always)]
pub fn try_transmute_ref(self) -> Result<&'a Dst, ValidityError<&'a Src, Dst>>
where
Src: IntoBytes + Immutable,
Dst: TryFromBytes + Immutable,
{
static_assert!(Src => mem::align_of::<Src>() == mem::align_of::<Wrapping<Src>>());
static_assert!(Dst => mem::align_of::<Dst>() == mem::align_of::<Wrapping<Dst>>());
let src: &Wrapping<Src> =
unsafe { crate::util::transmute_ref::<_, _, BecauseImmutable>(self.0) };
let src = Wrap::new(src);
<Wrap<&'a Wrapping<Src>, &'a Wrapping<Dst>> as TryTransmuteRefDst<'a>>::try_transmute_ref(
src,
)
.map(
#[inline(always)]
|dst| unsafe { crate::util::transmute_ref::<_, _, BecauseImmutable>(dst) },
)
.map_err(
#[inline(always)]
|err| {
ValidityError::new(unsafe {
crate::util::transmute_ref::<_, _, BecauseImmutable>(err.into_src())
})
},
)
}
}
impl<'a, Src, Dst> Wrap<&'a mut Src, &'a mut Dst>
where
Src: ?Sized,
Dst: ?Sized,
{
#[allow(clippy::must_use_candidate, clippy::missing_inline_in_public_items, clippy::empty_loop)]
pub fn transmute_mut_inference_helper(self) -> &'a mut Dst {
loop {}
}
}
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 }
}
#[inline(always)]
pub fn try_transmute_mut(self) -> Result<&'a mut Dst, ValidityError<&'a mut Src, Dst>>
where
Src: FromBytes + IntoBytes,
Dst: TryFromBytes + IntoBytes,
{
static_assert!(Src => mem::align_of::<Src>() == mem::align_of::<Wrapping<Src>>());
static_assert!(Dst => mem::align_of::<Dst>() == mem::align_of::<Wrapping<Dst>>());
let src: &mut Wrapping<Src> =
unsafe { crate::util::transmute_mut::<_, _, (_, (_, _))>(self.0) };
let src = Wrap::new(src);
<Wrap<&'a mut Wrapping<Src>, &'a mut Wrapping<Dst>> as TryTransmuteMutDst<'a>>
::try_transmute_mut(src)
.map(|dst| unsafe { crate::util::transmute_mut::<_, _, (_, (_, _))>(dst) })
.map_err(|err| {
ValidityError::new(unsafe {
crate::util::transmute_mut::<_, _, (_, (_, _))>(err.into_src())
})
})
}
}
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 + IntoBytes + Immutable,
Dst: KnownLayout<PointerMetadata = usize> + FromBytes + Immutable,
{
type Dst = Dst;
#[inline(always)]
fn transmute_ref(self) -> &'a Dst {
let ptr = Ptr::from_ref(self.0)
.recall_validity::<Initialized, _>()
.transmute_with::<Dst, Initialized, crate::layout::CastFrom<Dst>, (crate::pointer::BecauseMutationCompatible, _)>()
.recall_validity::<Valid, _>();
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");
let ptr = unsafe { ptr.assume_alignment() };
ptr.as_ref()
}
}
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 + FromBytes + IntoBytes,
Dst: KnownLayout<PointerMetadata = usize> + FromBytes + IntoBytes,
{
type Dst = Dst;
#[inline(always)]
fn transmute_mut(self) -> &'a mut Dst {
let ptr = Ptr::from_mut(self.0)
.recall_validity::<Initialized, (_, (_, _))>()
.transmute_with::<Dst, Initialized, crate::layout::CastFrom<Dst>, _>()
.recall_validity::<Valid, (_, (_, _))>();
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");
let ptr = unsafe { ptr.assume_alignment() };
ptr.as_mut()
}
}
#[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 core::num::NonZeroUsize;
use crate::util::testutil::*;
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
mod nightly {
use super::super::*;
use crate::util::testutil::*;
#[allow(clippy::decimal_literal_representation)]
#[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)]
#[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, None::<NonZeroUsize>, None::<NonZeroUsize>, [$($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, None::<NonZeroUsize>, None::<NonZeroUsize>, [$($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, None::<NonZeroUsize>, None::<usize>, [$($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, None::<NonZeroUsize>, None::<NonZeroUsize>, $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);
}
}