IterableMap

Struct IterableMap 

Source
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:

ComponentKeyValue (Data type)
Map InfoP, 0MapInfoCell
Key-IndexP, 1, L, KKeyIndexCell
Index-KeyP, 2, L, IValueCell (data: K)
Index-ValueP, 3, L, IValueCell
  • 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

  1. after execution of action method with receiver &mut self; or
  2. explicitly calling the setter Self::set().

Implementations§

Source§

impl<K, V> IterableMap<K, V>

Source

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);
Source

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());
   }
}
Source

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 => {}
}
Source

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);
Source

pub fn remove(&mut self, key: &K)

Remove key from IterableMap.

Source

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);
Source

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|{
    ...
});
Source

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|{
    ...
});
Source

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>

Source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self>

Source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
Source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
Source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

Source§

impl<K, V> BorshSerialize for IterableMap<K, V>

Source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<()>

Source§

fn try_to_vec(&self) -> Result<Vec<u8>, Error>

Serialize this instance into a vector of bytes.
Source§

impl<K, V> Clone for IterableMap<K, V>

Source§

fn clone(&self) -> IterableMap<K, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K, V> Iterable for IterableMap<K, V>

Source§

fn save(&mut self, key: Vec<u8>)

Source§

fn is_map(key: Vec<u8>) -> bool

Source§

fn load(key: Vec<u8>) -> Option<Self>

Source§

fn delete(key: Vec<u8>)

Source§

impl<K, V> Storable for IterableMap<K, V>

Source§

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)

This method is called at the end of contract execution, if this IterableMap is a field of the Contract Struct.

Auto Trait Implementations§

§

impl<K, V> Freeze for IterableMap<K, V>

§

impl<K, V> RefUnwindSafe for IterableMap<K, V>

§

impl<K, V> Send for IterableMap<K, V>
where V: Send, K: Send,

§

impl<K, V> Sync for IterableMap<K, V>
where V: Sync, K: Sync,

§

impl<K, V> Unpin for IterableMap<K, V>

§

impl<K, V> UnwindSafe for IterableMap<K, V>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V