Skip to main content

Vector

Struct Vector 

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

Source

pub fn keypath(&self) -> &Keypath

Source§

impl<S: StorageType> Vector<S>

Source

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.

Source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no elements.

Source

pub fn at(&self, index: u32) -> SlotRef<'_, Self, S>

Borrow element at index (bounds-checked).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source§

impl<S: StorageType + Deletable> Vector<S>

Source

pub fn pop(&mut self)

Remove the last element, deleting its storage. Aborts if the vector is empty.

Writes len under an exclusive lock, so concurrent same-block structural mutations conflict at commit — see the Concurrency notes on Vector.

Source

pub fn delete_at(&mut self, index: u32)

Delete the storage of the element at index (bounds-checked).

Shorthand for at_mut(index).delete(). This leaves a hole rather than removing the element: len is untouched and index stays in bounds. See at_mut for the full sparse-vector semantics, and pop to actually shrink the vector.

Trait Implementations§

Source§

impl<S> Debug for Vector<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: StorageType + Deletable> Deletable for Vector<S>

Source§

fn delete_internal(&mut self, exclusive: bool)

Delete storage for this node. Read more
Source§

fn delete(&mut self)

Delete using the keypath’s own locking semantics.
Source§

fn delete_exclusive(&mut self)

Delete, then request an exclusive lock, regardless of keypath flag. Later writes in the transaction keep the lock.
Source§

impl<S> From<Keypath> for Vector<S>

Source§

fn from(keypath: Keypath) -> Self

Converts to this type from the input type.
Source§

impl<'a, S: StorageType> IntoIterator for &'a Vector<S>

Source§

type Item = SlotRef<'a, Vector<S>, S>

The type of the elements being iterated over.
Source§

type IntoIter = VectorIter<'a, S>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, S: StorageType> IntoIterator for &'a mut Vector<S>

Source§

type Item = SlotMut<'a, Vector<S>, S>

The type of the elements being iterated over.
Source§

type IntoIter = VectorIterMut<'a, S>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<S> StorageType for Vector<S>

Auto Trait Implementations§

§

impl<S> Freeze for Vector<S>
where Map<u32, S>: Freeze,

§

impl<S> RefUnwindSafe for Vector<S>
where Map<u32, S>: RefUnwindSafe,

§

impl<S> Send for Vector<S>
where Map<u32, S>: Send,

§

impl<S> Sync for Vector<S>
where Map<u32, S>: Sync,

§

impl<S> Unpin for Vector<S>
where Map<u32, S>: Unpin,

§

impl<S> UnsafeUnpin for Vector<S>
where Map<u32, S>: UnsafeUnpin,

§

impl<S> UnwindSafe for Vector<S>
where Map<u32, S>: UnwindSafe,

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> ContractState for T
where T: From<Keypath>,

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> StaticIfNoAlloc for T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.