Struct dharitri_wasm::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 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_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_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(&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 set(&self, index: usize, item: &T)
pub fn set(&self, index: usize, item: &T)
Get item at index from 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<SA, T> EndpointResult for VecMapper<SA, T>
impl<SA, T> EndpointResult for VecMapper<SA, T>
Behaves like a MultiResultVec when an endpoint result.
§type DecodeAs = MultiArgVec<<T as EndpointResult>::DecodeAs>
type DecodeAs = MultiArgVec<<T as EndpointResult>::DecodeAs>
Self for most types.fn finish<FA>(&self)where
FA: ManagedTypeApi + EndpointFinishApi,
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> TypeAbi for VecMapper<SA, T>
impl<SA, T> TypeAbi for VecMapper<SA, T>
Behaves like a MultiResultVec when an endpoint result.