pub struct Map<K: Key, V: FadromaSerialize + FadromaDeserialize, N: Namespace> { /* private fields */ }
Expand description
A key-value storage type that can iterate over all values stored
while allowing to arbitrarily insert, get and remove them.
If you don’t need the ability to remove values use InsertOnlyMap
instead
as it has no overhead. Map
internally stores the key of the item together
with the value in order to enable the remove operation.
Implementations§
source§impl<K: Key, V: FadromaSerialize + FadromaDeserialize, N: Namespace> Map<K, V, N>
impl<K: Key, V: FadromaSerialize + FadromaDeserialize, N: Namespace> Map<K, V, N>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates an instance for the given namespace.
The following namespaces are reserved by Map
:
- N + “key” + K,
- N + “index”
- N + n - where n is a number
sourcepub fn values<'storage>(
&self,
storage: &'storage dyn Storage
) -> StdResult<MapValueIter<'storage, V>>
pub fn values<'storage>( &self, storage: &'storage dyn Storage ) -> StdResult<MapValueIter<'storage, V>>
Returns an iterator over all of the values stored by the map.
pub fn get( &self, storage: &dyn Storage, key: impl Into<K> ) -> StdResult<Option<V>>
sourcepub fn get_or_insert(
&mut self,
storage: &mut dyn Storage,
key: impl Into<K>,
value: V
) -> StdResult<(bool, V)>
pub fn get_or_insert( &mut self, storage: &mut dyn Storage, key: impl Into<K>, value: V ) -> StdResult<(bool, V)>
Returns a tuple where the first member indicates whether the given value was inserted.
The second member is the value itself, either loaded from storage or the value
parameter.
Examples
fadroma::namespace!(NumbersNs, b"numbers");
let mut map = Map::<TypedKey<String>, u8, NumbersNs>::new();
let key = "one".to_string();
let (is_new, value) = map.get_or_insert(storage, &key, 5)?;
// Value wasn't previously present in the map.
assert_eq!(is_new, true);
assert_eq!(value, 5);
let (is_new, value) = map.get_or_insert(storage, &key, 10)?;
// Value is now present and so the 10 is not inserted.
assert_eq!(is_new, false);
assert_eq!(value, 5);
sourcepub fn get_or_insert_with(
&mut self,
storage: &mut dyn Storage,
key: impl Into<K>,
func: impl FnOnce() -> V
) -> StdResult<(bool, V)>
pub fn get_or_insert_with( &mut self, storage: &mut dyn Storage, key: impl Into<K>, func: impl FnOnce() -> V ) -> StdResult<(bool, V)>
Returns a tuple where the first member indicates whether the given value was inserted. The second member is the value itself, either loaded from storage or computed from the provided closure.
Examples
fadroma::namespace!(NumbersNs, b"numbers");
let mut map = Map::<TypedKey<String>, u8, NumbersNs>::new();
let key = "one".to_string();
let (is_new, value) = map.get_or_insert_with(storage, &key, || 5)?;
// Value wasn't previously present in the map.
assert_eq!(is_new, true);
assert_eq!(value, 5);
let (is_new, value) = map.get_or_insert_with(storage, &key, || 10)?;
// Value is now present and so the 10 is not inserted.
assert_eq!(is_new, false);
assert_eq!(value, 5);
pub fn get_or_error( &self, storage: &dyn Storage, key: impl Into<K> ) -> StdResult<V>
pub fn canonize_and_insert<Input: Canonize<Output = V>>( &mut self, deps: DepsMut<'_>, key: impl Into<K>, item: Input ) -> StdResult<bool>
sourcepub fn insert(
&mut self,
storage: &mut dyn Storage,
key: impl Into<K>,
value: &V
) -> StdResult<bool>
pub fn insert( &mut self, storage: &mut dyn Storage, key: impl Into<K>, value: &V ) -> StdResult<bool>
Inserts a new value into the map. Returns true
if a value
was previously stored under the given key.
pub fn remove( &mut self, storage: &mut dyn Storage, key: impl Into<K> ) -> StdResult<bool>
source§impl<K: Key, V: FadromaSerialize + FadromaDeserialize + Default, N: Namespace> Map<K, V, N>
impl<K: Key, V: FadromaSerialize + FadromaDeserialize + Default, N: Namespace> Map<K, V, N>
sourcepub fn get_or_insert_default(
&mut self,
storage: &mut dyn Storage,
key: impl Into<K>
) -> StdResult<(bool, V)>
pub fn get_or_insert_default( &mut self, storage: &mut dyn Storage, key: impl Into<K> ) -> StdResult<(bool, V)>
Returns a tuple where the first member indicates whether the given value was inserted. The second member is the value itself, either loaded from storage or the default value for the type.
Examples
fadroma::namespace!(NumbersNs, b"numbers");
let mut map = Map::<TypedKey<String>, u8, NumbersNs>::new();
let key = "one".to_string();
let value = map.get(storage, &key)?;
// No value stored at this key.
assert!(value.is_none());
let (is_new, value) = map.get_or_insert_default(storage, &key)?;
// We've now inserted the default value for u8 which is 0.
assert_eq!(is_new, true);
assert_eq!(value, 0);
let value = map.get(storage, &key)?;
assert_eq!(value, Some(0));