pub struct UniqueIdMapper<SA, A = CurrentStorage>where
SA: StorageMapperApi,
A: StorageAddress<SA>,{ /* private fields */ }Expand description
A space-optimized storage mapper for managing ID permutations with minimal storage overhead.
§Storage Layout
The UniqueIdMapper is optimized for scenarios where most IDs map to themselves (identity mapping).
It uses a VecMapper internally but only stores non-identity mappings:
base_key + ".len"→ number of slots in the mapperbase_key + ".item" + index→ stored value only ifvalue != index(stores 0 otherwise)
Key Optimization: When Mapper[i] == i (identity mapping), it stores 0 instead of i,
significantly reducing storage costs when most IDs remain in their original positions.
Important: Indexes are 1-based (range: 1..=len()), consistent with VecMapper conventions.
§Main Operations
- Initialize:
set_initial_len(n)- Sets the mapper size (can only be called once). - Read:
get(index)- Returns the ID at the given index. O(1) with one storage read. - Write:
set(index, id)- Assigns an ID to an index. O(1) with one storage write. - Remove:
swap_remove(index)- Removes an index by swapping with the last element. O(1). - Iteration:
iter()- Iterates over all ID values in index order.
§Trade-offs
- Pros: Extremely space-efficient for permutation tracking; zero storage cost for identity mappings.
- Cons: Limited to ID permutations (1 to N); length must be set upfront and cannot be extended.
§Use Cases
- NFT position tracking where most NFTs remain in their original slots
- Shuffle/permutation algorithms where most elements stay in place
- ID reassignment systems with minimal changes
§Example
// Initialize with 5 slots (IDs 1-5)
mapper.set_initial_len(5);
// Initially, all IDs map to themselves: [1, 2, 3, 4, 5]
assert_eq!(mapper.get(1), 1);
assert_eq!(mapper.get(3), 3);
// Swap positions 2 and 4
mapper.set(2, 4);
mapper.set(4, 2);
// Now the mapping is: [1, 4, 3, 2, 5]
assert_eq!(mapper.get(2), 4);
assert_eq!(mapper.get(4), 2);
// Remove index 3 (swaps with last)
let removed = mapper.swap_remove(3);
assert_eq!(removed, 3);
assert_eq!(mapper.len(), 4);Implementations§
Source§impl<SA, A> UniqueIdMapper<SA, A>where
SA: StorageMapperApi,
A: StorageAddress<SA>,
impl<SA, A> UniqueIdMapper<SA, A>where
SA: StorageMapperApi,
A: StorageAddress<SA>,
Source§impl<SA> UniqueIdMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
impl<SA> UniqueIdMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
Sourcepub fn set_initial_len(&mut self, len: usize)
pub fn set_initial_len(&mut self, len: usize)
Initializes the mapper’s length. This may not be set again afterwards.
Sourcepub fn swap_remove(&mut self, index: usize) -> UniqueId
pub fn swap_remove(&mut self, index: usize) -> UniqueId
Gets the value from the index and removes it. The value is replaced by the last item, and length is decremented.
Trait Implementations§
Source§impl<'a, SA, A> IntoIterator for &'a UniqueIdMapper<SA, A>where
SA: StorageMapperApi,
A: StorageAddress<SA>,
impl<'a, SA, A> IntoIterator for &'a UniqueIdMapper<SA, A>where
SA: StorageMapperApi,
A: StorageAddress<SA>,
Source§impl<SA> StorageMapper<SA> for UniqueIdMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
impl<SA> StorageMapper<SA> for UniqueIdMapper<SA, CurrentStorage>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 UniqueIdMapper<SA, ManagedAddress<SA>>where
SA: StorageMapperApi,
impl<SA> StorageMapperFromAddress<SA> for UniqueIdMapper<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 UniqueIdMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
Behaves like a MultiResultVec when an endpoint result.
impl<SA> TopEncodeMulti for UniqueIdMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
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::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 UniqueIdMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
Behaves like a MultiResultVec when an endpoint result.
impl<SA> TypeAbi for UniqueIdMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
Behaves like a MultiResultVec when an endpoint result.