Struct cw_storage_plus::Map

source ·
pub struct Map<K, T> { /* private fields */ }

Implementations§

source§

impl<K, T> Map<K, T>

source

pub const fn new(namespace: &'static str) -> Self

Creates a new Map with the given storage key. This is a const fn only suitable when you have the storage key in the form of a static string slice.

source

pub fn new_dyn(namespace: impl Into<Namespace>) -> Self

Creates a new Map with the given storage key. Use this if you might need to handle a dynamic string. Otherwise, you might prefer Map::new.

source

pub fn namespace_bytes(&self) -> &[u8]

source§

impl<'a, K, T> Map<K, T>

source

pub fn key(&self, k: K) -> Path<T>

source

pub fn save(&self, store: &mut dyn Storage, k: K, data: &T) -> StdResult<()>

source

pub fn remove(&self, store: &mut dyn Storage, k: K)

source

pub fn load(&self, store: &dyn Storage, k: K) -> StdResult<T>

load will return an error if no data is set at the given key, or on parse error

source

pub fn may_load(&self, store: &dyn Storage, k: K) -> StdResult<Option<T>>

may_load will parse the data stored at the key if present, returns Ok(None) if no data there. returns an error on issues parsing

source

pub fn has(&self, store: &dyn Storage, k: K) -> bool

has returns true or false if any data is at this key, without parsing or interpreting the contents.

source

pub fn update<A, E>( &self, store: &mut dyn Storage, k: K, action: A ) -> Result<T, E>
where A: FnOnce(Option<T>) -> Result<T, E>, E: From<StdError>,

Loads the data, perform the specified action, and store the result in the database. This is shorthand for some common sequences, which may be useful.

If the data exists, action(Some(value)) is called. Otherwise action(None) is called.

source

pub fn query<Q: CustomQuery>( &self, querier: &QuerierWrapper<'_, Q>, remote_contract: Addr, k: K ) -> StdResult<Option<T>>

If you import the proper Map from the remote contract, this will let you read the data from a remote contract in a type-safe way using WasmQuery::RawQuery

source

pub fn clear(&self, store: &mut dyn Storage)

Clears the map, removing all elements.

source

pub fn is_empty(&self, store: &dyn Storage) -> bool

Returns true if the map is empty.

source§

impl<'a, K, T> Map<K, T>

source

pub fn sub_prefix( &self, p: K::SubPrefix ) -> Prefix<K::SuperSuffix, T, K::SuperSuffix>

source

pub fn prefix(&self, p: K::Prefix) -> Prefix<K::Suffix, T, K::Suffix>

source§

impl<'a, K, T> Map<K, T>

source

pub fn prefix_range_raw<'c>( &self, store: &'c dyn Storage, min: Option<PrefixBound<'a, K::Prefix>>, max: Option<PrefixBound<'a, K::Prefix>>, order: Order ) -> Box<dyn Iterator<Item = StdResult<Record<T>>> + 'c>
where T: 'c, 'a: 'c,

While range_raw over a prefix fixes the prefix to one element and iterates over the remaining, prefix_range_raw accepts bounds for the lowest and highest elements of the Prefix itself, and iterates over those (inclusively or exclusively, depending on PrefixBound). There are some issues that distinguish these two, and blindly casting to Vec<u8> doesn’t solve them.

source§

impl<'a, K, T> Map<K, T>

source

pub fn prefix_range<'c>( &self, store: &'c dyn Storage, min: Option<PrefixBound<'a, K::Prefix>>, max: Option<PrefixBound<'a, K::Prefix>>, order: Order ) -> Box<dyn Iterator<Item = StdResult<(K::Output, T)>> + 'c>
where T: 'c, K: 'c, K::Output: 'static, 'a: 'c,

While range over a prefix fixes the prefix to one element and iterates over the remaining, prefix_range accepts bounds for the lowest and highest elements of the Prefix itself, and iterates over those (inclusively or exclusively, depending on PrefixBound). There are some issues that distinguish these two, and blindly casting to Vec<u8> doesn’t solve them.

source§

impl<'a, K, T> Map<K, T>

source

pub fn range_raw<'c>( &self, store: &'c dyn Storage, min: Option<Bound<'a, K>>, max: Option<Bound<'a, K>>, order: Order ) -> Box<dyn Iterator<Item = StdResult<Record<T>>> + 'c>
where T: 'c,

source

pub fn keys_raw<'c>( &self, store: &'c dyn Storage, min: Option<Bound<'a, K>>, max: Option<Bound<'a, K>>, order: Order ) -> Box<dyn Iterator<Item = Vec<u8>> + 'c>
where T: 'c,

source§

impl<'a, K, T> Map<K, T>

source

pub fn range<'c>( &self, store: &'c dyn Storage, min: Option<Bound<'a, K>>, max: Option<Bound<'a, K>>, order: Order ) -> Box<dyn Iterator<Item = StdResult<(K::Output, T)>> + 'c>
where T: 'c, K::Output: 'static,

source

pub fn keys<'c>( &self, store: &'c dyn Storage, min: Option<Bound<'a, K>>, max: Option<Bound<'a, K>>, order: Order ) -> Box<dyn Iterator<Item = StdResult<K::Output>> + 'c>
where T: 'c, K::Output: 'static,

source

pub fn first(&self, storage: &dyn Storage) -> StdResult<Option<(K::Output, T)>>
where K::Output: 'static,

Returns the first key-value pair in the map. This is not according to insertion-order, but according to the key ordering.

§Examples
const MAP: Map<i32, u32> = Map::new("map");

// empty map
assert_eq!(MAP.first(&storage), Ok(None));

// insert entries
MAP.save(&mut storage, 1, &10).unwrap();
MAP.save(&mut storage, -2, &20).unwrap();

assert_eq!(MAP.first(&storage), Ok(Some((-2, 20))));
source

pub fn last(&self, storage: &dyn Storage) -> StdResult<Option<(K::Output, T)>>
where K::Output: 'static,

Returns the last key-value pair in the map. This is not according to insertion-order, but according to the key ordering.

§Examples
const MAP: Map<i32, u32> = Map::new("map");

// empty map
assert_eq!(MAP.last(&storage), Ok(None));

// insert entries
MAP.save(&mut storage, 1, &10).unwrap();
MAP.save(&mut storage, -2, &20).unwrap();

assert_eq!(MAP.last(&storage), Ok(Some((1, 10))));

Trait Implementations§

source§

impl<K: Clone, T: Clone> Clone for Map<K, T>

source§

fn clone(&self) -> Map<K, T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K: Debug, T: Debug> Debug for Map<K, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, T> Freeze for Map<K, T>

§

impl<K, T> RefUnwindSafe for Map<K, T>

§

impl<K, T> Send for Map<K, T>
where K: Send, T: Send,

§

impl<K, T> Sync for Map<K, T>
where K: Sync, T: Sync,

§

impl<K, T> Unpin for Map<K, T>
where K: Unpin, T: Unpin,

§

impl<K, T> UnwindSafe for Map<K, T>
where K: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<U> As for U

source§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where 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 for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.