Derive Macro bytemuck::TransparentWrapper
source · #[derive(TransparentWrapper)]
{
// Attributes available to this derive:
#[transparent]
}
Expand description
Derive the TransparentWrapper
trait for a struct
The macro ensures that the struct follows all the the safety requirements
for the TransparentWrapper
trait.
The following constraints need to be satisfied for the macro to succeed
- The struct must be
#[repr(transparent)]
- The struct must contain the
Wrapped
type - Any ZST fields must be
Zeroable
.
If the struct only contains a single field, the Wrapped
type will
automatically be determined. If there is more then one field in the struct,
you need to specify the Wrapped
type using #[transparent(T)]
Examples
#[derive(Copy, Clone, TransparentWrapper)]
#[repr(transparent)]
#[transparent(u16)]
struct Test<T> {
inner: u16,
extra: PhantomData<T>,
}
If the struct contains more than one field, the Wrapped
type must be
explicitly specified.
ⓘ
#[derive(Copy, Clone, TransparentWrapper)]
#[repr(transparent)]
// missing `#[transparent(u16)]`
struct Test<T> {
inner: u16,
extra: PhantomData<T>,
}
Any ZST fields must be Zeroable
.
ⓘ
struct NonTransparentSafeZST;
#[derive(TransparentWrapper)]
#[repr(transparent)]
#[transparent(u16)]
struct Test<T> {
inner: u16,
extra: PhantomData<T>,
another_extra: NonTransparentSafeZST, // not `Zeroable`
}