pub struct VecMapper<SA, T, A = CurrentStorage>{ /* 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, A> VecMapper<SA, T, A>
impl<SA, T, A> VecMapper<SA, T, A>
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>(self, index: usize, or_else: F) -> Twhere
F: FnOnce() -> T,
pub fn get_or_else<F>(self, index: usize, or_else: F) -> Twhere
F: FnOnce() -> 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(&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_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 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.
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 set(&mut self, index: usize, item: &T)
pub fn set(&mut self, index: usize, item: &T)
Set item at index in storage. Index must be valid (1 <= index <= count).
Sourcepub fn update<R, F>(&mut self, index: usize, f: F) -> R
pub fn update<R, F>(&mut self, index: usize, f: F) -> R
Syntactic sugar, to more compactly express a get, update and set in one line. Takes whatever lies in storage, apples the given closure and saves the final value back to storage. Propagates the return value of the given function.
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.
Trait Implementations§
Source§impl<'a, SA, T, A> IntoIterator for &'a VecMapper<SA, T, A>
impl<'a, SA, T, A> IntoIterator for &'a VecMapper<SA, T, A>
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>) -> VecMapper<SA, T>
fn new(base_key: StorageKey<SA>) -> VecMapper<SA, T>
#[storage_mapper]
annotation generated code.Source§impl<SA, T> StorageMapperFromAddress<SA> for VecMapper<SA, T, ManagedAddress<SA>>
impl<SA, T> StorageMapperFromAddress<SA> for VecMapper<SA, T, ManagedAddress<SA>>
Source§fn new_from_address(
address: ManagedAddress<SA>,
base_key: StorageKey<SA>,
) -> VecMapper<SA, T, ManagedAddress<SA>>
fn new_from_address( address: ManagedAddress<SA>, base_key: StorageKey<SA>, ) -> VecMapper<SA, T, ManagedAddress<SA>>
#[storage_mapper_from_address]
annotation generated code.Source§impl<SA, T> TopEncodeMulti for VecMapper<SA, T>
Behaves like a MultiResultVec when an endpoint result.
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 as EncodeErrorHandler>::HandledErr>where
O: TopEncodeMultiOutput,
H: EncodeErrorHandler,
fn multi_encode_or_handle_err<O, H>(
&self,
output: &mut O,
h: H,
) -> Result<(), <H as EncodeErrorHandler>::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>
Behaves like a MultiResultVec when an endpoint result.
impl<SA, T> TypeAbi for VecMapper<SA, T>
Behaves like a MultiResultVec when an endpoint result.
type Unmanaged = VecMapper<SA, T>
fn type_name() -> String
fn type_name_rust() -> String
Source§fn provide_type_descriptions<TDC>(accumulator: &mut TDC)where
TDC: TypeDescriptionContainer,
fn provide_type_descriptions<TDC>(accumulator: &mut TDC)where
TDC: TypeDescriptionContainer,
fn type_names() -> TypeNames
impl<SA, T> TypeAbiFrom<VecMapper<SA, T>> for MultiValueEncoded<SA, T>
impl<SA, T> TypeAbiFrom<VecMapper<SA, T>> for VecMapper<SA, T>
Auto Trait Implementations§
impl<SA, T, A> Freeze for VecMapper<SA, T, A>
impl<SA, T, A> RefUnwindSafe for VecMapper<SA, T, A>where
A: RefUnwindSafe,
SA: RefUnwindSafe,
T: RefUnwindSafe,
<SA as HandleTypeInfo>::ManagedBufferHandle: RefUnwindSafe,
impl<SA, T, A> Send for VecMapper<SA, T, A>
impl<SA, T, A> Sync for VecMapper<SA, T, A>
impl<SA, T, A> Unpin for VecMapper<SA, T, A>
impl<SA, T, A> UnwindSafe for VecMapper<SA, T, A>where
A: UnwindSafe,
SA: UnwindSafe,
T: UnwindSafe,
<SA as HandleTypeInfo>::ManagedBufferHandle: UnwindSafe,
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> InterpretableFrom<T> for T
impl<T> InterpretableFrom<T> for T
fn interpret_from(from: T, _context: &InterpreterContext) -> T
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