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());
Trait Implementations§
source§impl<K, V> BorshDeserialize for LookupMap<K, V>
impl<K, V> BorshDeserialize for LookupMap<K, V>
fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>
source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where
R: Read,
source§impl<K, V> BorshSerialize for LookupMap<K, V>
impl<K, V> BorshSerialize for LookupMap<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for LookupMap<K, V>
impl<K, V> RefUnwindSafe for LookupMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for LookupMap<K, V>
impl<K, V> Sync for LookupMap<K, V>
impl<K, V> Unpin for LookupMap<K, V>
impl<K, V> UnwindSafe for LookupMap<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more