pub struct InsertOnlyMap<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 and get them. This type is more efficient than Map but it does not allow removing values. If you need to be able to remove values use Map instead.

Implementations§

source§

impl<K: Key, V: FadromaSerialize + FadromaDeserialize, N: Namespace> InsertOnlyMap<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<Iter<'storage, V>>

Returns an iterator over all of the values stored by the map.

source

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

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

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

source

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

impl<K: Key, V: FadromaSerialize + FadromaDeserialize + Humanize, N: Namespace> InsertOnlyMap<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> InsertOnlyMap<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> InsertOnlyMap<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 InsertOnlyMap<K, V, N>where K: RefUnwindSafe, N: RefUnwindSafe, V: RefUnwindSafe,

§

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

§

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

§

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

§

impl<K, V, N> UnwindSafe for InsertOnlyMap<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