Struct soroban_sdk::Map
source · [−]#[repr(transparent)]pub struct Map<K, V>(_, _, _);
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 RawVals, and when retrieved from the Map are transmitted back and converted from RawVal back into their type.
The 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.
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 Data, 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(Ok((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
sourceimpl<K, V> Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
pub fn env(&self) -> &Env
pub fn as_raw(&self) -> &RawVal
pub fn to_raw(&self) -> RawVal
pub fn new(env: &Env) -> Map<K, V>
pub fn from_array<const N: usize>(env: &Env, items: [(K, V); N]) -> Map<K, V>
pub fn contains_key(&self, k: K) -> bool
pub fn get(&self, k: K) -> Option<Result<V, V::Error>>
pub fn get_unchecked(&self, k: K) -> Result<V, V::Error>
pub fn set(&mut self, k: K, v: V)
pub fn remove(&mut self, k: K) -> Option<()>
pub fn remove_unchecked(&mut self, k: K)
pub fn is_empty(&self) -> bool
pub fn len(&self) -> u32
pub fn keys(&self) -> Vec<K>
pub fn values(&self) -> Vec<V>
pub fn iter(&self) -> MapIter<K, V>where
K: Clone,
V: Clone,
pub fn iter_unchecked(
&self
) -> UncheckedIter<MapIter<K, V>, (K, V), ConversionError>ⓘNotable traits for UncheckedIter<I, T, E>impl<I, T, E> Iterator for UncheckedIter<I, T, E>where
I: Iterator<Item = Result<T, E>>,
E: Debug, type Item = T;
where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Clone,
K::Error: Debug,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Clone,
V::Error: Debug,
I: Iterator<Item = Result<T, E>>,
E: Debug, type Item = T;
pub fn into_iter_unchecked(
self
) -> UncheckedIter<MapIter<K, V>, (K, V), ConversionError>ⓘNotable traits for UncheckedIter<I, T, E>impl<I, T, E> Iterator for UncheckedIter<I, T, E>where
I: Iterator<Item = Result<T, E>>,
E: Debug, type Item = T;
where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Clone,
K::Error: Debug,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Clone,
V::Error: Debug,
I: Iterator<Item = Result<T, E>>,
E: Debug, type Item = T;
Trait Implementations
sourceimpl<K, V> Debug for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Debug + Clone,
K::Error: Debug,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Debug + Clone,
V::Error: Debug,
impl<K, V> Debug for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Debug + Clone,
K::Error: Debug,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Debug + Clone,
V::Error: Debug,
sourceimpl<K, V> From<Map<K, V>> for EnvVal<Env, Object>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> From<Map<K, V>> for EnvVal<Env, Object>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
sourceimpl<K, V> From<Map<K, V>> for RawValwhere
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> From<Map<K, V>> for RawValwhere
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
sourceimpl<K, V> IntoIterator for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> IntoIterator for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
sourceimpl<K, V> IntoVal<Env, RawVal> for &Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> IntoVal<Env, RawVal> for &Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
sourceimpl<K, V> IntoVal<Env, RawVal> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> IntoVal<Env, RawVal> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
sourceimpl<K, V> Ord for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> Ord for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
sourceimpl<K, V> PartialEq<Map<K, V>> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> PartialEq<Map<K, V>> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
sourceimpl<K, V> PartialOrd<Map<K, V>> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> PartialOrd<Map<K, V>> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl<K, V> TryFromVal<Env, Object> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> TryFromVal<Env, Object> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
type Error = ConversionError
fn try_from_val(env: &Env, obj: Object) -> Result<Self, Self::Error>
sourceimpl<K, V> TryFromVal<Env, RawVal> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> TryFromVal<Env, RawVal> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
sourceimpl<K, V> TryFromVal<Env, ScVal> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> TryFromVal<Env, ScVal> for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
type Error = ConversionError
fn try_from_val(env: &Env, val: ScVal) -> Result<Self, Self::Error>
sourceimpl<K, V> TryIntoVal<Env, Map<K, V>> for Objectwhere
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> TryIntoVal<Env, Map<K, V>> for Objectwhere
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
type Error = ConversionError
fn try_into_val(self, env: &Env) -> Result<Map<K, V>, Self::Error>
fn try_into_env_val(self, env: &E) -> Result<EnvVal<E, V>, Self::Error>
sourceimpl<K, V> TryIntoVal<Env, Map<K, V>> for RawValwhere
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> TryIntoVal<Env, Map<K, V>> for RawValwhere
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
type Error = ConversionError
fn try_into_val(self, env: &Env) -> Result<Map<K, V>, Self::Error>
fn try_into_env_val(self, env: &E) -> Result<EnvVal<E, V>, Self::Error>
sourceimpl<K, V> TryIntoVal<Env, Map<K, V>> for ScValwhere
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> TryIntoVal<Env, Map<K, V>> for ScValwhere
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
type Error = ConversionError
fn try_into_val(self, env: &Env) -> Result<Map<K, V>, Self::Error>
fn try_into_env_val(self, env: &E) -> Result<EnvVal<E, V>, Self::Error>
impl<K, V> Eq for Map<K, V>where
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
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
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T> Deserialize for Twhere
T: TryFromVal<Env, RawVal>,
impl<T> Deserialize for Twhere
T: TryFromVal<Env, RawVal>,
type Error = <T as TryFromVal<Env, RawVal>>::Error
fn deserialize(env: &Env, b: &Bytes) -> Result<T, <T as Deserialize>::Error>
impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
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
. Read morefn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read morefn 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. Read morefn 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. Read more