Skip to main content

LayoutContract

Trait LayoutContract 

Source
pub trait LayoutContract:
    Sized
    + Copy
    + FieldMap {
    const DISC: u8;
    const VERSION: u8;
    const LAYOUT_ID: [u8; 8];
    const SIZE: usize;
    const TYPE_OFFSET: usize = HopperHeader::SIZE;
    const RESERVED_BYTES: usize = 0;
    const EXTENSION_OFFSET: Option<usize> = None;

    // Provided methods
    fn validate_header(data: &[u8]) -> ProgramResult { ... }
    fn projected_len() -> usize { ... }
    fn required_len() -> usize { ... }
    fn validate(data: &[u8]) -> bool { ... }
    fn check_disc(data: &[u8]) -> ProgramResult { ... }
    fn check_version(data: &[u8]) -> ProgramResult { ... }
    fn compatible(version: u8) -> bool { ... }
    fn has_extension_region(data: &[u8]) -> bool { ... }
    fn layout_info_static() -> LayoutInfo { ... }
    fn fields() -> &'static [FieldInfo] { ... }
}
Expand description

A compile-time layout contract binding type identity to wire format.

Implementors declare their discriminator, version, layout fingerprint, and wire size. The runtime uses these to validate accounts before granting typed access via overlay or load.

§Wire format (Hopper account header)

byte 0   : discriminator (u8)
byte 1   : version (u8)
bytes 2-3: flags (u16 LE)
bytes 4-11: layout_id (first 8 bytes of SHA-256 fingerprint)
bytes 12-15: reserved

§Example

impl LayoutContract for Vault {
    const DISC: u8 = 1;
    const VERSION: u8 = 1;
    const LAYOUT_ID: [u8; 8] = compute_layout_id("Vault", 1, "authority:[u8;32]:32,balance:LeU64:8,");
    const SIZE: usize = 16 + 32 + 8; // header + fields
}

Required Associated Constants§

Source

const DISC: u8

Account type discriminator (byte 0 of data).

Source

const VERSION: u8

Schema version for this layout (byte 1 of data).

Source

const LAYOUT_ID: [u8; 8]

First 8 bytes of the deterministic layout fingerprint. Computed from SHA-256("hopper:v1:" + name + ":" + version + ":" + field_spec).

Source

const SIZE: usize

Total wire size in bytes (including the 16-byte header).

Provided Associated Constants§

Source

const TYPE_OFFSET: usize = HopperHeader::SIZE

Byte offset where the typed projection begins.

Body-only runtime layouts keep the default HopperHeader::SIZE, while header-inclusive layouts set this to 0 so AccountView::load() projects the full account struct.

Source

const RESERVED_BYTES: usize = 0

Number of reserved bytes at the end of the layout. Reserved bytes provide forward-compatible padding that future versions can claim without a realloc.

Source

const EXTENSION_OFFSET: Option<usize> = None

Byte offset where an extension region begins, if the layout supports one. Extension regions allow appending variable-length data beyond the fixed layout without breaking existing readers.

Provided Methods§

Source

fn validate_header(data: &[u8]) -> ProgramResult

Validate a raw data slice against this contract.

Returns Ok(()) if the discriminator, version, and layout_id all match. This is the canonical “is this account what I think it is?” check.

Source

fn projected_len() -> usize

Byte length required to project this typed view safely.

Source

fn required_len() -> usize

Minimum account data length required by both the wire contract and projection shape.

Source

fn validate(data: &[u8]) -> bool

Lightweight boolean validation helper for foreign readers and tools.

Source

fn check_disc(data: &[u8]) -> ProgramResult

Check only the discriminator (fast path for dispatch).

Source

fn check_version(data: &[u8]) -> ProgramResult

Check only the version (for migration gates).

Source

fn compatible(version: u8) -> bool

Check whether a given version is compatible with this layout.

The default implementation accepts only the exact version, but implementors can override this to accept older versions for backward-compatible migration.

Source

fn has_extension_region(data: &[u8]) -> bool

Check whether the account data contains an extension region (data beyond the fixed layout boundary).

Source

fn layout_info_static() -> LayoutInfo

Build a LayoutInfo snapshot from this contract’s compile-time constants.

Source

fn fields() -> &'static [FieldInfo]

Compile-time field metadata for this layout.

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.

Implementors§