Skip to main content

Array

Struct Array 

Source
pub struct Array<S, const N: usize> { /* private fields */ }
Expand description

A fixed-length array of N storage slots of type S.

Element i lives at the array’s keypath extended by i as a u32 — the same slot Map<u32, S> at that keypath would use for key i, so the two layouts are interchangeable. Like Map, an Array holds only its own keypath and derives an element’s key when the element is accessed, so its size and load cost do not depend on N. Unlike Map, the length is known at compile time: indexing is bounds-checked without a storage read, and the array can enumerate (iterate and delete) its elements.

Unlike Vector, there is no stored length: every index in 0..N is always in bounds, and an element that was never written (or was deleted) reads back as missing. Read with try_get + none_if_missing when an element may be unset. Elements share no storage, so writes to distinct elements never conflict with each other.

N must fit in a u32; a larger N fails to compile on first use.

Implementations§

Source§

impl<S, const N: usize> Array<S, N>

Source

pub const LEN: u32

The array’s length, N.

Source

pub fn keypath(&self) -> &Keypath

Source

pub const fn len(&self) -> u32

The array’s length, N. Never reads storage.

Source

pub const fn is_empty(&self) -> bool

Returns true if N == 0.

Source§

impl<S: StorageType, const N: usize> Array<S, N>

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

Source

pub fn at_mut_exclusive(&mut self, index: u32) -> SlotMut<'_, Self, S>

Like at_mut, but every successful write through the returned handle is followed by a request for an exclusive lock on the element. See Map::at_mut_exclusive.

Source

pub fn at_disjoint_mut<const M: usize>( &mut self, indices: [u32; M], ) -> DisjointMut<'_, Self, S, M>

Mutably borrow M elements at once (bounds-checked). Panics if any indices are equal.

See Map::at_disjoint_mut.

Source

pub fn iter(&self) -> ArrayIter<'_, S, N> ⓘ

Iterate over all N elements. Yields read-only SlotRef handles, deriving each element’s key as it is reached.

Source

pub fn iter_mut(&mut self) -> ArrayIterMut<'_, S, N> ⓘ

Iterate over all N elements with mutable access. Holds an exclusive borrow on the array.

Source§

impl<S: StorageType + Deletable, const N: usize> Array<S, N>

Source

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

Delete the storage of the element at index (bounds-checked). The element reads back as missing afterwards.

Trait Implementations§

Source§

impl<S, const N: usize> Debug for Array<S, N>

Source§

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

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

impl<S: StorageType + Deletable, const N: usize> Deletable for Array<S, N>

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, const N: usize> From<Keypath> for Array<S, N>

Source§

fn from(keypath: Keypath) -> Self

Converts to this type from the input type.
Source§

impl<'a, S: StorageType, const N: usize> IntoIterator for &'a Array<S, N>

Source§

type Item = SlotRef<'a, Array<S, N>, S>

The type of the elements being iterated over.
Source§

type IntoIter = ArrayIter<'a, S, N>

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, const N: usize> IntoIterator for &'a mut Array<S, N>

Source§

type Item = SlotMut<'a, Array<S, N>, S>

The type of the elements being iterated over.
Source§

type IntoIter = ArrayIterMut<'a, S, N>

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, const N: usize> StorageType for Array<S, N>

Auto Trait Implementations§

§

impl<S, const N: usize> Freeze for Array<S, N>
where PhantomData<fn() -> S>: Freeze,

§

impl<S, const N: usize> RefUnwindSafe for Array<S, N>
where PhantomData<fn() -> S>: RefUnwindSafe,

§

impl<S, const N: usize> Send for Array<S, N>
where PhantomData<fn() -> S>: Send,

§

impl<S, const N: usize> Sync for Array<S, N>
where PhantomData<fn() -> S>: Sync,

§

impl<S, const N: usize> Unpin for Array<S, N>
where PhantomData<fn() -> S>: Unpin,

§

impl<S, const N: usize> UnsafeUnpin for Array<S, N>
where PhantomData<fn() -> S>: UnsafeUnpin,

§

impl<S, const N: usize> UnwindSafe for Array<S, N>
where PhantomData<fn() -> 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.