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
andV
. It may be necessary to validate all pairs, either before storing, or when loading withtry_
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>
impl<K, V> Map<K, V>
Sourcepub fn from_array<const N: usize>(env: &Env, items: [(K, V); N]) -> Map<K, V>
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.
Sourcepub fn contains_key(&self, k: K) -> bool
pub fn contains_key(&self, k: K) -> bool
Returns true if a key-value pair exists in the map with the given key.
Sourcepub fn get(&self, k: K) -> Option<V>
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.
Sourcepub fn try_get(&self, k: K) -> Result<Option<V>, V::Error>
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.
Sourcepub fn get_unchecked(&self, k: K) -> V
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.
Sourcepub fn try_get_unchecked(&self, k: K) -> Result<V, V::Error>
pub fn try_get_unchecked(&self, k: K) -> Result<V, V::Error>
Sourcepub fn set(&mut self, k: K, v: V)
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.
Sourcepub fn remove(&mut self, k: K) -> Option<()>
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.
Sourcepub fn remove_unchecked(&mut self, k: K)
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§impl<K, V> Map<K, V>
impl<K, V> Map<K, V>
pub fn iter(&self) -> UnwrappedIter<MapTryIter<K, V>, (K, V), ConversionError> ⓘ
pub fn try_iter(&self) -> MapTryIter<K, V>
pub fn into_try_iter(self) -> MapTryIter<K, V>
Trait Implementations§
Source§impl<K, V> IntoIterator for Map<K, V>
impl<K, V> IntoIterator for Map<K, V>
Source§impl<K, V> Ord for Map<K, V>
impl<K, V> Ord for Map<K, V>
Source§impl<K, V> PartialOrd for Map<K, V>
impl<K, V> PartialOrd for Map<K, V>
Source§impl<K, V> SorobanArbitrary for Map<K, V>where
K: SorobanArbitrary,
V: SorobanArbitrary,
Available on crate feature testutils
only.
impl<K, V> SorobanArbitrary for Map<K, V>where
K: SorobanArbitrary,
V: SorobanArbitrary,
testutils
only.Source§type Prototype = ArbitraryMap<<K as SorobanArbitrary>::Prototype, <V as SorobanArbitrary>::Prototype>
type Prototype = ArbitraryMap<<K as SorobanArbitrary>::Prototype, <V as SorobanArbitrary>::Prototype>
Arbitrary
and can be converted to this
SorobanArbitrary
type.Source§impl<K, V> TryFromVal<Env, Map<K, V>> for ScVal
impl<K, V> TryFromVal<Env, Map<K, V>> for ScVal
type Error = ConversionError
fn try_from_val(_e: &Env, v: &Map<K, V>) -> Result<Self, ConversionError>
impl<K, V> Eq for Map<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for Map<K, V>
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>
impl<K, V> !UnwindSafe for Map<K, V>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
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>
Source§impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
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>
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<E, T, U> FromVal<E, T> for Uwhere
E: Env,
U: TryFromVal<E, T>,
impl<E, T, U> FromVal<E, T> for Uwhere
E: Env,
U: TryFromVal<E, T>,
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