Skip to main content

SegmentMap

Trait SegmentMap 

Source
pub trait SegmentMap {
    const SEGMENTS: &'static [StaticSegment];

    // Provided methods
    fn segment(name: &str) -> Option<StaticSegment> { ... }
    fn body_size() -> u32 { ... }
    fn segment_count() -> usize { ... }
    fn segment_by_index(index: usize) -> StaticSegment { ... }
}
Expand description

Compile-time segment layout for a zero-copy struct.

Types implementing this trait declare their byte-level field map as a constant array. This enables segment-level access with zero runtime overhead, the compiler can resolve segment offsets to immediate constants in the generated code.

§Runtime vs Compile-Time

  • SegmentMap = compile-time, generated from struct definitions
  • SegmentRegistry = runtime, stored on-chain for dynamic accounts

Both can coexist: a struct might implement SegmentMap for its fixed fields while also using SegmentRegistry for its dynamic segments.

Required Associated Constants§

Source

const SEGMENTS: &'static [StaticSegment]

All segments in this layout, ordered by offset.

Provided Methods§

Source

fn segment(name: &str) -> Option<StaticSegment>

Look up a segment by field name.

Linear scan over const array. For layouts with ≤16 fields (the norm), this compiles to a small branchless sequence.

Source

fn body_size() -> u32

Total size of the layout body (sum of all segments, or max(end)).

Source

fn segment_count() -> usize

Number of segments.

Source

fn segment_by_index(index: usize) -> StaticSegment

Look up a segment by index (zero-overhead const access).

Prefer this over segment() in generated accessor code where the index is a compile-time constant. The compiler will resolve this to an immediate constant load, no branching, no string comparison.

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§