pub struct FabricVec<T> { /* private fields */ }Expand description
A growable array stored in leased fabric memory.
Elements are serialized via postcard into fixed-size slots within the
arena. The collection owns the underlying MemLease and releases it
when dropped.
§Type parameters
T must implement Serialize + DeserializeOwned. The serialized form
of each element must fit within stride - 4 bytes (4 bytes are used
for the length prefix).
§Example
use grafos_collections::vec::FabricVec;
use grafos_std::mem::MemBuilder;
let mut v: FabricVec<u32> = FabricVec::with_capacity(100, 16)?;
v.push(&42)?;
v.push(&99)?;
for item in v.iter() {
println!("{}", item?);
}Implementations§
Source§impl<T: Serialize + DeserializeOwned> FabricVec<T>
impl<T: Serialize + DeserializeOwned> FabricVec<T>
Sourcepub fn new(lease: MemLease, stride: usize) -> Result<Self>
pub fn new(lease: MemLease, stride: usize) -> Result<Self>
Create a new FabricVec that takes ownership of the given lease.
stride is the slot width in bytes. Each slot stores a 4-byte
length prefix plus the postcard-serialized element, so the maximum
serialized element size is stride - 4.
Capacity is computed as (arena_size - 24) / stride.
§Errors
Returns FabricError::CapacityExceeded if the arena is too small
to hold even the 24-byte header.
Sourcepub fn with_capacity(cap: usize, stride: usize) -> Result<Self>
pub fn with_capacity(cap: usize, stride: usize) -> Result<Self>
Create a new FabricVec by acquiring a lease sized for cap elements
of stride bytes each.
This is a convenience constructor that calls MemBuilder::new().min_bytes(...).acquire()
and then FabricVec::new().
§Errors
Returns FabricError::CapacityExceeded if the host cannot provide
an arena large enough for cap * stride + 24 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.
If the vector is full, grow() is called to acquire a new memory
lease and chain it. Returns FabricError::CapacityExceeded only
if grow() fails (the host cannot allocate more memory).
Sourcepub fn grow(&mut self) -> Result<()>
pub fn grow(&mut self) -> Result<()>
Acquire a new memory lease and chain it to this vector, increasing total capacity.
The new region has the same stride and approximately the same capacity as the primary region.
§Errors
Returns FabricError::CapacityExceeded if the host cannot
allocate more memory.
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 the vector is empty.
Sourcepub fn iter(&self) -> FabricVecIter<'_, T> ⓘ
pub fn iter(&self) -> FabricVecIter<'_, T> ⓘ
Returns an iterator over the elements.
Sourcepub fn lease_id(&self) -> u128
pub fn lease_id(&self) -> u128
Returns the lease ID of the primary 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 primary memory lease for external renewal management.