Struct multiversx_sc::storage::mappers::VecMapper
source · pub struct VecMapper<SA, T>{ /* private fields */ }
Expand description
Manages a list of items of the same type. Saves each of the items under a separate key in storage. To produce each individual key, it concatenates the main key with a serialized 4-byte index. Indexes start from 1, instead of 0. (We avoid 0-value indexes to prevent confusion between an uninitialized variable and zero.) It also stores the count separately, at what would be index 0. The count is always kept in sync automatically.
Implementations§
source§impl<SA, T> VecMapper<SA, T>
impl<SA, T> VecMapper<SA, T>
sourcepub fn len_at_address(&self, address: &ManagedAddress<SA>) -> usize
pub fn len_at_address(&self, address: &ManagedAddress<SA>) -> usize
Number of items in the mapper at the given address.
pub fn is_empty_at_address(&self, address: &ManagedAddress<SA>) -> bool
sourcepub fn push(&mut self, item: &T) -> usize
pub fn push(&mut self, item: &T) -> usize
Add one item at the end of the list. Returns the index of the newly inserted item, which is also equal to the new number of elements.
sourcepub fn extend_from_slice(&mut self, items: &[T]) -> usize
pub fn extend_from_slice(&mut self, items: &[T]) -> usize
Adds multiple items at the end of the list.
Cheaper than multiple push
-es because the count only gets updated once at the end.
Returns the index of the last inserted item, which is also equal to the new number of elements.
sourcepub fn get(&self, index: usize) -> T
pub fn get(&self, index: usize) -> T
Get item at index from storage. Index must be valid (1 <= index <= count).
sourcepub fn get_at_address(&self, address: &ManagedAddress<SA>, index: usize) -> T
pub fn get_at_address(&self, address: &ManagedAddress<SA>, index: usize) -> T
Get the item at index from the target’s storage. Index must be valid (1 <= index <= count).
sourcepub fn get_unchecked(&self, index: usize) -> T
pub fn get_unchecked(&self, index: usize) -> T
Get item at index from storage. There are no restrictions on the index, calling for an invalid index will simply return the zero-value.
sourcepub fn get_unchecked_at_address(
&self,
address: &ManagedAddress<SA>,
index: usize
) -> T
pub fn get_unchecked_at_address( &self, address: &ManagedAddress<SA>, index: usize ) -> T
Gets the item without checking index bounds.
Prefer using get_at_address
instead.
sourcepub fn get_or_else<F: FnOnce() -> T>(self, index: usize, or_else: F) -> T
pub fn get_or_else<F: FnOnce() -> T>(self, index: usize, or_else: F) -> T
Get item at index from storage. If index is valid (1 <= index <= count), returns value at index, else calls lambda given as argument. The lambda only gets called lazily if the index is not valid.
sourcepub fn item_is_empty_unchecked(&self, index: usize) -> bool
pub fn item_is_empty_unchecked(&self, index: usize) -> bool
Checks whether or not there is anything in storage at index.
There are no restrictions on the index,
calling for an invalid index will simply return true
.
sourcepub fn item_is_empty_unchecked_at_address(
&self,
address: &ManagedAddress<SA>,
index: usize
) -> bool
pub fn item_is_empty_unchecked_at_address( &self, address: &ManagedAddress<SA>, index: usize ) -> bool
Checks if the mapper at the given address stores anything at this index.
Does not check index bounds.
Prefer using item_is_empty
instead.
sourcepub fn item_is_empty(&self, index: usize) -> bool
pub fn item_is_empty(&self, index: usize) -> bool
Checks whether or not there is anything ins storage at index. Index must be valid (1 <= index <= count).
sourcepub fn item_is_empty_at_address(
&self,
address: &ManagedAddress<SA>,
index: usize
) -> bool
pub fn item_is_empty_at_address( &self, address: &ManagedAddress<SA>, index: usize ) -> bool
Checks if the mapper at the given address stores anything at this index. Index must be valid (1 <= index <= count).
sourcepub fn set(&self, index: usize, item: &T)
pub fn set(&self, index: usize, item: &T)
Set item at index in storage. Index must be valid (1 <= index <= count).
sourcepub fn clear_entry(&self, index: usize)
pub fn clear_entry(&self, index: usize)
Clears item at index from storage. Index must be valid (1 <= index <= count).
sourcepub fn clear_entry_unchecked(&self, index: usize)
pub fn clear_entry_unchecked(&self, index: usize)
Clears item at index from storage. There are no restrictions on the index, calling for an invalid index will simply do nothing.
sourcepub fn swap_remove(&mut self, index: usize)
pub fn swap_remove(&mut self, index: usize)
Clears item at index from storage by swap remove last item takes the index of the item to remove and we remove the last index.
sourcepub fn load_as_vec(&self) -> Vec<T>
pub fn load_as_vec(&self) -> Vec<T>
Loads all items from storage and places them in a Vec. Can easily consume a lot of gas.
Trait Implementations§
source§impl<'a, SA, T> IntoIterator for &'a VecMapper<SA, T>
impl<'a, SA, T> IntoIterator for &'a VecMapper<SA, T>
source§impl<SA, T> StorageClearable for VecMapper<SA, T>
impl<SA, T> StorageClearable for VecMapper<SA, T>
source§impl<SA, T> StorageMapper<SA> for VecMapper<SA, T>
impl<SA, T> StorageMapper<SA> for VecMapper<SA, T>
source§fn new(base_key: StorageKey<SA>) -> Self
fn new(base_key: StorageKey<SA>) -> Self
#[storage_mapper]
annotation generated code.source§impl<SA, T> TopEncodeMulti for VecMapper<SA, T>
impl<SA, T> TopEncodeMulti for VecMapper<SA, T>
Behaves like a MultiResultVec when an endpoint result.
source§fn multi_encode_or_handle_err<O, H>(
&self,
output: &mut O,
h: H
) -> Result<(), H::HandledErr>where
O: TopEncodeMultiOutput,
H: EncodeErrorHandler,
fn multi_encode_or_handle_err<O, H>(
&self,
output: &mut O,
h: H
) -> Result<(), H::HandledErr>where
O: TopEncodeMultiOutput,
H: EncodeErrorHandler,
top_encode
that can handle errors as soon as they occur.
For instance in can exit immediately and make sure that if it returns, it is a success.
By not deferring error handling, this can lead to somewhat smaller bytecode.source§fn multi_encode<O>(&self, output: &mut O) -> Result<(), EncodeError>where
O: TopEncodeMultiOutput,
fn multi_encode<O>(&self, output: &mut O) -> Result<(), EncodeError>where
O: TopEncodeMultiOutput,
source§impl<SA, T> TypeAbi for VecMapper<SA, T>
impl<SA, T> TypeAbi for VecMapper<SA, T>
Behaves like a MultiResultVec when an endpoint result.