Skip to main content

SegmentBorrowRegistry

Struct SegmentBorrowRegistry 

Source
pub struct SegmentBorrowRegistry { /* private fields */ }
Expand description

Instruction-scoped segment borrow registry.

Tracks active segment borrows and enforces conflict rules. Designed for inline use in an execution context, no heap, no dynamic dispatch.

Uses compact 8-byte address fingerprints and a flat array of fixed-size entries. Total stack footprint: ~280 bytes (vs ~1.3 KB with full 32-byte addresses and Option wrappers).

§Example

let mut borrows = SegmentBorrowRegistry::new();
borrows.register_read(&vault_key, 0, 8)?;   // read balance
borrows.register_write(&vault_key, 8, 32)?;  // write metadata, OK, non-overlapping
borrows.register_write(&vault_key, 0, 8)?;   // REJECTED, overlaps read

Implementations§

Source§

impl SegmentBorrowRegistry

Source

pub const fn new() -> Self

Create an empty registry.

Source

pub const fn len(&self) -> usize

Number of active borrows.

Source

pub const fn is_empty(&self) -> bool

Whether the registry is empty.

Source

pub fn register_leased_read( &mut self, key: &Address, offset: u32, size: u32, ) -> Result<SegmentBorrow, ProgramError>

Register a new read borrow and return the SegmentBorrow record the caller can hand to SegmentLease::new for RAII release. This is the plumbing that makes crate::segment_lease::SegRef possible.

Source

pub fn register_leased_write( &mut self, key: &Address, offset: u32, size: u32, ) -> Result<SegmentBorrow, ProgramError>

Mutable counterpart of [register_leased_read].

Source

pub fn register(&mut self, new: SegmentBorrow) -> Result<(), ProgramError>

Register a new segment borrow, checking for conflicts.

Returns Err(AccountBorrowFailed) if the new borrow overlaps an existing borrow with incompatible access (read+write or write+write) on the same account (full-address identity, not fingerprint).

Source

pub fn register_read( &mut self, key: &Address, offset: u32, size: u32, ) -> Result<(), ProgramError>

Convenience: register a read borrow for the given account region.

Source

pub fn register_write( &mut self, key: &Address, offset: u32, size: u32, ) -> Result<(), ProgramError>

Convenience: register a write borrow for the given account region.

Source

pub fn release(&mut self, borrow: &SegmentBorrow) -> bool

Release a previously registered borrow.

Finds the first matching entry and removes it, compacting the array. Identity is full-address (not fingerprint) to stay collision-safe.

Source

pub fn clear(&mut self)

Reset the registry, clearing all active borrows.

Source

pub fn would_conflict(&self, proposed: &SegmentBorrow) -> bool

Check if a proposed borrow would conflict, without registering it.

Uses full-address identity, fingerprint collisions do not produce false positives.

Source

pub fn register_guard( &mut self, borrow: SegmentBorrow, ) -> Result<SegmentBorrowGuard<'_>, ProgramError>

Register a borrow and return an RAII guard that auto-releases it on drop.

This is the preferred way to acquire segment borrows, the guard ensures the borrow is released even if the caller returns early via ? or encounters an error.

§Example
{
    let _guard = borrows.register_guard_write(&key, 0, 8)?;
    // ... write to segment ...
} // guard dropped → borrow released
Source

pub fn register_guard_read( &mut self, key: &Address, offset: u32, size: u32, ) -> Result<SegmentBorrowGuard<'_>, ProgramError>

Register a read borrow with RAII auto-release.

Source

pub fn register_guard_write( &mut self, key: &Address, offset: u32, size: u32, ) -> Result<SegmentBorrowGuard<'_>, ProgramError>

Register a write borrow with RAII auto-release.

Source

pub fn for_each<F: FnMut(&SegmentBorrow)>(&self, f: F)

Visit each active borrow in registration order.

Intended for diagnostics and for the hopper explain introspection path, never for hot-path decisions.

Source

pub fn find_exact( &self, key: &Address, offset: u32, size: u32, kind: AccessKind, ) -> Option<&SegmentBorrow>

Look up an active borrow by exact (key, offset, size, kind).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.