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>

source

pub fn is_empty(&self) -> bool

Returns true if the collection contains no elements.

source

pub fn len(&self) -> usize

Gets the number of elements stored.

source

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.

source

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.

source

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.

source

pub fn get(&self, index: impl TryInto<usize>) -> Option<S::Wraps<'_>>

Gets the element at the given index, if it exists.

source

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.

source

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);
source

pub fn shrink(&mut self) -> Option<StorageGuardMut<'_, S>>

Removes and returns an accessor to the last element of the vector, if any.

source

pub fn truncate(&mut self, len: usize)

Shortens the vector, keeping the first len elements.

Note: this method does not erase any underlying storage.

source§

impl<'a, S: SimpleStorageType<'a>> StorageVec<S>

source

pub fn push(&mut self, value: S::Wraps<'a>)

Adds an element to the end of the vector.

source

pub fn pop(&mut self) -> Option<S::Wraps<'a>>

Removes and returns the last element of the vector, if it exists.

Note: the underlying storage slot is erased when all elements in a word are freed.

source§

impl<S: Erase> StorageVec<S>

source

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>

source§

fn erase(&mut self)

Erase the value from persistent storage.
source§

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)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<S: StorageType> StorageType for StorageVec<S>

§

type Wraps<'a> = StorageGuard<'a, StorageVec<S>> where Self: 'a

For primitive types, this is the type being stored. For collections, this is the StorageType being collected.
§

type WrapsMut<'a> = StorageGuardMut<'a, StorageVec<S>> where Self: 'a

Mutable accessor to the type being stored.
source§

unsafe fn new(slot: U256, offset: u8) -> Self

Where in persistent storage the type should live. Although useful for framework designers creating new storage types, most user programs shouldn’t call this. Note: implementations will have to be const once generic_const_exprs stabilizes. Read more
source§

fn load<'s>(self) -> Self::Wraps<'s>

Load the wrapped type, consuming the accessor. Note: most types have a get and/or getter, which don’t consume Self.
source§

fn load_mut<'s>(self) -> Self::WrapsMut<'s>

Load the wrapped mutable type, consuming the accessor. Note: most types have a set and/or setter, which don’t consume Self.
source§

const SLOT_BYTES: usize = 32usize

The number of bytes in a slot needed to represent the type. Must not exceed 32. For types larger than 32 bytes that are stored inline with a struct’s fields, set this to 32 and return the full size in StorageType::new. Read more
source§

const REQUIRED_SLOTS: usize = 0usize

The number of words this type must fill. For primitives this is always 0. For complex types requiring more than one inline word, set this to the total size.

Auto Trait Implementations§

§

impl<S> !Freeze for StorageVec<S>

§

impl<S> !RefUnwindSafe for StorageVec<S>

§

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

§

impl<S> !Sync for StorageVec<S>

§

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

§

impl<S> UnwindSafe for StorageVec<S>
where 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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.