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>where
SA: StorageMapperApi,
impl<SA> UserMapper<SA>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>) -> UserMapper<SA>
fn new(base_key: StorageKey<SA>) -> UserMapper<SA>
#[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>,
) -> UserMapper<SA, ManagedAddress<SA>>
fn new_from_address( address: ManagedAddress<SA>, base_key: StorageKey<SA>, ) -> UserMapper<SA, ManagedAddress<SA>>
#[storage_mapper_from_address]
annotation generated code.Source§impl<SA> TopEncodeMulti for UserMapper<SA>where
SA: StorageMapperApi,
Behaves like a MultiResultVec
when an endpoint result,
and lists all users addresses.
impl<SA> TopEncodeMulti for UserMapper<SA>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 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> TypeAbi for UserMapper<SA>where
SA: StorageMapperApi,
Behaves like a MultiResultVec when an endpoint result.
impl<SA> TypeAbi for UserMapper<SA>where
SA: StorageMapperApi,
Behaves like a MultiResultVec when an endpoint result.
type Unmanaged = UserMapper<SA>
fn type_name() -> String
fn type_name_rust() -> String
fn type_names() -> TypeNames
Source§fn provide_type_descriptions<TDC>(accumulator: &mut TDC)where
TDC: TypeDescriptionContainer,
fn provide_type_descriptions<TDC>(accumulator: &mut TDC)where
TDC: TypeDescriptionContainer,
impl<SA> TypeAbiFrom<UserMapper<SA>> for MultiValueEncoded<SA, ManagedAddress<SA>>where
SA: StorageMapperApi,
impl<SA> TypeAbiFrom<UserMapper<SA>> for UserMapper<SA>where
SA: StorageMapperApi,
Auto Trait Implementations§
impl<SA, A> Freeze for UserMapper<SA, A>
impl<SA, A> RefUnwindSafe for UserMapper<SA, A>where
A: RefUnwindSafe,
SA: RefUnwindSafe,
<SA as HandleTypeInfo>::ManagedBufferHandle: RefUnwindSafe,
impl<SA, A> Send for UserMapper<SA, A>
impl<SA, A> Sync for UserMapper<SA, A>
impl<SA, A> Unpin for UserMapper<SA, A>
impl<SA, A> UnwindSafe for UserMapper<SA, A>
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> 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