pub struct FastMap<K, V>where
K: BorshSerialize,
V: Insertable,{ /* private fields */ }Expand description
FastMap 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.
§FastMap
FastMap can be a Contract Field defined in the contract struct. E.g.
#[contract]
struct MyContract {
/// This FastMap accepts key and value with borsh-serializable data types.
map: FastMap<String, u64>
}§Storage Model
Account Storage State Key Format:
| Component | Key | Value (Data type) |
|---|---|---|
| Key-Value | P, E, K | Cell |
- P: parent key
- E: little endian bytes of edition number (u32)
- K: user defined key
In account storage state, the key format is parent key + edition (u32, 4 bytes) + user defined key. If nested FastMap is
inserted to FastMap as a value, parent key would be the key of the FastMap being inserted. Actual value to be stored
into world state is borsh-serialized structure of Cell which is either a value (bytes) or information of nested map.
§Lazy Write
Trait Storage implements the FastMap 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> FastMap<K, V>where
K: BorshSerialize,
V: Insertable,
impl<K, V> FastMap<K, V>where
K: BorshSerialize,
V: Insertable,
Sourcepub fn new() -> Self
pub fn new() -> Self
New instance of FastMap detached to world state, which is mainly used for being a nested map as a value of parent FastMap
(by calling insert from parent FastMap). It does not interact with world state if it is not inserted into contract field.
§Example
let fast_map: FastMap<String, u64> = FastMap::new();
self.fast_map.insert(&"fast_map".to_string(), fast_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.fast_map.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 the data that is obtained either from cached value or world state.
§Example
match self.fast_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 => {}
}Trait Implementations§
Source§impl<K, V> BorshDeserialize for FastMap<K, V>where
K: BorshSerialize,
V: Insertable,
impl<K, V> BorshDeserialize for FastMap<K, V>where
K: BorshSerialize,
V: Insertable,
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 FastMap<K, V>where
K: BorshSerialize,
V: Insertable,
impl<K, V> BorshSerialize for FastMap<K, V>where
K: BorshSerialize,
V: Insertable,
Source§impl<K, V> Insertable for FastMap<K, V>where
K: BorshSerialize,
V: Insertable,
impl<K, V> Insertable for FastMap<K, V>where
K: BorshSerialize,
V: Insertable,
Source§impl<K, V> Storable for FastMap<K, V>where
K: BorshSerialize,
V: Insertable,
impl<K, V> Storable for FastMap<K, V>where
K: BorshSerialize,
V: Insertable,
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 FastMap 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 FastMap is a field of the Contract Struct.