Struct soroban_sdk::Map

source ·
pub struct Map<K, V> { /* private fields */ }
Expand description

Map is a ordered key-value dictionary.

The map is ordered by its keys. Iterating a map is stable and always returns the keys and values in order of the keys.

The map is stored in the Host and available to the Guest through the functions defined on Map. Values stored in the Map are transmitted to the Host as Vals, and when retrieved from the Map are transmitted back and converted from Val back into their type.

The pairs of keys and values in a Map are not guaranteed to be of type K/V and conversion will fail if they are not. Most functions on Map return a Result due to this.

There are some cases where this lack of guarantee is important:

  • When storing a Map that has been provided externally as a contract function argument, be aware there is no guarantee that all pairs in the Map will be of type K and V. It may be necessary to validate all pairs, either before storing, or when loading with try_ variation functions.

  • When accessing and iterating over a Map that has been provided externally as a contract function input, and the contract needs to be resilient to failure, use the try_ variation functions.

Maps have at most one entry per key. Setting a value for a key in the map that already has a value for that key replaces the value.

Map values can be stored as Storage, or in other types like Vec, Map, etc.

Examples

Maps can be created and iterated.

use soroban_sdk::{Env, Map, map};

let env = Env::default();
let map = map![&env, (2, 20), (1, 10)];
assert_eq!(map.len(), 2);
assert_eq!(map.iter().next(), Some((1, 10)));

Maps are ordered and so maps created with elements in different order will be equal.

use soroban_sdk::{Env, Map, map};

let env = Env::default();
assert_eq!(
    map![&env, (1, 10), (2, 20)],
    map![&env, (2, 20), (1, 10)],
)

Implementations§

source§

impl<K, V> Map<K, V>

source

pub fn env(&self) -> &Env

source

pub fn as_val(&self) -> &Val

source

pub fn to_val(&self) -> Val

source§

impl<K, V> Map<K, V>

source

pub fn new(env: &Env) -> Map<K, V>

Create an empty Map.

source

pub fn from_array<const N: usize>(env: &Env, items: [(K, V); N]) -> Map<K, V>

Create a Map from the key-value pairs in the array.

source

pub fn contains_key(&self, k: K) -> bool

Returns true if a key-value pair exists in the map with the given key.

source

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

Returns the value corresponding to the key or None if the map does not contain a value with the specified key.

Panics

If the value corresponding to the key cannot be converted to type V.

source

pub fn try_get(&self, k: K) -> Result<Option<V>, V::Error>

Returns the value corresponding to the key or None if the map does not contain a value with the specified key.

Errors

If the value corresponding to the key cannot be converted to type V.

source

pub fn get_unchecked(&self, k: K) -> V

Returns the value corresponding to the key.

Panics

If the map does not contain a value with the specified key.

If the value corresponding to the key cannot be converted to type V.

source

pub fn try_get_unchecked(&self, k: K) -> Result<V, V::Error>

Returns the value corresponding to the key.

Errors

If the value corresponding to the key cannot be converted to type V.

Panics

If the map does not contain a value with the specified key.

source

pub fn set(&mut self, k: K, v: V)

Set the value for the specified key.

If the map contains a value corresponding to the key, the value is replaced with the given value.

source

pub fn remove(&mut self, k: K) -> Option<()>

Remove the value corresponding to the key.

Returns None if the map does not contain a value with the specified key.

source

pub fn remove_unchecked(&mut self, k: K)

Remove the value corresponding to the key.

Panics

If the map does not contain a value with the specified key.

source

pub fn keys(&self) -> Vec<K>

Returns a Vec of all keys in the map.

source

pub fn values(&self) -> Vec<V>

Returns a Vec of all values in the map.

source§

impl<K, V> Map<K, V>

source

pub fn is_empty(&self) -> bool

Returns true if the map is empty and contains no key-values.

source

pub fn len(&self) -> u32

Returns the number of key-value pairs in the map.

source§

impl<K, V> Map<K, V>

source

pub fn iter(&self) -> UnwrappedIter<MapTryIter<K, V>, (K, V), ConversionError>
where K: Clone, V: Clone,

source

pub fn try_iter(&self) -> MapTryIter<K, V>

source

pub fn into_try_iter(self) -> MapTryIter<K, V>

Trait Implementations§

source§

impl<K: Clone, V: Clone> Clone for Map<K, V>

source§

fn clone(&self) -> Map<K, V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

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

source§

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

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

impl<K, V> From<Map<K, V>> for Val

source§

fn from(m: Map<K, V>) -> Self

Converts to this type from the input type.
source§

impl<K, V> IntoIterator for Map<K, V>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = UnwrappedIter<MapTryIter<K, V>, (K, V), ConversionError>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K, V> Ord for Map<K, V>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<K, V> PartialEq for Map<K, V>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, V> PartialOrd for Map<K, V>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<K, V> SorobanArbitrary for Map<K, V>

Available on crate feature testutils only.
§

type Prototype = ArbitraryMap<<K as SorobanArbitrary>::Prototype, <V as SorobanArbitrary>::Prototype>

A type that implements Arbitrary and can be converted to this SorobanArbitrary type.
source§

impl<K, V> TryFrom<&Map<K, V>> for ScVal

§

type Error = ConversionError

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

fn try_from(v: &Map<K, V>) -> Result<Self, ConversionError>

Performs the conversion.
source§

impl<K, V> TryFrom<Map<K, V>> for ScVal

§

type Error = ConversionError

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

fn try_from(v: Map<K, V>) -> Result<Self, ConversionError>

Performs the conversion.
source§

impl<K, V> TryFromVal<Env, Map<K, V>> for ScVal

source§

impl<K, V> TryFromVal<Env, Map<K, V>> for Val

§

type Error = Infallible

source§

fn try_from_val(_env: &Env, v: &Map<K, V>) -> Result<Self, Self::Error>

source§

impl<K, V> TryFromVal<Env, MapObject> for Map<K, V>

§

type Error = Infallible

source§

fn try_from_val(env: &Env, obj: &MapObject) -> Result<Self, Self::Error>

source§

impl<K, V> TryFromVal<Env, ScVal> for Map<K, V>

§

type Error = ConversionError

source§

fn try_from_val(env: &Env, val: &ScVal) -> Result<Self, Self::Error>

source§

impl<K, V> TryFromVal<Env, Val> for Map<K, V>

§

type Error = ConversionError

source§

fn try_from_val(env: &Env, val: &Val) -> Result<Self, Self::Error>

source§

impl<K, V> Eq for Map<K, V>

Auto Trait Implementations§

§

impl<K, V> !RefUnwindSafe for Map<K, V>

§

impl<K, V> !Send for Map<K, V>

§

impl<K, V> !Sync for Map<K, V>

§

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

§

impl<K, V> !UnwindSafe for Map<K, V>

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
§

impl<T, C> Compare<&T> for C
where C: Compare<T>,

§

type Error = <C as Compare<T>>::Error

§

fn compare(&self, a: &&T, b: &&T) -> Result<Ordering, <C as Compare<&T>>::Error>

§

impl<T, U, E, C> Compare<(T, U)> for C
where C: Compare<T, Error = E, Error = E> + Compare<U>,

§

type Error = E

§

fn compare( &self, a: &(T, U), b: &(T, U) ) -> Result<Ordering, <C as Compare<(T, U)>>::Error>

§

impl<T, U, V, E, C> Compare<(T, U, V)> for C
where C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,

§

type Error = E

§

fn compare( &self, a: &(T, U, V), b: &(T, U, V) ) -> Result<Ordering, <C as Compare<(T, U, V)>>::Error>

§

impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
where C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,

§

type Error = E

§

fn compare( &self, a: &(T, U, V, W), b: &(T, U, V, W) ) -> Result<Ordering, <C as Compare<(T, U, V, W)>>::Error>

§

impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
where C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,

§

type Error = E

§

fn compare( &self, a: &(T, U, V, W, X), b: &(T, U, V, W, X) ) -> Result<Ordering, <C as Compare<(T, U, V, W, X)>>::Error>

§

impl<T, C> Compare<Box<T>> for C
where C: Compare<T>,

§

type Error = <C as Compare<T>>::Error

§

fn compare( &self, a: &Box<T>, b: &Box<T> ) -> Result<Ordering, <C as Compare<Box<T>>>::Error>

§

impl<T, C> Compare<Option<T>> for C
where C: Compare<T>,

§

type Error = <C as Compare<T>>::Error

§

fn compare( &self, a: &Option<T>, b: &Option<T> ) -> Result<Ordering, <C as Compare<Option<T>>>::Error>

§

impl<T, C> Compare<Rc<T>> for C
where C: Compare<T>,

§

type Error = <C as Compare<T>>::Error

§

fn compare( &self, a: &Rc<T>, b: &Rc<T> ) -> Result<Ordering, <C as Compare<Rc<T>>>::Error>

§

impl<T, C> Compare<Vec<T>> for C
where C: Compare<T>,

§

type Error = <C as Compare<T>>::Error

§

fn compare( &self, a: &Vec<T>, b: &Vec<T> ) -> Result<Ordering, <C as Compare<Vec<T>>>::Error>

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<E, T, U> FromVal<E, T> for U
where E: Env, U: TryFromVal<E, T>,

source§

fn from_val(e: &E, v: &T) -> U

source§

impl<T> FromXdr for T
where T: TryFromVal<Env, Val>,

§

type Error = <T as TryFromVal<Env, Val>>::Error

source§

fn from_xdr(env: &Env, b: &Bytes) -> Result<T, <T as FromXdr>::Error>

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<E, T, U> IntoVal<E, T> for U
where E: Env, T: FromVal<E, U>,

source§

fn into_val(&self, e: &E) -> T

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToXdr for T
where T: IntoVal<Env, Val>,

source§

fn to_xdr(self, env: &Env) -> Bytes

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.
§

impl<E, T, U> TryIntoVal<E, T> for U
where E: Env, T: TryFromVal<E, U>,

§

type Error = <T as TryFromVal<E, U>>::Error

§

fn try_into_val(&self, env: &E) -> Result<T, <U as TryIntoVal<E, T>>::Error>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V