Struct near_sdk::collections::LookupMap
source · pub struct LookupMap<K, V> { /* private fields */ }
Expand description
An non-iterable implementation of a map that stores its content directly on the trie.
Implementations§
source§impl<K, V> LookupMap<K, V>
impl<K, V> LookupMap<K, V>
sourcepub fn new<S>(key_prefix: S) -> Selfwhere
S: IntoStorageKey,
pub fn new<S>(key_prefix: S) -> Selfwhere
S: IntoStorageKey,
Create a new map. Use key_prefix
as a unique prefix for keys.
§Examples
use near_sdk::collections::LookupMap;
let mut map: LookupMap<String, String> = LookupMap::new(b"m");
sourcepub fn insert_raw(
&mut self,
key_raw: &[u8],
value_raw: &[u8]
) -> Option<Vec<u8>>
pub fn insert_raw( &mut self, key_raw: &[u8], value_raw: &[u8] ) -> Option<Vec<u8>>
Inserts a serialized key-value pair into the map.
If the map did not have this key present, None
is returned. Otherwise returns
a serialized value. Note, the keys that have the same hash value are undistinguished by
the implementation.
source§impl<K, V> LookupMap<K, V>
impl<K, V> LookupMap<K, V>
sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true if the map contains a given key.
§Examples
use near_sdk::collections::LookupMap;
let mut map: LookupMap<String, String> = LookupMap::new(b"m");
assert_eq!(map.contains_key(&"Toyota".into()), false);
map.insert(&"Toyota".into(), &"Camry".into());
assert_eq!(map.contains_key(&"Toyota".into()), true);
sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Returns the value corresponding to the key.
§Examples
use near_sdk::collections::LookupMap;
let mut map: LookupMap<String, String> = LookupMap::new(b"m");
assert_eq!(map.get(&"Toyota".into()), None);
map.insert(&"Toyota".into(), &"Camry".into());
assert_eq!(map.get(&"Toyota".into()), Some("Camry".into()));
sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a key from the map, returning the value at the key if the key was previously in the map.
§Examples
use near_sdk::collections::LookupMap;
let mut map: LookupMap<String, String> = LookupMap::new(b"m");
assert_eq!(map.remove(&"Toyota".into()), None);
map.insert(&"Toyota".into(), &"Camry".into());
assert_eq!(map.remove(&"Toyota".into()), Some("Camry".into()));
sourcepub fn insert(&mut self, key: &K, value: &V) -> Option<V>
pub fn insert(&mut self, key: &K, value: &V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None
is returned. Otherwise returns
a value. Note, the keys that have the same hash value are undistinguished by
the implementation.
§Examples
use near_sdk::collections::LookupMap;
let mut map: LookupMap<String, String> = LookupMap::new(b"m");
assert_eq!(map.insert(&"Toyota".into(), &"Camry".into()), None);
assert_eq!(map.insert(&"Toyota".into(), &"Corolla".into()), Some("Camry".into()));
sourcepub fn extend<IT: IntoIterator<Item = (K, V)>>(&mut self, iter: IT)
pub fn extend<IT: IntoIterator<Item = (K, V)>>(&mut self, iter: IT)
Inserts all new key-values from the iterator and replaces values with existing keys with new values returned from the iterator.
§Examples
use near_sdk::collections::LookupMap;
let mut extendee: LookupMap<String, String> = LookupMap::new(b"m");
let mut source = vec![];
source.push(("Toyota".into(), "Camry".into()));
source.push(("Nissan".into(), "Almera".into()));
source.push(("Ford".into(), "Mustang".into()));
source.push(("Chevrolet".into(), "Camaro".into()));
extendee.extend(source.into_iter());