pub struct UserMapper<SA, A = CurrentStorage>where
SA: StorageMapperApi,
A: StorageAddress<SA>,{ /* private fields */ }
Expand description
Very widely used mapper, that manages the users of a smart contract. It holds a bi-directional map, from addresses to ids and viceversa. This is so we can easily iterate over all users, using their ids. Also holds the user count in sync. This is also necessary for iteration.
This particular implementation of a user mapper doesn’t contain any additional user data other than address/id.
It also doesn’t allow removing users. Once in, their ids are reserved forever.
Implementations§
Source§impl<SA, A> UserMapper<SA, A>where
SA: StorageMapperApi,
A: StorageAddress<SA>,
impl<SA, A> UserMapper<SA, A>where
SA: StorageMapperApi,
A: StorageAddress<SA>,
Sourcepub fn get_user_id(&self, address: &ManagedAddress<SA>) -> usize
pub fn get_user_id(&self, address: &ManagedAddress<SA>) -> usize
Yields the user id for a given address. Will return 0 if the address is not known to the contract.
Sourcepub fn get_user_address(&self, id: usize) -> Option<ManagedAddress<SA>>
pub fn get_user_address(&self, id: usize) -> Option<ManagedAddress<SA>>
Yields the user address for a given id, if the id is valid.
Sourcepub fn get_user_address_unchecked(&self, id: usize) -> ManagedAddress<SA>
pub fn get_user_address_unchecked(&self, id: usize) -> ManagedAddress<SA>
Yields the user address for a given id. Will cause a deserialization error if the id is invalid.
Sourcepub fn get_user_address_or_zero(&self, id: usize) -> ManagedAddress<SA>
pub fn get_user_address_or_zero(&self, id: usize) -> ManagedAddress<SA>
Yields the user address for a given id, if the id is valid. Otherwise returns the zero address (0x000…)
Sourcepub fn get_user_count(&self) -> usize
pub fn get_user_count(&self) -> usize
Number of users.
Sourcepub fn get_all_addresses(&self) -> ManagedVec<SA, ManagedAddress<SA>>
pub fn get_all_addresses(&self) -> ManagedVec<SA, ManagedAddress<SA>>
Loads all addresses from storage and places them in a ManagedVec. Can easily consume a lot of gas.
Source§impl<SA> UserMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
impl<SA> UserMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
Sourcepub fn get_or_create_users<AddressIter, F>(
&self,
address_iter: AddressIter,
user_id_lambda: F,
)
pub fn get_or_create_users<AddressIter, F>( &self, address_iter: AddressIter, user_id_lambda: F, )
Tries to insert a number of addresses. Calls a lambda function for each, with the new user id and whether of nor the user was already present.
Sourcepub fn get_or_create_user(&self, address: &ManagedAddress<SA>) -> usize
pub fn get_or_create_user(&self, address: &ManagedAddress<SA>) -> usize
Yields the user id for a given address, or creates a new user id if there isn’t one. Will safely keep the user count in sync.
Trait Implementations§
Source§impl<SA> StorageMapper<SA> for UserMapper<SA>where
SA: StorageMapperApi,
impl<SA> StorageMapper<SA> for UserMapper<SA>where
SA: StorageMapperApi,
Source§fn new(base_key: StorageKey<SA>) -> Self
fn new(base_key: StorageKey<SA>) -> Self
#[storage_mapper]
annotation generated code.Source§impl<SA> StorageMapperFromAddress<SA> for UserMapper<SA, ManagedAddress<SA>>where
SA: StorageMapperApi,
impl<SA> StorageMapperFromAddress<SA> for UserMapper<SA, ManagedAddress<SA>>where
SA: StorageMapperApi,
Source§fn new_from_address(
address: ManagedAddress<SA>,
base_key: StorageKey<SA>,
) -> Self
fn new_from_address( address: ManagedAddress<SA>, base_key: StorageKey<SA>, ) -> Self
#[storage_mapper_from_address]
annotation generated code.Source§impl<SA> TopEncodeMulti for UserMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
Behaves like a MultiResultVec
when an endpoint result,
and lists all users addresses.
impl<SA> TopEncodeMulti for UserMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
Behaves like a MultiResultVec
when an endpoint result, and lists all users addresses.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> TypeAbi for UserMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
Behaves like a MultiResultVec when an endpoint result.
impl<SA> TypeAbi for UserMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
Behaves like a MultiResultVec when an endpoint result.