Skip to main content

FlatStruct

Trait FlatStruct 

Source
pub unsafe trait FlatStruct:
    Copy
    + 'static
    + Send
    + Sync {
    const TYPE_HASH: [u8; 16];
    const WIRE_SIZE: usize = _;

    // Provided methods
    fn as_bytes(&self) -> &[u8]  { ... }
    unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self { ... }
}
Expand description

Marker trait for FlatData-capable types.

Spec quote (zerodds-flatdata-1.0 §1.1):

Guarantees:

  • Self: Copy (no Drop glue, plain bytes)
  • Self: 'static (no lifetime reference)
  • #[repr(C)] with a fixed, defined alignment
  • as_bytes() and from_bytes_unchecked() are safe by layout

§Safety

The implementer MUST ensure:

  • Self is #[repr(C)] (or #[repr(transparent)] over a single repr(C) type).
  • All fields are FlatStruct or primitive types without padding-sensitive UB (no #[repr(packed)] with pointer-aligned fields).
  • Self: Copy (the trait bound enforces this).
  • TYPE_HASH is unique for the exact wire-layout variant; on every schema change the hash MUST be regenerated.

Required Associated Constants§

Source

const TYPE_HASH: [u8; 16]

Unique type-hash (16 byte). Caller code generates it via SHA-256(type_name + field_layout_string) and takes the first 16 byte. The reader checks this hash against the discovery hash; on mismatch → slot drop.

Provided Associated Constants§

Source

const WIRE_SIZE: usize = _

Wire size of the repr(C) struct (= core::mem::size_of::<Self>()).

Provided Methods§

Source

fn as_bytes(&self) -> &[u8]

Returns the slot layout as a slice. Safe by layout — Self: Copy

  • repr(C) guarantee that the byte cast is defined.
Source

unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self

Reconstructs from a raw slice. The caller MUST:

  • bytes.len() >= WIRE_SIZE
  • bytes provenance valid for WIRE_SIZE
  • type-hash verified beforehand (otherwise UB on alignment mismatch)
§Safety

See above.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§