Struct sov_state::AccessoryStateMap
source · pub struct AccessoryStateMap<K, V, Codec = BorshCodec> { /* private fields */ }Expand description
A container that maps keys to values stored as “accessory” state, outside of the JMT.
Type parameters
AccessoryStateMap is generic over:
- a key type
K; - a value type
V; - a
StateValueCodecCodec.
Implementations§
source§impl<K, V> AccessoryStateMap<K, V>
impl<K, V> AccessoryStateMap<K, V>
sourcepub fn new(prefix: Prefix) -> Self
pub fn new(prefix: Prefix) -> Self
Creates a new AccessoryStateMap with the given prefix and the default
StateValueCodec (i.e. BorshCodec).
source§impl<K, V, Codec> AccessoryStateMap<K, V, Codec>
impl<K, V, Codec> AccessoryStateMap<K, V, Codec>
sourcepub fn with_codec(prefix: Prefix, codec: Codec) -> Self
pub fn with_codec(prefix: Prefix, codec: Codec) -> Self
Creates a new AccessoryStateMap with the given prefix and StateValueCodec.
sourcepub fn prefix(&self) -> &Prefix
pub fn prefix(&self) -> &Prefix
Returns the prefix used when this AccessoryStateMap was created.
source§impl<K, V, Codec> AccessoryStateMap<K, V, Codec>where
Codec: StateCodec,
Codec::KeyCodec: StateKeyCodec<K>,
Codec::ValueCodec: StateValueCodec<V>,
impl<K, V, Codec> AccessoryStateMap<K, V, Codec>where Codec: StateCodec, Codec::KeyCodec: StateKeyCodec<K>, Codec::ValueCodec: StateValueCodec<V>,
sourcepub fn set<Q, S: Storage>(
&self,
key: &Q,
value: &V,
working_set: &mut AccessoryWorkingSet<'_, S>
)where
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Q: ?Sized,
pub fn set<Q, S: Storage>( &self, key: &Q, value: &V, working_set: &mut AccessoryWorkingSet<'_, S> )where Codec::KeyCodec: EncodeKeyLike<Q, K>, Q: ?Sized,
Inserts a key-value pair into the map.
Much like AccessoryStateMap::get, the key may be any borrowed form of the
map’s key type.
sourcepub fn get<Q, S: Storage>(
&self,
key: &Q,
working_set: &mut AccessoryWorkingSet<'_, S>
) -> Option<V>where
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Q: ?Sized,
pub fn get<Q, S: Storage>( &self, key: &Q, working_set: &mut AccessoryWorkingSet<'_, S> ) -> Option<V>where Codec::KeyCodec: EncodeKeyLike<Q, K>, Q: ?Sized,
Returns the value corresponding to the key, or None if the map
doesn’t contain the key.
Examples
The key may be any item that implements EncodeKeyLike the map’s key type
using your chosen codec.
use sov_state::{AccessoryStateMap, Storage, AccessoryWorkingSet};
fn foo<S>(map: AccessoryStateMap<Vec<u8>, u64>, key: &[u8], ws: &mut AccessoryWorkingSet<S>) -> Option<u64>
where
S: Storage,
{
// We perform the `get` with a slice, and not the `Vec`. it is so because `Vec` borrows
// `[T]`.
map.get(key, ws)
}If the map’s key type does not implement EncodeKeyLike for your desired
target type, you’ll have to convert the key to something else. An
example of this would be “slicing” an array to use in Vec-keyed
maps:
use sov_state::{AccessoryStateMap, Storage, AccessoryWorkingSet};
fn foo<S>(map: AccessoryStateMap<Vec<u8>, u64>, key: [u8; 32], ws: &mut AccessoryWorkingSet<S>) -> Option<u64>
where
S: Storage,
{
map.get(&key[..], ws)
}sourcepub fn get_or_err<Q, S: Storage>(
&self,
key: &Q,
working_set: &mut AccessoryWorkingSet<'_, S>
) -> Result<V, StateMapError>where
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Q: ?Sized,
pub fn get_or_err<Q, S: Storage>( &self, key: &Q, working_set: &mut AccessoryWorkingSet<'_, S> ) -> Result<V, StateMapError>where Codec::KeyCodec: EncodeKeyLike<Q, K>, Q: ?Sized,
Returns the value corresponding to the key or StateMapError if key is absent in
the map.
sourcepub fn remove<Q, S: Storage>(
&self,
key: &Q,
working_set: &mut AccessoryWorkingSet<'_, S>
) -> Option<V>where
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Q: ?Sized,
pub fn remove<Q, S: Storage>( &self, key: &Q, working_set: &mut AccessoryWorkingSet<'_, S> ) -> Option<V>where Codec::KeyCodec: EncodeKeyLike<Q, K>, Q: ?Sized,
Removes a key from the map, returning the corresponding value (or
None if the key is absent).
sourcepub fn remove_or_err<Q, S: Storage>(
&self,
key: &Q,
working_set: &mut AccessoryWorkingSet<'_, S>
) -> Result<V, StateMapError>where
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Q: ?Sized,
pub fn remove_or_err<Q, S: Storage>( &self, key: &Q, working_set: &mut AccessoryWorkingSet<'_, S> ) -> Result<V, StateMapError>where Codec::KeyCodec: EncodeKeyLike<Q, K>, Q: ?Sized,
Removes a key from the map, returning the corresponding value (or
StateMapError if the key is absent).
Use AccessoryStateMap::remove if you want an Option instead of a Result.
sourcepub fn delete<Q, S: Storage>(
&self,
key: &Q,
working_set: &mut AccessoryWorkingSet<'_, S>
)where
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Q: ?Sized,
pub fn delete<Q, S: Storage>( &self, key: &Q, working_set: &mut AccessoryWorkingSet<'_, S> )where Codec::KeyCodec: EncodeKeyLike<Q, K>, Q: ?Sized,
Deletes a key-value pair from the map.
This is equivalent to AccessoryStateMap::remove, but doesn’t deserialize and
return the value beforing deletion.
Trait Implementations§
source§impl<K, V, Codec> BorshDeserialize for AccessoryStateMap<K, V, Codec>where
(PhantomData<K>, PhantomData<V>): BorshDeserialize,
Codec: BorshDeserialize,
Prefix: BorshDeserialize,
impl<K, V, Codec> BorshDeserialize for AccessoryStateMap<K, V, Codec>where (PhantomData<K>, PhantomData<V>): BorshDeserialize, Codec: BorshDeserialize, Prefix: BorshDeserialize,
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>
source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where R: Read,
source§impl<K, V, Codec> BorshSerialize for AccessoryStateMap<K, V, Codec>where
(PhantomData<K>, PhantomData<V>): BorshSerialize,
Codec: BorshSerialize,
Prefix: BorshSerialize,
impl<K, V, Codec> BorshSerialize for AccessoryStateMap<K, V, Codec>where (PhantomData<K>, PhantomData<V>): BorshSerialize, Codec: BorshSerialize, Prefix: BorshSerialize,
source§impl<K: Clone, V: Clone, Codec: Clone> Clone for AccessoryStateMap<K, V, Codec>
impl<K: Clone, V: Clone, Codec: Clone> Clone for AccessoryStateMap<K, V, Codec>
source§fn clone(&self) -> AccessoryStateMap<K, V, Codec>
fn clone(&self) -> AccessoryStateMap<K, V, Codec>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl<'de, K, V, Codec> Deserialize<'de> for AccessoryStateMap<K, V, Codec>where
Codec: Deserialize<'de>,
impl<'de, K, V, Codec> Deserialize<'de> for AccessoryStateMap<K, V, Codec>where Codec: Deserialize<'de>,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl<K: PartialEq, V: PartialEq, Codec: PartialEq> PartialEq<AccessoryStateMap<K, V, Codec>> for AccessoryStateMap<K, V, Codec>
impl<K: PartialEq, V: PartialEq, Codec: PartialEq> PartialEq<AccessoryStateMap<K, V, Codec>> for AccessoryStateMap<K, V, Codec>
source§fn eq(&self, other: &AccessoryStateMap<K, V, Codec>) -> bool
fn eq(&self, other: &AccessoryStateMap<K, V, Codec>) -> bool
self and other values to be equal, and is used
by ==.