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 implementCodec32for 32-byte encoding
§Storage Layout
derive_slot(namespace, ["vec:len"])- Length as u64derive_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(); // 200Implementations§
Source§impl<V: Codec32> StorageVec<V>
impl<V: Codec32> StorageVec<V>
Sourcepub const fn new(namespace: Namespace) -> Self
pub const fn new(namespace: Namespace) -> Self
Creates a new vector with the specified namespace.
Sourcepub fn slot_for_index(&self, index: u64) -> [u8; 32]
pub fn slot_for_index(&self, index: u64) -> [u8; 32]
Returns the storage slot for an element at the given index.
Sourcepub fn len_in<B: StorageBackend>(&self, backend: &B) -> Result<u64>
pub fn len_in<B: StorageBackend>(&self, backend: &B) -> Result<u64>
Returns the number of elements (with explicit backend).
Sourcepub fn is_empty_in<B: StorageBackend>(&self, backend: &B) -> Result<bool>
pub fn is_empty_in<B: StorageBackend>(&self, backend: &B) -> Result<bool>
Checks if the vector is empty (with explicit backend).
Sourcepub fn get_in<B: StorageBackend>(
&self,
backend: &B,
index: u64,
) -> Result<Option<V>>
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.
Sourcepub fn set_in<B: StorageBackend>(
&self,
backend: &mut B,
index: u64,
value: &V,
) -> Result<bool>
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.
Sourcepub fn push_in<B: StorageBackend>(
&self,
backend: &mut B,
value: &V,
) -> Result<u64>
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.
Sourcepub fn pop_in<B: StorageBackend>(&self, backend: &mut B) -> Result<Option<V>>
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.
Sourcepub fn clear_in<B: StorageBackend>(&self, backend: &mut B) -> Result<()>
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.
Sourcepub fn len(&self) -> Result<u64>
pub fn len(&self) -> Result<u64>
Returns the number of elements (production, uses HostStorage).
Sourcepub fn get(&self, index: u64) -> Result<Option<V>>
pub fn get(&self, index: u64) -> Result<Option<V>>
Gets the element at index (production, uses HostStorage).
Sourcepub fn set(&self, index: u64, value: &V) -> Result<bool>
pub fn set(&self, index: u64, value: &V) -> Result<bool>
Sets the element at index (production, uses HostStorage).
Sourcepub fn push(&self, value: &V) -> Result<u64>
pub fn push(&self, value: &V) -> Result<u64>
Appends an element to the end (production, uses HostStorage).