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
extern crate alloc;
use stylus_sdk::storage::{StorageVec, StorageType, StorageU256};
use stylus_sdk::alloy_primitives::U256;
use stylus_test::*;
let vm = TestVM::default();
let mut vec: StorageVec<StorageVec<StorageU256>> = StorageVec::from(&vm);
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, T> From<&T> for StorageVec<S>
impl<S, T> From<&T> for StorageVec<S>
Source§impl<S: StorageType> HostAccess for StorageVec<S>
impl<S: StorageType> HostAccess for StorageVec<S>
Source§impl<S: StorageType> StorageType for StorageVec<S>
impl<S: StorageType> StorageType for StorageVec<S>
Source§type Wraps<'a> = StorageGuard<'a, StorageVec<S>>
where
Self: 'a
type Wraps<'a> = StorageGuard<'a, StorageVec<S>> where Self: 'a
StorageType
being collected.Source§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, host: VM) -> Self
unsafe fn new(slot: U256, offset: u8, host: VM) -> 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 moreSource§const REQUIRED_SLOTS: usize = 0usize
const REQUIRED_SLOTS: usize = 0usize
Auto Trait Implementations§
impl<S> !Freeze for StorageVec<S>
impl<S> !RefUnwindSafe for StorageVec<S>
impl<S> !Send for StorageVec<S>
impl<S> !Sync for StorageVec<S>
impl<S> Unpin for StorageVec<S>where
S: Unpin,
impl<S> !UnwindSafe for StorageVec<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more