pub struct UnorderedMap<K, V> { /* private fields */ }
Expand description

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

Implementations§

source§

impl<K, V> UnorderedMap<K, V>

source

pub fn len(&self) -> u64

Returns the number of elements in the map, also referred to as its size.

§Examples
use near_sdk::collections::UnorderedMap;

let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
assert_eq!(map.len(), 0);
map.insert(&1, &1);
map.insert(&2, &2);
assert_eq!(map.len(), 2);
source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

source

pub fn new<S>(prefix: S) -> Self
where S: IntoStorageKey,

Create new map with zero elements. Use prefix as a unique identifier.

§Examples
use near_sdk::collections::UnorderedMap;
let mut map: UnorderedMap<u8, u8> = UnorderedMap::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> UnorderedMap<K, V>

source

pub fn get(&self, key: &K) -> Option<V>

Returns the value corresponding to the key.

§Examples
use near_sdk::collections::UnorderedMap;

let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
assert_eq!(map.get(&1), None);
map.insert(&1, &10);
assert_eq!(map.get(&1), Some(10));
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::UnorderedMap;

let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
assert_eq!(map.remove(&1), None);
map.insert(&1, &10);
assert_eq!(map.remove(&1), Some(10));
assert_eq!(map.len(), 0);
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::UnorderedMap;

let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
map.insert(&1, &10);
assert_eq!(map.get(&1), Some(10));
assert_eq!(map.len(), 1);
source

pub fn clear(&mut self)

Clears the map, removing all elements.

§Examples
use near_sdk::collections::UnorderedMap;

let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
map.insert(&1, &10);
map.insert(&2, &20);
map.clear();
assert_eq!(map.len(), 0);
source

pub fn to_vec(&self) -> Vec<(K, V)>

Copies elements into an std::vec::Vec.

source

pub fn keys(&self) -> impl Iterator<Item = K> + '_

An iterator visiting all keys. The iterator element type is K.

source

pub fn values(&self) -> impl Iterator<Item = V> + '_

An iterator visiting all values. The iterator element type is V.

source

pub fn iter(&self) -> Iter<'_, K, V>

Iterate over deserialized keys and values.

source

pub fn extend<IT: IntoIterator<Item = (K, V)>>(&mut self, iter: IT)

source

pub fn keys_as_vector(&self) -> &Vector<K>

Returns a view of keys as a vector. It’s sometimes useful to have random access to the keys.

source

pub fn values_as_vector(&self) -> &Vector<V>

Returns a view of values as a vector. It’s sometimes useful to have random access to the values.

Trait Implementations§

source§

impl<K, V> BorshDeserialize for UnorderedMap<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 UnorderedMap<K, V>

source§

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

source§

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

source§

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

Formats the value using the given formatter. Read more
source§

impl<'a, K, V> IntoIterator for &'a UnorderedMap<K, V>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

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

§

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

§

impl<K, V> Send for UnorderedMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for UnorderedMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for UnorderedMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for UnorderedMap<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.