Module near_sdk::collections

source ·
Expand description

Collections that offer an alternative to standard containers from std::collections::* by utilizing the underlying blockchain trie storage more efficiently.

The updated version of this module lives in near_sdk::store, where the data structures are more optimized and have a closer API to std::collections.

For example, the following smart contract does not work with state efficiently, because it will load the entire HashMap at the beginning of the contract call, and will save it entirely at the end, in cases when there is state modification. This is fine for small number of elements, but very inefficient for large numbers.


#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct StatusMessage {
   records: HashMap<String, String>,
}

The following is an efficient alternative. It will load each element individually only when it is read and will save it only when it is written/removed.


#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct StatusMessage {
   records: LookupMap<String, String>,
}

The efficiency of LookupMap comes at the cost, since it has fewer methods than HashMap and is not that seemlessly integrated with the rest of the Rust standard library.

Re-exports§

Modules§

  • A map implemented on a trie. Unlike std::collections::HashMap the keys in this map are not hashed but are instead serialized.
  • A vector implemented on a trie. Unlike standard vector does not support insertion and removal of an element results in the last element being placed in the empty position.

Structs§

  • An persistent lazy option, that stores a value in the storage.
  • LegacyTreeMapDeprecated
    TreeMap based on AVL-tree
  • An non-iterable implementation of a map that stores its content directly on the trie.
  • A non-iterable implementation of a set that stores its content directly on the storage trie.
  • TreeMap based on AVL-tree
  • An iterable implementation of a set that stores its content directly on the trie.

Constants§