Struct stylus_sdk::storage::StorageVec
source · pub struct StorageVec<S: StorageType> { /* private fields */ }
Expand description
Accessor for a storage-backed vector.
Implementations§
source§impl<S: StorageType> StorageVec<S>
impl<S: StorageType> StorageVec<S>
sourcepub unsafe fn set_len(&mut self, len: usize)
pub unsafe fn set_len(&mut self, len: usize)
Overwrites the vector’s length.
§Safety
It must be sensible to create accessors for S
from zero-slots,
or any junk data left over from prior dirty operations.
Note that StorageVec
has unlimited capacity, so all lengths are valid.
sourcepub fn getter(&self, index: impl TryInto<usize>) -> Option<StorageGuard<'_, S>>
pub fn getter(&self, index: impl TryInto<usize>) -> Option<StorageGuard<'_, S>>
Gets an accessor to the element at a given index, if it exists.
Note: the accessor is protected by a StorageGuard
, which restricts
its lifetime to that of &self
.
sourcepub fn setter(
&mut self,
index: impl TryInto<usize>
) -> Option<StorageGuardMut<'_, S>>
pub fn setter( &mut self, index: impl TryInto<usize> ) -> Option<StorageGuardMut<'_, S>>
Gets a mutable accessor to the element at a given index, if it exists.
Note: the accessor is protected by a StorageGuardMut
, which restricts
its lifetime to that of &mut self
.
sourcepub fn get(&self, index: impl TryInto<usize>) -> Option<S::Wraps<'_>>
pub fn get(&self, index: impl TryInto<usize>) -> Option<S::Wraps<'_>>
Gets the element at the given index, if it exists.
sourcepub fn get_mut(&mut self, index: impl TryInto<usize>) -> Option<S::WrapsMut<'_>>
pub fn get_mut(&mut self, index: impl TryInto<usize>) -> Option<S::WrapsMut<'_>>
Gets a mutable accessor to the element at a given index, if it exists.
sourcepub fn grow(&mut self) -> StorageGuardMut<'_, S>
pub fn grow(&mut self) -> StorageGuardMut<'_, S>
Like std::vec::Vec::push
, but returns a mutable accessor to the new slot.
This enables pushing elements without constructing them first.
§Example
use stylus_sdk::storage::{StorageVec, StorageType, StorageU256};
use stylus_sdk::alloy_primitives::U256;
let mut vec: StorageVec<StorageVec<StorageU256>> = unsafe { StorageVec::new(U256::ZERO, 0) };
let mut inner_vec = vec.grow();
inner_vec.push(U256::from(8));
let value = inner_vec.get(0).unwrap();
assert_eq!(value, U256::from(8));
assert_eq!(inner_vec.len(), 1);
sourcepub fn shrink(&mut self) -> Option<StorageGuardMut<'_, S>>
pub fn shrink(&mut self) -> Option<StorageGuardMut<'_, S>>
Removes and returns an accessor to the last element of the vector, if any.
source§impl<'a, S: SimpleStorageType<'a>> StorageVec<S>
impl<'a, S: SimpleStorageType<'a>> StorageVec<S>
source§impl<S: Erase> StorageVec<S>
impl<S: Erase> StorageVec<S>
sourcepub fn erase_last(&mut self)
pub fn erase_last(&mut self)
Removes and erases the last element of the vector.
Trait Implementations§
source§impl<S: Erase> Erase for StorageVec<S>
impl<S: Erase> Erase for StorageVec<S>
source§impl<'a, S: SimpleStorageType<'a>> Extend<<S as StorageType>::Wraps<'a>> for StorageVec<S>
impl<'a, S: SimpleStorageType<'a>> Extend<<S as StorageType>::Wraps<'a>> for StorageVec<S>
source§fn extend<T: IntoIterator<Item = S::Wraps<'a>>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = S::Wraps<'a>>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<S: StorageType> StorageType for StorageVec<S>
impl<S: StorageType> StorageType for StorageVec<S>
§type Wraps<'a> = StorageGuard<'a, StorageVec<S>>
where
Self: 'a
type Wraps<'a> = StorageGuard<'a, StorageVec<S>> where Self: 'a
StorageType
being collected.§type WrapsMut<'a> = StorageGuardMut<'a, StorageVec<S>>
where
Self: 'a
type WrapsMut<'a> = StorageGuardMut<'a, StorageVec<S>> where Self: 'a
source§unsafe fn new(slot: U256, offset: u8) -> Self
unsafe fn new(slot: U256, offset: u8) -> Self
const
once generic_const_exprs
stabilizes. Read moresource§fn load<'s>(self) -> Self::Wraps<'s>
fn load<'s>(self) -> Self::Wraps<'s>
get
and/or getter
, which don’t consume Self
.source§fn load_mut<'s>(self) -> Self::WrapsMut<'s>
fn load_mut<'s>(self) -> Self::WrapsMut<'s>
set
and/or setter
, which don’t consume Self
.source§const SLOT_BYTES: usize = 32usize
const SLOT_BYTES: usize = 32usize
StorageType::new
. Read more