pub struct Vector<S> { /* private fields */ }Expand description
Element slots are keyed by the element index (u32); see Map for the
key-encoding injectivity invariant that governs the underlying derivation.
§Concurrency
len is a plain storage value that every structural mutation —
push_with, push_empty_slot,
pop — reads and rewrites under an exclusive lock. Of a set
of concurrent transactions that grow or shrink the same vector, only one
can be included in a block; the rest conflict at commit. A Vector is
therefore a poor fit for hot append paths (an event log every caller pushes
to, say) — prefer keying a Map by something caller-specific, or a
mergeable counter like NNInt, when concurrent writers
matter. Element reads and in-place element writes through
at/at_mut read len for their bounds check
but never rewrite it; apart from that read they carry only the
per-element storage semantics.
Implementations§
Source§impl<S: StorageType> Vector<S>
impl<S: StorageType> Vector<S>
Sourcepub fn len(&self) -> u32
pub fn len(&self) -> u32
Current logical extent (number of in-bounds indices).
A missing len slot reads as 0 (a never-written vector is empty).
Any other failure — a value at the slot that is not a u32, a
wrong storage type, a host fault — aborts typed like every other
storage accessor, instead of masquerading as an empty vector.
Vector slots are sparse-capable: callers may delete element storage without shrinking len, so an in-bounds index does not guarantee that the slot currently has backing data.
Sourcepub fn at_mut(&mut self, index: u32) -> SlotMut<'_, Self, S>
pub fn at_mut(&mut self, index: u32) -> SlotMut<'_, Self, S>
Mutably borrow element at index (bounds-checked).
For a Deletable element type the returned slot is itself
Deletable, and deleting it removes only that element’s storage:
len is unchanged, index stays in bounds, and the slot reads back
as missing. Elements do not shift down and the hole is not reused by
a later push. After any element delete, read with try_get +
none_if_missing —
get aborts on a hole — and treat len as a high-water
mark rather than a count of live elements. To reset the vector
entirely, delete the Vector itself, which clears len too.
Sourcepub fn push_empty_slot(&mut self) -> SlotMut<'_, Self, S>
pub fn push_empty_slot(&mut self) -> SlotMut<'_, Self, S>
Allocate a new sparse-capable slot at the end and return a mutable handle to it.
This increments len before the returned slot is written, making the new index immediately in-bounds for the rest of the invocation.
Writes len under an exclusive lock, so concurrent same-block pushes
conflict at commit — see the Concurrency notes on Vector.
Sourcepub fn push_with<R>(&mut self, f: impl FnOnce(&mut S) -> R) -> R
pub fn push_with<R>(&mut self, f: impl FnOnce(&mut S) -> R) -> R
Construct a new slot at the end, initialize it through f, then make
the index visible by incrementing len.
If f panics or traps, the entire invocation frame faults and reverts,
so neither the slot data nor the len increment are committed.
Writes len under an exclusive lock, so concurrent same-block pushes
conflict at commit — see the Concurrency notes on Vector.
Sourcepub fn iter(&self) -> VectorIter<'_, S> ⓘ
pub fn iter(&self) -> VectorIter<'_, S> ⓘ
Iterate over all elements. Yields read-only SlotRef handles.
Holds a shared borrow on the vector, preventing structural
modifications (push_with/push_empty_slot/pop/at_mut) while the
iterator is alive.
Sourcepub fn iter_mut(&mut self) -> VectorIterMut<'_, S> ⓘ
pub fn iter_mut(&mut self) -> VectorIterMut<'_, S> ⓘ
Iterate over all elements with mutable access. Yields owned storage handles that may be used for mutations. Holds an exclusive borrow on the vector.