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
sourceimpl<K, V> LookupMap<K, V>
impl<K, V> LookupMap<K, V>
sourcepub fn new<S>(key_prefix: S) -> Self where
S: IntoStorageKey,
pub fn new<S>(key_prefix: S) -> Self where
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.
sourceimpl<K, V> LookupMap<K, V> where
K: BorshSerialize,
V: BorshSerialize + BorshDeserialize,
impl<K, V> LookupMap<K, V> where
K: BorshSerialize,
V: BorshSerialize + BorshDeserialize,
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());
Trait Implementations
sourceimpl<K, V> BorshDeserialize for LookupMap<K, V> where
Vec<u8>: BorshDeserialize,
impl<K, V> BorshDeserialize for LookupMap<K, V> where
Vec<u8>: BorshDeserialize,
sourceimpl<K, V> BorshSerialize for LookupMap<K, V> where
Vec<u8>: BorshSerialize,
impl<K, V> BorshSerialize for LookupMap<K, V> where
Vec<u8>: BorshSerialize,
sourceimpl<K, V> Debug for LookupMap<K, V> where
K: Debug + BorshSerialize,
V: Debug + BorshSerialize + BorshDeserialize,
impl<K, V> Debug for LookupMap<K, V> where
K: Debug + BorshSerialize,
V: Debug + BorshSerialize + BorshDeserialize,
Auto Trait Implementations
impl<K, V> RefUnwindSafe for LookupMap<K, V> where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for LookupMap<K, V> where
K: Send,
V: Send,
impl<K, V> Sync for LookupMap<K, V> where
K: Sync,
V: Sync,
impl<K, V> Unpin for LookupMap<K, V> where
K: Unpin,
V: Unpin,
impl<K, V> UnwindSafe for LookupMap<K, V> where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more