FastMap

Struct FastMap 

Source
pub struct FastMap<K, V>{ /* 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:

ComponentKeyValue (Data type)
Key-ValueP, E, KCell
  • 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

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

Implementations§

Source§

impl<K, V> FastMap<K, V>

Source

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

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

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

pub fn insert(&mut self, key: &K, value: V)

Insert data to the cache of the FastMap. The value will be stored to world state after contract execution.

§Example
self.fast_map.insert(key, value);
Source

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

Remove key from FastMap. The delete will take effective to world state after contract execution.

§Example
self.fast_map.remove(key);

Trait Implementations§

Source§

impl<K, V> BorshDeserialize for FastMap<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 FastMap<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> Insertable for FastMap<K, V>

Source§

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

Save to world state by FastMap’s storage model

Source§

fn edition(key: &[u8]) -> u32

Source§

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

Source§

fn delete(key: Vec<u8>)

Source§

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

Source§

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)

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<K, V> UnwindSafe for FastMap<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> 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, 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