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 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 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(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§
source§impl<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> ⓘwhere K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Clone, K::Error: Debug, V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Clone, V::Error: Debug,
pub fn into_iter_unchecked( self ) -> UncheckedIter<MapIter<K, V>, (K, V), ConversionError> ⓘwhere K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Clone, K::Error: Debug, V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal> + Clone, V::Error: Debug,
Trait Implementations§
source§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,
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,
source§impl<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>,
source§impl<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>,
source§impl<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>,
source§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>,
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>,
source§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>,
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>,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<K, V> TryFromVal<Env, Map<K, V>> for RawValwhere
K: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
V: IntoVal<Env, RawVal> + TryFromVal<Env, RawVal>,
impl<K, V> TryFromVal<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_from_val(_env: &Env, v: &Map<K, V>) -> Result<Self, Self::Error>
source§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>,
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>
source§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>,
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>,
source§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>,
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>
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§
§impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
source§impl<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
.§fn 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
.§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.§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.