Macro plain_data
macro_rules! plain_data {
(
@munch [$($acc:tt)*]
#[repr(C)]
$(#[$meta:meta])*
$vis:vis struct $name:ident {
$(
$(#[$field_meta:meta])*
$field_vis:vis $field:ident : $field_ty:ty
),* $(,)?
}
) => { ... };
(
@munch [$($acc:tt)*]
#[repr(C)]
$(#[$meta:meta])*
$vis:vis struct $name:ident (
$(
$(#[$field_meta:meta])*
$field_vis:vis $field_ty:ty
),+ $(,)?
) $(;)?
) => { ... };
(
@munch [$($acc:tt)*]
#[repr(transparent)]
$(#[$meta:meta])*
$vis:vis struct $name:ident (
$(#[$field_meta:meta])*
$field_vis:vis $field_ty:ty
) $(;)?
) => { ... };
(
@munch [$($acc:tt)*]
#[$attr:meta] $($rest:tt)*
) => { ... };
(
@munch [$($acc:tt)*]
$($rest:tt)*
) => { ... };
($($tokens:tt)*) => { ... };
}Expand description
Declares a plain-data structure and implements PlainData
for it.
The macro emits the structure itself, so the checked field list cannot drift
from the declaration. Every field type must implement
PlainData, and the structure must have no padding
bytes; both are checked at compile time.
Two forms are accepted:
- a named-field structure, which must be
#[repr(C)]so that the guest and the host agree on the layout; - a tuple structure, which must be
#[repr(C)], or#[repr(transparent)]when it has a single field.
Other attributes (doc comments, #[derive(..)], …) may appear before or
after the representation attribute. #[repr(packed)] on its own matches no
form; combined with the #[repr(C)] the macro adds, rustc accepts the pair,
so the macro rejects packing with a compile-time alignment check instead (a
packed structure would expose misaligned field references).
§Safety contract
The structure must be #[repr(C)] or #[repr(transparent)] as required by
each form, and every field type must be plain data. Padding bytes of a
#[repr(C)] structure are never read as values: derived trait
implementations compare or format fields, not padding. Because the
padding check requires size_of::<Self>() to equal the sum of the field
sizes, a plain_data! structure never has padding, so writing a payload
never copies uninitialised memory across the host/guest boundary.
§Examples
use shared::plain_data;
plain_data! {
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Example {
pub version: u32,
pub enabled: u8,
pub reserved: [u8; 3],
}
}
// Other attributes may also come first.
plain_data! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct Pair {
pub first: u16,
pub second: u16,
}
}
plain_data! {
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TuplePair(pub u16, pub u16);
}
plain_data! {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Wrapper(pub u8);
}A field that is not plain data does not compile:
use shared::plain_data;
plain_data! {
#[repr(C)]
pub struct Bad {
pub flag: bool,
}
}A structure with padding does not compile either:
use shared::plain_data;
plain_data! {
#[repr(C)]
pub struct Padded {
pub a: u8,
pub b: u32,
}
}A packed structure does not compile (rustc accepts #[repr(C)] combined
with #[repr(packed)], so the macro rejects it with an alignment check):
use shared::plain_data;
plain_data! {
#[repr(C)]
#[repr(packed)]
pub struct Packed {
pub a: u8,
pub b: u32,
}
}