Skip to main content

Heap

Struct Heap 

Source
pub struct Heap<H: Hasher = DefaultContentHasher> { /* private fields */ }
Expand description

Deterministic heap for protocol resources.

Uses BTreeMap for O(log n) operations with deterministic iteration order. The nullifiers set tracks consumed resources to prevent double-spending.

Implementations§

Source§

impl<H: Hasher> Heap<H>

Source

pub fn new() -> Self

Create an empty heap.

Source

pub fn size(&self) -> usize

Get the number of retained resource entries.

Consumed resources are still retained until removed explicitly.

Source

pub fn nullified_count(&self) -> usize

Get the number of nullified resources.

Source

pub fn contains(&self, rid: &ResourceId<H>) -> bool

Check if a resource exists in the heap (not necessarily active).

Source

pub fn is_consumed(&self, rid: &ResourceId<H>) -> bool

Check if a resource has been consumed (nullified).

Source

pub fn is_active(&self, rid: &ResourceId<H>) -> bool

Check if a resource is active (exists and not consumed).

Source

pub fn alloc_counter(&self) -> u64

Get the current allocation counter.

Source

pub fn alloc(&self, resource: Resource) -> (ResourceId<H>, Heap<H>)

Allocate a new resource on the heap.

Returns the new ResourceId and updated heap. The ResourceId is derived from the resource content and allocation counter.

Source

pub fn alloc_mut(&mut self, resource: Resource) -> ResourceId<H>

Allocate a resource mutably (for efficiency when building heaps).

Source

pub fn read(&self, rid: &ResourceId<H>) -> Result<&Resource, HeapError<H>>

Read a resource from the heap.

Returns an error if the resource doesn’t exist or has been consumed.

Source

pub fn consume(&self, rid: &ResourceId<H>) -> Result<Heap<H>, HeapError<H>>

Consume a resource, adding it to the nullifier set.

Returns an error if the resource doesn’t exist or has already been consumed. The resource remains in the heap but is marked as consumed.

Source

pub fn consume_mut(&mut self, rid: &ResourceId<H>) -> Result<(), HeapError<H>>

Consume a resource mutably.

Source

pub fn remove(&self, rid: &ResourceId<H>) -> Result<Heap<H>, HeapError<H>>

Remove a resource from the heap entirely (for cleanup).

Unlike consume, this removes the resource from both maps. Returns an error if the resource doesn’t exist.

Source

pub fn active_resources( &self, ) -> impl Iterator<Item = (&ResourceId<H>, &Resource)>

Get all active (non-consumed) resources.

Source

pub fn consumed_ids(&self) -> impl Iterator<Item = &ResourceId<H>>

Get all consumed resource IDs.

Source

pub fn alloc_many( &self, resources: impl IntoIterator<Item = Resource>, ) -> (Vec<ResourceId<H>>, Heap<H>)

Allocate multiple resources at once.

Source

pub fn consume_many( &self, rids: &[ResourceId<H>], ) -> Result<Heap<H>, HeapError<H>>

Try to consume multiple resources atomically.

If any consumption fails, returns the error without modifying the heap.

Source

pub fn alloc_channel( &self, sender: &str, receiver: &str, ) -> (ResourceId<H>, Heap<H>)

Create a channel resource and allocate it.

Source

pub fn alloc_message( &self, source: &str, dest: &str, label: &str, payload: Vec<u8>, seq_no: u64, ) -> (ResourceId<H>, Heap<H>)

Create a message resource and allocate it.

Source

pub fn alloc_session( &self, role: &str, type_hash: u64, ) -> (ResourceId<H>, Heap<H>)

Create a session state resource and allocate it.

Trait Implementations§

Source§

impl<H: Clone + Hasher> Clone for Heap<H>

Source§

fn clone(&self) -> Heap<H>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<H: Debug + Hasher> Debug for Heap<H>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<H: Default + Hasher> Default for Heap<H>

Source§

fn default() -> Heap<H>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<H> Freeze for Heap<H>

§

impl<H> RefUnwindSafe for Heap<H>

§

impl<H> Send for Heap<H>

§

impl<H> Sync for Heap<H>

§

impl<H> Unpin for Heap<H>

§

impl<H> UnsafeUnpin for Heap<H>

§

impl<H> UnwindSafe for Heap<H>

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> Endpoint for T
where T: Send,

Source§

impl<T> ProgramMessage for T
where T: Clone + Send + Sync + Debug,