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>
impl<H: Hasher> Heap<H>
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Get the number of retained resource entries.
Consumed resources are still retained until removed explicitly.
Sourcepub fn nullified_count(&self) -> usize
pub fn nullified_count(&self) -> usize
Get the number of nullified resources.
Sourcepub fn contains(&self, rid: &ResourceId<H>) -> bool
pub fn contains(&self, rid: &ResourceId<H>) -> bool
Check if a resource exists in the heap (not necessarily active).
Sourcepub fn is_consumed(&self, rid: &ResourceId<H>) -> bool
pub fn is_consumed(&self, rid: &ResourceId<H>) -> bool
Check if a resource has been consumed (nullified).
Sourcepub fn is_active(&self, rid: &ResourceId<H>) -> bool
pub fn is_active(&self, rid: &ResourceId<H>) -> bool
Check if a resource is active (exists and not consumed).
Sourcepub fn alloc_counter(&self) -> u64
pub fn alloc_counter(&self) -> u64
Get the current allocation counter.
Sourcepub fn alloc(&self, resource: Resource) -> (ResourceId<H>, Heap<H>)
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.
Sourcepub fn alloc_mut(&mut self, resource: Resource) -> ResourceId<H>
pub fn alloc_mut(&mut self, resource: Resource) -> ResourceId<H>
Allocate a resource mutably (for efficiency when building heaps).
Sourcepub fn read(&self, rid: &ResourceId<H>) -> Result<&Resource, HeapError<H>>
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.
Sourcepub fn consume(&self, rid: &ResourceId<H>) -> Result<Heap<H>, HeapError<H>>
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.
Sourcepub fn consume_mut(&mut self, rid: &ResourceId<H>) -> Result<(), HeapError<H>>
pub fn consume_mut(&mut self, rid: &ResourceId<H>) -> Result<(), HeapError<H>>
Consume a resource mutably.
Sourcepub fn remove(&self, rid: &ResourceId<H>) -> Result<Heap<H>, HeapError<H>>
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.
Sourcepub fn active_resources(
&self,
) -> impl Iterator<Item = (&ResourceId<H>, &Resource)>
pub fn active_resources( &self, ) -> impl Iterator<Item = (&ResourceId<H>, &Resource)>
Get all active (non-consumed) resources.
Sourcepub fn consumed_ids(&self) -> impl Iterator<Item = &ResourceId<H>>
pub fn consumed_ids(&self) -> impl Iterator<Item = &ResourceId<H>>
Get all consumed resource IDs.
Sourcepub fn alloc_many(
&self,
resources: impl IntoIterator<Item = Resource>,
) -> (Vec<ResourceId<H>>, Heap<H>)
pub fn alloc_many( &self, resources: impl IntoIterator<Item = Resource>, ) -> (Vec<ResourceId<H>>, Heap<H>)
Allocate multiple resources at once.
Sourcepub fn consume_many(
&self,
rids: &[ResourceId<H>],
) -> Result<Heap<H>, HeapError<H>>
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.
Sourcepub fn alloc_channel(
&self,
sender: &str,
receiver: &str,
) -> (ResourceId<H>, Heap<H>)
pub fn alloc_channel( &self, sender: &str, receiver: &str, ) -> (ResourceId<H>, Heap<H>)
Create a channel resource and allocate it.
Sourcepub fn alloc_message(
&self,
source: &str,
dest: &str,
label: &str,
payload: Vec<u8>,
seq_no: u64,
) -> (ResourceId<H>, Heap<H>)
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.
Sourcepub fn alloc_session(
&self,
role: &str,
type_hash: u64,
) -> (ResourceId<H>, Heap<H>)
pub fn alloc_session( &self, role: &str, type_hash: u64, ) -> (ResourceId<H>, Heap<H>)
Create a session state resource and allocate it.