VarFabricVec

Struct VarFabricVec 

Source
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>

Source

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.

Source

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.

Source

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
Source

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).

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of elements.

Source

pub fn is_empty(&self) -> bool

Returns true if the vector is empty.

Source

pub fn index_capacity(&self) -> usize

Returns the maximum number of index entries.

Source

pub fn heap_size(&self) -> u64

Returns the total heap space in bytes.

Source

pub fn heap_used(&self) -> u64

Returns the heap space used so far in bytes.

Source

pub fn clear(&mut self) -> Result<()>

Clear the vector, resetting len and heap_used to 0.

Source

pub fn lease_id(&self) -> u128

Returns the lease ID of the memory lease for external renewal management (e.g. via [grafos_leasekit::RenewalManager]).

Source

pub fn expires_at_unix_secs(&self) -> u64

Returns the expiry time (unix seconds) of the memory lease for external renewal management.

Source

pub fn iter(&self) -> VarFabricVecIter<'_, T>

Returns an iterator over the elements.

Auto Trait Implementations§

§

impl<T> Freeze for VarFabricVec<T>

§

impl<T> !RefUnwindSafe for VarFabricVec<T>

§

impl<T> !Send for VarFabricVec<T>

§

impl<T> !Sync for VarFabricVec<T>

§

impl<T> Unpin for VarFabricVec<T>
where T: Unpin,

§

impl<T> !UnwindSafe for VarFabricVec<T>

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.