pub struct IterableMap<K, V>{ /* private fields */ }Expand description
IterableMap is a contract-level data structure to provide abstraction by utilizing Get and Set operations associated with Contract Storage. It supports lazy read/write on key-value tuples which can also be iterated as a vector.
§IterableMap
IterableMap can be a Contract Field defined in the contract struct. E.g.
#[contract]
struct MyContract {
iterable_map: IterableMap<K, V>,
}§Storage Model
Account Storage State Key Format:
| Component | Key | Value (Data type) |
|---|---|---|
| Map Info | P, 0 | MapInfoCell |
| Key-Index | P, 1, L, K | KeyIndexCell |
| Index-Key | P, 2, L, I | ValueCell (data: K) |
| Index-Value | P, 3, L, I | ValueCell |
- P: parent key
- L: map level
- I: little endian bytes of index (u32)
- K: user defined key
§Lazy Write
Trait Storage implements the IterableMap so that data can be saved to world state
- after execution of action method with receiver
&mut self; or - explicitly calling the setter
Self::set().
Implementations§
Source§impl<K, V> IterableMap<K, V>
impl<K, V> IterableMap<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Instantiate new instance of IterableMap. It does not interact with world state if it is not inserted into
contract field.
§Example
let nested_map: IterableMap<String, u64> = IterableMap::new();
self.iterable_map.insert(&"nested_map".to_string(), nested_map);Sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Get data either from cached value or world state.
§Example
match self.get(key) {
Some(value) => {
log("GET".as_bytes(), format!("value = {}", value).as_bytes());
},
None => {
log("GET".as_bytes(), "key not found".as_bytes());
}
}Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Get data as mutable reference to Iterable either from cached value or world state.
§Example
match self.iterable_map.get_mut(key) {
Some(value) => {
// value is mutable reference.
*value += 1;
// the change will be updated to world state after contract method execution
},
None => {}
}Sourcepub fn insert(&mut self, key: &K, value: V) -> Option<&mut V>
pub fn insert(&mut self, key: &K, value: V) -> Option<&mut V>
Insert value to IterableMap. It returns a mutable reference to the inserted value in cache.
§Example
self.iterable_map.insert(key, value);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
clear the map. It performs actual Write to world state.
§Example
// It performs clearance of pending key-value pairs and also world state.
self.iterable_map.clear();
// After this point, no value can be obtained after clear.
self.iterable_map.get(key);Sourcepub fn keys(&self) -> IterableMapKeys<'_, K, V> ⓘ
pub fn keys(&self) -> IterableMapKeys<'_, K, V> ⓘ
Iterator to iterating keys in the map as MapKey. Iterating is a Lazy Read operation.
§Example
self.iterable_map.keys().for_each(|k|{
...
});Sourcepub fn values(&self) -> IterableMapValues<'_, K, V> ⓘ
pub fn values(&self) -> IterableMapValues<'_, K, V> ⓘ
Iterator to iterating values in the map as Iterable. Iterating is a Lazy Read operation.
§Example
self.iterable_map.values().for_each(|v|{
...
});Sourcepub fn values_mut(&mut self) -> IterableMapValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> IterableMapValuesMut<'_, K, V> ⓘ
Mutable Iterator to iterating values in the map as &mut Iterable. Iterating is a Lazy Read operation.
It is expensive operation because the values are expected to save back to storage at the end of contract execution.
§Example
self.iterable_map.values_mut().for_each(|v|{
...
});Trait Implementations§
Source§impl<K, V> BorshDeserialize for IterableMap<K, V>
impl<K, V> BorshDeserialize for IterableMap<K, V>
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self>
Source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
Source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where
R: Read,
Source§impl<K, V> BorshSerialize for IterableMap<K, V>
impl<K, V> BorshSerialize for IterableMap<K, V>
Source§impl<K, V> Clone for IterableMap<K, V>
impl<K, V> Clone for IterableMap<K, V>
Source§fn clone(&self) -> IterableMap<K, V>
fn clone(&self) -> IterableMap<K, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<K, V> Iterable for IterableMap<K, V>
impl<K, V> Iterable for IterableMap<K, V>
Source§impl<K, V> Storable for IterableMap<K, V>
impl<K, V> Storable for IterableMap<K, V>
Source§fn __load_storage(field: &StoragePath) -> Self
fn __load_storage(field: &StoragePath) -> Self
This method is called at the beginning of contract execution, if this IterableMap is a field of the Contract Struct.
Source§fn __save_storage(&mut self, field: &StoragePath)
fn __save_storage(&mut self, field: &StoragePath)
This method is called at the end of contract execution, if this IterableMap is a field of the Contract Struct.