Skip to main content

StorageVec

Struct StorageVec 

Source
pub struct StorageVec<V: Codec32> { /* private fields */ }
Expand description

A persistent dynamic array stored in contract storage.

StorageVec provides a Vec-like interface over contract storage slots. It stores a length counter and individual elements at derived slot addresses.

§Type Parameters

  • V - Element type, must implement Codec32 for 32-byte encoding

§Storage Layout

  • derive_slot(namespace, ["vec:len"]) - Length as u64
  • derive_slot(namespace, ["vec:elem", index]) - Element at index

§Example

const NS_HISTORY: Namespace = Namespace([1u8; 32]);
let history = StorageVec::<u64>::new(NS_HISTORY);

// Push elements
history.push(&100)?;
history.push(&200)?;

// Get length
let len = history.len()?; // 2

// Get element
let first = history.get(0)?.unwrap(); // 100

// Pop element
let last = history.pop()?.unwrap(); // 200

Implementations§

Source§

impl<V: Codec32> StorageVec<V>

Source

pub const fn new(namespace: Namespace) -> Self

Creates a new vector with the specified namespace.

Source

pub fn len_slot(&self) -> [u8; 32]

Returns the storage slot for the length counter.

Source

pub fn slot_for_index(&self, index: u64) -> [u8; 32]

Returns the storage slot for an element at the given index.

Source

pub fn len_in<B: StorageBackend>(&self, backend: &B) -> Result<u64>

Returns the number of elements (with explicit backend).

Source

pub fn is_empty_in<B: StorageBackend>(&self, backend: &B) -> Result<bool>

Checks if the vector is empty (with explicit backend).

Source

pub fn get_in<B: StorageBackend>( &self, backend: &B, index: u64, ) -> Result<Option<V>>

Gets the element at index (with explicit backend).

Returns None if index is out of bounds.

Source

pub fn set_in<B: StorageBackend>( &self, backend: &mut B, index: u64, value: &V, ) -> Result<bool>

Sets the element at index (with explicit backend).

Returns false if index is out of bounds.

Source

pub fn push_in<B: StorageBackend>( &self, backend: &mut B, value: &V, ) -> Result<u64>

Appends an element to the end (with explicit backend).

Returns the index where the element was inserted.

Source

pub fn pop_in<B: StorageBackend>(&self, backend: &mut B) -> Result<Option<V>>

Removes and returns the last element (with explicit backend).

Returns None if the vector is empty.

Source

pub fn clear_in<B: StorageBackend>(&self, backend: &mut B) -> Result<()>

Clears the vector by setting length to 0 (with explicit backend).

Note: This doesn’t zero out element slots, just resets the length.

Source

pub fn len(&self) -> Result<u64>

Returns the number of elements (production, uses HostStorage).

Source

pub fn get(&self, index: u64) -> Result<Option<V>>

Gets the element at index (production, uses HostStorage).

Source

pub fn set(&self, index: u64, value: &V) -> Result<bool>

Sets the element at index (production, uses HostStorage).

Source

pub fn push(&self, value: &V) -> Result<u64>

Appends an element to the end (production, uses HostStorage).

Source

pub fn pop(&self) -> Result<Option<V>>

Removes and returns the last element (production, uses HostStorage).

Source

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

Clears the vector (production, uses HostStorage).

Auto Trait Implementations§

§

impl<V> Freeze for StorageVec<V>

§

impl<V> RefUnwindSafe for StorageVec<V>
where V: RefUnwindSafe,

§

impl<V> Send for StorageVec<V>
where V: Send,

§

impl<V> Sync for StorageVec<V>
where V: Sync,

§

impl<V> Unpin for StorageVec<V>
where V: Unpin,

§

impl<V> UnsafeUnpin for StorageVec<V>

§

impl<V> UnwindSafe for StorageVec<V>
where V: 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> 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, 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.