Skip to main content

Flat

Trait Flat 

Source
pub unsafe trait Flat: Sized {
    // Required methods
    unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos);
    fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>;

    // Provided method
    fn validate_option(addr: usize, buf: &[u8]) -> Result<(), ValidateError> { ... }
}
Expand description

Marker trait for types that can be stored in a Region.

§Safety

Implementors must satisfy all four invariants:

  1. No Drop: The type must not implement Drop. The derive macro enforces this with const { assert!(!needs_drop::<Self>()) }.
  2. All fields are Flat: Every field type must itself implement Flat. This is enforced by where-clause bounds in the generated impl.
  3. No heap pointers: Indirection is expressed exclusively through Near<T> and NearList<T>, which store self-relative i32 offsets. Raw pointers, Box, Vec, String, Rc, etc. are forbidden.
  4. Correct deep_copy: Must write all transitively reachable data into the target buffer at the correct offsets, patching self-relative pointers.

§Derive macro

#[derive(Flat)] generates all of this automatically. For enums, a #[repr(u8)] or #[repr(C, u8)] attribute is required (data variants need #[repr(C, u8)]) so the discriminant byte is at a known position.

§Blanket impl

A blanket Emit<T> for &T impl exists, so T: Flat alone implies &T: Emit<T> — enabling deep-copy via Region::trim and re_splice_list without explicit builder types.

Required Methods§

Source

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Deep-copy self into the buffer at position at, patching all self-relative pointers so they remain valid in the new location.

§Safety

at must be a position previously allocated for Self in the same buffer.

Source

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Validate that buf[addr..] contains a valid representation of Self.

Checks bounds, alignment, and type-specific invariants (e.g. enum discriminants, Near<T> non-null offsets, NearList<T> consistency).

For types with Near<T> or NearList<T> fields, the derive macro generates code that recursively validates targets.

§Errors

Returns ValidateError if the bytes at addr do not form a valid Self.

Provided Methods§

Source

fn validate_option(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Validate an Option<Self> at addr in buf.

The default implementation just checks bounds and alignment for Option<Self>. Types with niche layouts (e.g. Near<T> using NonZero<i32>) override this to inspect the discriminant and validate the inner value when present.

§Errors

Returns ValidateError if the bytes at addr do not form a valid Option<Self>.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Flat for Infallible

Source§

unsafe fn deep_copy(&self, _p: &mut impl Patch, _at: Pos)

Source§

fn validate(_addr: usize, _buf: &[u8]) -> Result<(), ValidateError>

Source§

impl Flat for bool

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Source§

impl Flat for i32

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Source§

impl Flat for i64

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Source§

impl Flat for u8

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Source§

impl Flat for u16

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Source§

impl Flat for u32

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Source§

impl Flat for u64

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Source§

impl<A: Flat, B: Flat> Flat for (A, B)

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Source§

impl<T: Flat> Flat for Option<T>

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Source§

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

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Implementors§

Source§

impl<T: Flat> Flat for Near<T>

Source§

impl<T: Flat> Flat for NearList<T>