Struct fadroma::storage::map::Map

source ·
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>

source

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
source

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.

source

pub fn get( &self, storage: &dyn Storage, key: impl Into<K> ) -> StdResult<Option<V>>

source

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);
source

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);
source

pub fn get_or_error( &self, storage: &dyn Storage, key: impl Into<K> ) -> StdResult<V>

source

pub fn canonize_and_insert<Input: Canonize<Output = V>>( &mut self, deps: DepsMut<'_>, key: impl Into<K>, item: Input ) -> StdResult<bool>

source

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.

source

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>

source

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));
source§

impl<K: Key, V: FadromaSerialize + FadromaDeserialize + Humanize, N: Namespace> Map<K, V, N>

source

pub fn get_humanize( &self, deps: Deps<'_>, key: impl Into<K> ) -> StdResult<Option<<V as Humanize>::Output>>

source

pub fn get_humanize_or_error( &self, deps: Deps<'_>, key: impl Into<K> ) -> StdResult<<V as Humanize>::Output>

source§

impl<K: Key, V: FadromaSerialize + FadromaDeserialize + Humanize, N: Namespace> Map<K, V, N>where <V as Humanize>::Output: Default,

source

pub fn get_humanize_or_default( &self, deps: Deps<'_>, key: impl Into<K> ) -> StdResult<<V as Humanize>::Output>

source§

impl<K: Key, V: FadromaSerialize + FadromaDeserialize + Default, N: Namespace> Map<K, V, N>

source

pub fn get_or_default( &self, storage: &dyn Storage, key: impl Into<K> ) -> StdResult<V>

Auto Trait Implementations§

§

impl<K, V, N> RefUnwindSafe for Map<K, V, N>where K: RefUnwindSafe, N: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V, N> Send for Map<K, V, N>where K: Send, N: Send, V: Send,

§

impl<K, V, N> Sync for Map<K, V, N>where K: Sync, N: Sync, V: Sync,

§

impl<K, V, N> Unpin for Map<K, V, N>where K: Unpin, N: Unpin, V: Unpin,

§

impl<K, V, N> UnwindSafe for Map<K, V, N>where K: UnwindSafe, N: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V