Expand description
RAII-leased typed segment guards.
SegmentBorrowRegistry
records live byte-range borrows. SegmentLease owns one registry entry
and removes it on drop. SegRef and SegRefMut pair that lease with an
account-data guard, allowing sequential access after the previous guard is
dropped while rejecting incompatible live ranges.
For example, both mutations below are sequential because the first guard is dropped before the second is acquired:
{ let mut b = ctx.segment_mut::<WireU64>(0, BAL)?; *b += amount; }
{ let mut b = ctx.segment_mut::<WireU64>(0, BAL)?; *b += more; }§Representation
SegmentLease stores a raw pointer to the registry plus a
PhantomData<&'a mut SegmentBorrowRegistry>. A raw pointer avoids extending
a Rust &mut borrow of the entire context through the returned segment
guard. The lifetime marker ties the lease to the registry borrow, and Drop
performs an exact entry release without allocation.
§Why a wrapper, not a field on Ref/RefMut
The canonical hopper_runtime::Ref / RefMut are kept flat on
Solana ({ptr, state_ptr} = 2 words, see borrow.rs). Adding a
registry pointer to them would expand the representation
for all access paths, including the whole-account load() path that
doesn’t touch the segment registry. Keeping the lease as a separate
wrapper leaves load() at two words while segment access carries the
additional lease pointer.
Structs§
- SegRef
- Shared typed segment guard: a
Ref<T>paired with aSegmentLeasethat releases the registry entry on drop. - SegRef
Mut - Exclusive typed segment guard.
- Segment
Lease - RAII lease on one registered entry in a
SegmentBorrowRegistry. - Segments
Mut - A guard over several disjoint typed sub-ranges of one account,
returned by
AccountView::split_segments_mut.