Skip to main content

ZeroCopy

Trait ZeroCopy 

Source
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 { ... }
}
Available on crate feature 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§

Source

const ALIGN: usize

The required alignment for this type.

Provided Methods§

Source

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.

Source

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".

Implementations on Foreign Types§

Source§

impl ZeroCopy for bool

Source§

impl ZeroCopy for char

Source§

impl ZeroCopy for f32

Source§

impl ZeroCopy for f64

Source§

impl ZeroCopy for i8

Source§

impl ZeroCopy for i16

Source§

impl ZeroCopy for i32

Source§

impl ZeroCopy for i64

Source§

impl ZeroCopy for i128

Source§

impl ZeroCopy for isize

Source§

impl ZeroCopy for u8

Source§

impl ZeroCopy for u16

Source§

impl ZeroCopy for u32

Source§

impl ZeroCopy for u64

Source§

impl ZeroCopy for u128

Source§

impl ZeroCopy for usize

Source§

impl<T: ZeroCopy, const N: usize> ZeroCopy for [T; N]

Source§

const ALIGN: usize = T::ALIGN

Source§

fn write_repr(&self, dst: &mut [u8])

Source§

fn is_valid_repr(bytes: &[u8]) -> bool

Implementors§

Source§

impl<E: Endian> ZeroCopy for ZeroStr<E>

Source§

impl<T, const ALIGN: usize, E: Endian> ZeroCopy for RelativePtr<T, ALIGN, E>

Source§

impl<T: ZeroCopy, const ALIGN: usize, E: Endian> ZeroCopy for ZeroSlice<T, ALIGN, E>

Source§

impl<T: ZeroCopy, const N: usize, const ALIGN: usize, E: Endian> ZeroCopy for ZeroArray<T, N, ALIGN, E>

Source§

impl<const CAP: usize, E: Endian> ZeroCopy for ZeroString<CAP, E>