Struct sov_state::StateMap

source ·
pub struct StateMap<K, V, Codec = BorshCodec> { /* private fields */ }
Expand description

A container that maps keys to values.

Type parameters

StateMap is generic over:

Implementations§

source§

impl<K, V> StateMap<K, V>

source

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>

source

pub fn with_codec(prefix: Prefix, codec: Codec) -> Self

Creates a new StateMap with the given prefix and StateValueCodec.

source

pub fn codec(&self) -> &Codec

source

pub fn prefix(&self) -> &Prefix

Returns the prefix used when this StateMap was created.

source§

impl<K, V, Codec> StateMap<K, V, Codec>where Codec: StateCodec, Codec::KeyCodec: StateKeyCodec<K>, Codec::ValueCodec: StateValueCodec<V>,

source

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.

source

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

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.

source

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

source

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.

source

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.

Trait Implementations§

source§

impl<K, V, Codec> BorshDeserialize for StateMap<K, V, Codec>where (PhantomData<K>, PhantomData<V>): BorshDeserialize, Codec: BorshDeserialize, Prefix: BorshDeserialize,

source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>

source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where R: Read,

source§

impl<K, V, Codec> BorshSerialize for StateMap<K, V, Codec>where (PhantomData<K>, PhantomData<V>): BorshSerialize, Codec: BorshSerialize, Prefix: BorshSerialize,

source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error>

source§

fn try_to_vec(&self) -> Result<Vec<u8, Global>, Error>

Serialize this instance into a vector of bytes.
source§

impl<K: Clone, V: Clone, Codec: Clone> Clone for StateMap<K, V, Codec>

source§

fn clone(&self) -> StateMap<K, V, Codec>

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, V: Debug, Codec: Debug> Debug for StateMap<K, V, Codec>

source§

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

Formats the value using the given formatter. Read more
source§

impl<'de, K, V, Codec> Deserialize<'de> for StateMap<K, V, Codec>where Codec: Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<K: PartialEq, V: PartialEq, Codec: PartialEq> PartialEq<StateMap<K, V, Codec>> for StateMap<K, V, Codec>

source§

fn eq(&self, other: &StateMap<K, V, Codec>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, V, Codec> Serialize for StateMap<K, V, Codec>where Codec: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<K, V, Codec> StructuralPartialEq for StateMap<K, V, Codec>

Auto Trait Implementations§

§

impl<K, V, Codec> RefUnwindSafe for StateMap<K, V, Codec>where Codec: RefUnwindSafe, K: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V, Codec> Send for StateMap<K, V, Codec>where Codec: Send, K: Send, V: Send,

§

impl<K, V, Codec> Sync for StateMap<K, V, Codec>where Codec: Sync, K: Sync, V: Sync,

§

impl<K, V, Codec> Unpin for StateMap<K, V, Codec>where Codec: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, Codec> UnwindSafe for StateMap<K, V, Codec>where Codec: UnwindSafe, K: 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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for Twhere 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 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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,