Module near_sdk::collections[][src]

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

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

Structs

LegacyTreeMap

TreeMap based on AVL-tree

LookupMap

An non-iterable implementation of a map that stores its content directly on the trie.

LookupSet

An non-iterable implementation of a set that stores its content directly on the trie.

TreeMap

TreeMap based on AVL-tree

UnorderedMap

An iterable implementation of a map that stores its content directly on the trie.

UnorderedSet

An iterable implementation of a set that stores its content directly on the trie.

Vector

An iterable implementation of vector that stores its content on the trie. Uses the following map: index -> element.

Constants

ERR_ELEMENT_DESERIALIZATION
ERR_ELEMENT_SERIALIZATION
ERR_INCONSISTENT_STATE