pub struct UserMapper<SA>
where SA: StorageMapperApi,
{ /* 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> UserMapper<SA>
where SA: StorageMapperApi,

source

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.

source

pub fn get_user_address(&self, id: usize) -> Option<ManagedAddress<SA>>

Yields the user address for a given id, if the id is valid.

source

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.

source

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…)

source

pub fn get_user_count(&self) -> usize

Number of users.

source

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.

source

pub fn get_or_create_users<AddressIter, F>( &self, address_iter: AddressIter, user_id_lambda: F )
where AddressIter: Iterator<Item = ManagedAddress<SA>>, F: FnMut(usize, bool),

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.

source

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.

Trait Implementations§

source§

impl<SA> EndpointResult for UserMapper<SA>
where SA: StorageMapperApi,

Behaves like a MultiResultVec

when an endpoint result, and lists all users addresses.

§

type DecodeAs = MultiArgVec<ManagedAddress<SA>>

Indicates how the result of the endpoint can be interpreted when called via proxy. Self for most types.
source§

fn finish<FA>(&self)

source§

impl<SA> StorageMapper<SA> for UserMapper<SA>
where SA: StorageMapperApi,

source§

fn new(base_key: StorageKey<SA>) -> Self

Will be called automatically by the #[storage_mapper] annotation generated code.
source§

impl<SA> TypeAbi for UserMapper<SA>
where SA: StorageMapperApi,

Behaves like a MultiResultVec when an endpoint result.

source§

fn type_name() -> TypeName

source§

fn provide_type_descriptions<TDC: TypeDescriptionContainer>( accumulator: &mut TDC )

A type can provide more than its own description. For instance, a struct can also provide the descriptions of the type of its fields. TypeAbi doesn’t care for the exact accumulator type, which is abstracted by the TypeDescriptionContainer trait.

Auto Trait Implementations§

§

impl<SA> RefUnwindSafe for UserMapper<SA>
where SA: RefUnwindSafe,

§

impl<SA> Send for UserMapper<SA>
where SA: Send,

§

impl<SA> Sync for UserMapper<SA>
where SA: Sync,

§

impl<SA> Unpin for UserMapper<SA>
where SA: Unpin,

§

impl<SA> UnwindSafe for UserMapper<SA>
where SA: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.