pub struct VarFabricVec<T> { /* private fields */ }Expand description
A vector that stores variable-size elements in leased fabric memory.
Unlike FabricVec which uses fixed-stride slots, VarFabricVec uses
an index+heap layout. The index region holds fixed-size 12-byte entries
(offset: u64, len: u32) that point into a heap region where the actual
postcard-serialized data is packed contiguously. This avoids wasting
space when serialized element sizes vary widely (e.g. String,
Vec<u8>).
§Trade-offs
- Better space efficiency for variable-size types.
- Two remote reads per
get()(index entry + heap data) instead of one. - No in-place
set()if the new element is larger than the old one (the old heap space is not reclaimed; a compacting GC is not provided).
§Example
use grafos_collections::vec::VarFabricVec;
use grafos_std::mem::MemBuilder;
let lease = MemBuilder::new().min_bytes(8192).acquire()?;
let mut v: VarFabricVec<String> = VarFabricVec::new(lease, 128)?;
v.push(&"hello".into())?;
v.push(&"world, this is a longer string".into())?;
assert_eq!(v.get(0)?, "hello".to_string());
assert_eq!(v.len(), 2);Implementations§
Source§impl<T: Serialize + DeserializeOwned> VarFabricVec<T>
impl<T: Serialize + DeserializeOwned> VarFabricVec<T>
Sourcepub fn new(lease: MemLease, max_elements: usize) -> Result<Self>
pub fn new(lease: MemLease, max_elements: usize) -> Result<Self>
Create a new VarFabricVec over the given lease.
max_elements determines the number of index entries reserved.
The remaining arena space after the header and index region is
used as the heap for element data.
§Errors
Returns FabricError::CapacityExceeded if the arena is too small
for the header, index region, and at least 1 byte of heap.
Sourcepub fn with_capacity(
max_elements: usize,
avg_element_bytes: usize,
) -> Result<Self>
pub fn with_capacity( max_elements: usize, avg_element_bytes: usize, ) -> Result<Self>
Create a new VarFabricVec by acquiring a lease.
max_elements is the index capacity. avg_element_bytes is a hint
for sizing the heap region. The actual arena requested is:
header + index + max_elements * avg_element_bytes.
Sourcepub fn push(&mut self, item: &T) -> Result<()>
pub fn push(&mut self, item: &T) -> Result<()>
Push an element onto the end of the vector.
Serializes the element and appends it to the heap region.
§Errors
FabricError::CapacityExceededif the index region is full or the heap cannot fit the serialized element.
Sourcepub fn pop(&mut self) -> Result<Option<T>>
pub fn pop(&mut self) -> Result<Option<T>>
Remove and return the last element, or None if empty.
The heap space used by the popped element is reclaimed only if it was the last item appended (heap_used is rewound).
Sourcepub fn get(&self, index: usize) -> Result<T>
pub fn get(&self, index: usize) -> Result<T>
Read the element at index.
Performs two remote reads: one for the index entry and one for the heap data.
Sourcepub fn index_capacity(&self) -> usize
pub fn index_capacity(&self) -> usize
Returns the maximum number of index entries.
Sourcepub fn lease_id(&self) -> u128
pub fn lease_id(&self) -> u128
Returns the lease ID of the memory lease for external renewal
management (e.g. via [grafos_leasekit::RenewalManager]).
Sourcepub fn expires_at_unix_secs(&self) -> u64
pub fn expires_at_unix_secs(&self) -> u64
Returns the expiry time (unix seconds) of the memory lease for external renewal management.
Sourcepub fn iter(&self) -> VarFabricVecIter<'_, T> ⓘ
pub fn iter(&self) -> VarFabricVecIter<'_, T> ⓘ
Returns an iterator over the elements.