Struct fadroma::storage::map::InsertOnlyMap
source · pub struct InsertOnlyMap<K: Key, V: FadromaSerialize + FadromaDeserialize, N: Namespace> { /* private fields */ }
Expand description
Implementations§
source§impl<K: Key, V: FadromaSerialize + FadromaDeserialize, N: Namespace> InsertOnlyMap<K, V, N>
impl<K: Key, V: FadromaSerialize + FadromaDeserialize, N: Namespace> InsertOnlyMap<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<Iter<'storage, V>>
pub fn values<'storage>( &self, storage: &'storage dyn Storage ) -> StdResult<Iter<'storage, V>>
Returns an iterator over all of the values stored by the map.
sourcepub fn insert(
&mut self,
storage: &mut dyn Storage,
key: impl Into<K>,
value: &V
) -> StdResult<Option<u64>>
pub fn insert( &mut self, storage: &mut dyn Storage, key: impl Into<K>, value: &V ) -> StdResult<Option<u64>>
Inserts the given value into the map. Returns the index at which
the value was stored. If the value was updated instead, it will return None
.
The index can be used to call InsertOnlyMap::get_by_index
as an optimization
to avoid loading it internally using a key which is what InsertOnlyMap::get
does.
Examples
fadroma::namespace!(NumbersNs, b"numbers");
let mut map = InsertOnlyMap::<TypedKey<String>, u8, NumbersNs>::new();
let key = "one".to_string();
let index = map.insert(storage, &key, &1)?;
// We inserted a new value, so the index is returned.
assert_eq!(index, Some(0));
let index = map.insert(storage, &key, &2)?;
// We are updating an existing value, so no index is returned.
assert_eq!(index, None);
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<(Option<u64>, V)>
pub fn get_or_insert( &mut self, storage: &mut dyn Storage, key: impl Into<K>, value: V ) -> StdResult<(Option<u64>, V)>
Returns a tuple where the first member is the index of the value if it 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 = InsertOnlyMap::<TypedKey<String>, u8, NumbersNs>::new();
let key = "one".to_string();
let (index, value) = map.get_or_insert(storage, &key, 5)?;
// Value wasn't previously present in the map.
assert_eq!(index, Some(0));
assert_eq!(value, 5);
let (index, value) = map.get_or_insert(storage, &key, 10)?;
// Value is now present and so the 10 is not inserted.
assert_eq!(index, None);
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<(Option<u64>, V)>
pub fn get_or_insert_with( &mut self, storage: &mut dyn Storage, key: impl Into<K>, func: impl FnOnce() -> V ) -> StdResult<(Option<u64>, V)>
Returns a tuple where the first member is the index of the value if it 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 = InsertOnlyMap::<TypedKey<String>, u8, NumbersNs>::new();
let key = "one".to_string();
let (index, value) = map.get_or_insert_with(storage, &key, || 5)?;
// Value wasn't previously present in the map.
assert_eq!(index, Some(0));
assert_eq!(value, 5);
let (index, value) = map.get_or_insert_with(storage, &key, || 10)?;
// Value is now present and so the 10 is not inserted.
assert_eq!(index, None);
assert_eq!(value, 5);
sourcepub fn get_by_index(
&self,
storage: &dyn Storage,
index: u64
) -> StdResult<Option<V>>
pub fn get_by_index( &self, storage: &dyn Storage, index: u64 ) -> StdResult<Option<V>>
Gets the value using the index at which the value was stored. Internally the key maps to the index which itself maps to the value. So if you have the index, you can skip loading the key and try getting the value directly. The index is returned by all methods that can insert values.
Examples
fadroma::namespace!(NumbersNs, b"numbers");
let mut map = InsertOnlyMap::<TypedKey<String>, u8, NumbersNs>::new();
let key = "one".to_string();
let index = map.insert(storage, &key, &1)?;
assert_eq!(index, Some(0));
let value = map.get_by_index(storage, index.unwrap())?;
assert_eq!(value, Some(1));
// Try to get a non-existent index.
let value = map.get_by_index(storage, 1)?;
assert_eq!(value, None);
pub fn get_or_error( &self, storage: &dyn Storage, key: impl Into<K> ) -> StdResult<V>
sourcepub fn canonize_and_insert<Input: Canonize<Output = V>>(
&mut self,
deps: DepsMut<'_>,
key: impl Into<K>,
item: Input
) -> StdResult<Option<u64>>
pub fn canonize_and_insert<Input: Canonize<Output = V>>( &mut self, deps: DepsMut<'_>, key: impl Into<K>, item: Input ) -> StdResult<Option<u64>>
Canonizes the given value and inserts it into the map. Returns the index at which
the value was stored. If the value was updated instead, it will return None
.
The index can be used to call InsertOnlyMap::get_by_index
as an optimization
to avoid loading it internally using a key which is what InsertOnlyMap::get
does.
source§impl<K: Key, V: FadromaSerialize + FadromaDeserialize + Default, N: Namespace> InsertOnlyMap<K, V, N>
impl<K: Key, V: FadromaSerialize + FadromaDeserialize + Default, N: Namespace> InsertOnlyMap<K, V, N>
sourcepub fn get_or_insert_default(
&mut self,
storage: &mut dyn Storage,
key: impl Into<K>
) -> StdResult<(Option<u64>, V)>
pub fn get_or_insert_default( &mut self, storage: &mut dyn Storage, key: impl Into<K> ) -> StdResult<(Option<u64>, V)>
Returns a tuple where the first member is the index of the value if it 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 = InsertOnlyMap::<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 (index, value) = map.get_or_insert_default(storage, &key)?;
// We've now inserted the default value for u8 which is 0.
assert_eq!(index, Some(0));
assert_eq!(value, 0);
let value = map.get(storage, &key)?;
assert_eq!(value, Some(0));