pub unsafe trait ZeroCopy: StaticSize {
const ALIGN: usize;
// Provided methods
fn write_repr(&self, dst: &mut [u8])
where Self: Sized { ... }
fn is_valid_repr(bytes: &[u8]) -> bool { ... }
}zero-copy only.Expand description
A marker trait indicating that a type has a fixed, predictable layout (e.g., #[repr(C)])
and contains no padding bytes or invalid bit patterns allowing safe zero-copy casting.
§Safety
The type must have a fixed, predictable layout (e.g., #[repr(C)]) and contain no padding bytes or invalid bit patterns allowing safe zero-copy casting.
Required Associated Constants§
Provided Methods§
Sourcefn write_repr(&self, dst: &mut [u8])where
Self: Sized,
fn write_repr(&self, dst: &mut [u8])where
Self: Sized,
Writes the object representation of self into dst (exactly Self::SIZE bytes,
already zero-initialized by the caller).
The default implementation copies the whole representation, which is only sound for types
without padding. Types that may contain padding (such as #[derive(ZeroCopy)] types) must
override this to copy field by field so that padding bytes are never read.
Sourcefn is_valid_repr(bytes: &[u8]) -> bool
fn is_valid_repr(bytes: &[u8]) -> bool
Returns true if bytes (exactly Self::SIZE bytes, in native representation) form a
valid value of Self. Types with invalid bit patterns (e.g. bool, char) must override
this; it is checked before any reference to Self is created from untrusted bytes.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".