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>

source

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");
source

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

pub fn remove_raw(&mut self, key_raw: &[u8]) -> Option<Vec<u8>>

Removes a serialized key from the map, returning the serialized value at the key if the key was previously in the map.

source§

impl<K, V> LookupMap<K, V>

source

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);
source

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()));
source

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()));
source

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()));
source

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>

source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>

source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

source§

impl<K, V> BorshSerialize for LookupMap<K, V>

source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error>

source§

impl<K, V> Debug for LookupMap<K, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for LookupMap<K, V>

§

impl<K, V> RefUnwindSafe for LookupMap<K, V>

§

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.