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:
- No
Drop: The type must not implementDrop. The derive macro enforces this withconst { assert!(!needs_drop::<Self>()) }. - All fields are
Flat: Every field type must itself implementFlat. This is enforced by where-clause bounds in the generated impl. - No heap pointers: Indirection is expressed exclusively through
Near<T>andNearList<T>, which store self-relativei32offsets. Raw pointers,Box,Vec,String,Rc, etc. are forbidden. - 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§
Sourceunsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)
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.
Sourcefn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>
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§
Sourcefn validate_option(addr: usize, buf: &[u8]) -> Result<(), ValidateError>
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.