pub struct StateMap<K, V, Codec = BorshCodec> { /* private fields */ }Expand description
A container that maps keys to values.
Type parameters
StateMap is generic over:
- a key type
K; - a value type
V; - a
StateValueCodecCodec.
Implementations§
source§impl<K, V> StateMap<K, V>
impl<K, V> StateMap<K, V>
sourcepub fn new(prefix: Prefix) -> Self
pub fn new(prefix: Prefix) -> Self
Creates a new StateMap with the given prefix and the default
StateValueCodec (i.e. BorshCodec).
source§impl<K, V, Codec> StateMap<K, V, Codec>where
Codec: StateCodec,
Codec::KeyCodec: StateKeyCodec<K>,
Codec::ValueCodec: StateValueCodec<V>,
impl<K, V, Codec> StateMap<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 WorkingSet<S>
)where
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Q: ?Sized,
pub fn set<Q, S: Storage>( &self, key: &Q, value: &V, working_set: &mut WorkingSet<S> )where Codec::KeyCodec: EncodeKeyLike<Q, K>, Q: ?Sized,
Inserts a key-value pair into the map.
Much like StateMap::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 WorkingSet<S>
) -> Option<V>where
Codec: StateCodec,
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Codec::ValueCodec: StateValueCodec<V>,
Q: ?Sized,
pub fn get<Q, S: Storage>( &self, key: &Q, working_set: &mut WorkingSet<S> ) -> Option<V>where Codec: StateCodec, Codec::KeyCodec: EncodeKeyLike<Q, K>, Codec::ValueCodec: StateValueCodec<V>, 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::{StateMap, Storage, WorkingSet};
fn foo<S>(map: StateMap<Vec<u8>, u64>, key: &[u8], ws: &mut WorkingSet<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::{StateMap, Storage, WorkingSet};
fn foo<S>(map: StateMap<Vec<u8>, u64>, key: [u8; 32], ws: &mut WorkingSet<S>) -> Option<u64>
where
S: Storage,
{
map.get(&key[..], ws)
}sourcepub fn get_or_err<Q, S: Storage>(
&self,
key: &Q,
working_set: &mut WorkingSet<S>
) -> Result<V, StateMapError>where
Codec: StateCodec,
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Codec::ValueCodec: StateValueCodec<V>,
Q: ?Sized,
pub fn get_or_err<Q, S: Storage>( &self, key: &Q, working_set: &mut WorkingSet<S> ) -> Result<V, StateMapError>where Codec: StateCodec, Codec::KeyCodec: EncodeKeyLike<Q, K>, Codec::ValueCodec: StateValueCodec<V>, 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 WorkingSet<S>
) -> Option<V>where
Codec: StateCodec,
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Codec::ValueCodec: StateValueCodec<V>,
Q: ?Sized,
pub fn remove<Q, S: Storage>( &self, key: &Q, working_set: &mut WorkingSet<S> ) -> Option<V>where Codec: StateCodec, Codec::KeyCodec: EncodeKeyLike<Q, K>, Codec::ValueCodec: StateValueCodec<V>, 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 WorkingSet<S>
) -> Result<V, StateMapError>where
Codec: StateCodec,
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Codec::ValueCodec: StateValueCodec<V>,
Q: ?Sized,
pub fn remove_or_err<Q, S: Storage>( &self, key: &Q, working_set: &mut WorkingSet<S> ) -> Result<V, StateMapError>where Codec: StateCodec, Codec::KeyCodec: EncodeKeyLike<Q, K>, Codec::ValueCodec: StateValueCodec<V>, Q: ?Sized,
Removes a key from the map, returning the corresponding value (or
StateMapError if the key is absent).
Use StateMap::remove if you want an Option instead of a Result.
sourcepub fn delete<Q, S: Storage>(&self, key: &Q, working_set: &mut WorkingSet<S>)where
Codec: StateCodec,
Codec::KeyCodec: EncodeKeyLike<Q, K>,
Q: ?Sized,
pub fn delete<Q, S: Storage>(&self, key: &Q, working_set: &mut WorkingSet<S>)where Codec: StateCodec, Codec::KeyCodec: EncodeKeyLike<Q, K>, Q: ?Sized,
Deletes a key-value pair from the map.
This is equivalent to StateMap::remove, but doesn’t deserialize and
return the value beforing deletion.