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§
Provided Associated Constants§
Sourceconst TYPE_OFFSET: usize = HopperHeader::SIZE
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.
Sourceconst RESERVED_BYTES: usize = 0
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.
Sourceconst EXTENSION_OFFSET: Option<usize> = None
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§
Sourcefn validate_header(data: &[u8]) -> ProgramResult
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.
Sourcefn projected_len() -> usize
fn projected_len() -> usize
Byte length required to project this typed view safely.
Sourcefn required_len() -> usize
fn required_len() -> usize
Minimum account data length required by both the wire contract and projection shape.
Sourcefn validate(data: &[u8]) -> bool
fn validate(data: &[u8]) -> bool
Lightweight boolean validation helper for foreign readers and tools.
Sourcefn check_disc(data: &[u8]) -> ProgramResult
fn check_disc(data: &[u8]) -> ProgramResult
Check only the discriminator (fast path for dispatch).
Sourcefn check_version(data: &[u8]) -> ProgramResult
fn check_version(data: &[u8]) -> ProgramResult
Check only the version (for migration gates).
Sourcefn compatible(version: u8) -> bool
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.
Sourcefn has_extension_region(data: &[u8]) -> bool
fn has_extension_region(data: &[u8]) -> bool
Check whether the account data contains an extension region (data beyond the fixed layout boundary).
Sourcefn layout_info_static() -> LayoutInfo
fn layout_info_static() -> LayoutInfo
Build a LayoutInfo snapshot from this contract’s compile-time constants.
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.