Trait concordium_std::HasStateApi
source · pub trait HasStateApi: Clone {
type EntryType: HasStateEntry;
type IterType: Iterator<Item = Self::EntryType>;
// Required methods
fn create_entry(
&mut self,
key: &[u8]
) -> Result<Self::EntryType, StateError>;
fn lookup_entry(&self, key: &[u8]) -> Option<Self::EntryType>;
fn delete_entry(&mut self, entry: Self::EntryType) -> Result<(), StateError>;
fn delete_prefix(&mut self, prefix: &[u8]) -> Result<bool, StateError>;
fn iterator(&self, prefix: &[u8]) -> Result<Self::IterType, StateError>;
fn delete_iterator(&mut self, iter: Self::IterType);
// Provided methods
fn entry<K: AsRef<[u8]> + Into<Key>>(&mut self, key: K) -> EntryRaw<Self> { ... }
fn read_root<A: DeserialWithState<Self>>(&self) -> ParseResult<A> { ... }
fn write_root<A: Serial>(&mut self, new_state: &A) { ... }
}
Expand description
Types which can serve as the contract state.
§Deprecation notice
This trait is deprecated along with
crate::test_infrastructure
.
Use StateApi
instead unless you intend to use
the deprecated test infrastructure.
See the crate documentation for more details.
Required Associated Types§
Required Methods§
sourcefn create_entry(&mut self, key: &[u8]) -> Result<Self::EntryType, StateError>
fn create_entry(&mut self, key: &[u8]) -> Result<Self::EntryType, StateError>
Create a new entry in the state. If an entry with the given key already exists then it is reset to an empty entry. If the part of the tree where the key points to is locked due to an acquired iterator then no entry is created, and an error will be returned.
sourcefn lookup_entry(&self, key: &[u8]) -> Option<Self::EntryType>
fn lookup_entry(&self, key: &[u8]) -> Option<Self::EntryType>
Lookup an entry in the state.
sourcefn delete_entry(&mut self, entry: Self::EntryType) -> Result<(), StateError>
fn delete_entry(&mut self, entry: Self::EntryType) -> Result<(), StateError>
Delete an entry. Returns an error if the entry did not exist, or if it is part of a locked subtree.
sourcefn delete_prefix(&mut self, prefix: &[u8]) -> Result<bool, StateError>
fn delete_prefix(&mut self, prefix: &[u8]) -> Result<bool, StateError>
Delete the entire subtree. Returns whether any values were deleted, or an error if the given prefix is part of a locked subtree.
sourcefn iterator(&self, prefix: &[u8]) -> Result<Self::IterType, StateError>
fn iterator(&self, prefix: &[u8]) -> Result<Self::IterType, StateError>
Get an iterator over a map in the state.
An iterator locks the subtree with the given prefix. Locking means that the structure of the tree cannot be altered, i.e., that entries cannot be created or deleted. Altering the data inside an entry of a locked subtree is, however, allowed.
Deleting iterators with
delete_iterator
is necessary to
unlock the subtree. To unlock a subtree with prefix
p1
, all iterators with prefixes p2
, where p2
is a prefix of p1
,
must be deleted.
Returns an error if the number of active iterators for the same prefix exceeds u32::MAX.
sourcefn delete_iterator(&mut self, iter: Self::IterType)
fn delete_iterator(&mut self, iter: Self::IterType)
Delete an iterator.
See the iterator
method for why this is
necessary.
Provided Methods§
sourcefn entry<K: AsRef<[u8]> + Into<Key>>(&mut self, key: K) -> EntryRaw<Self>
fn entry<K: AsRef<[u8]> + Into<Key>>(&mut self, key: K) -> EntryRaw<Self>
Like lookup_entry
except that it consumes the
key and returns an EntryRaw
instead of an optional entry.
For maximal flexibility the function is parametrized by the type of key
with the intention that needless allocations are avoided for short
keys and existing entries. The most common examples of keys will be Vec<u8>
or fixed sized arrays [u8; N]
.
sourcefn read_root<A: DeserialWithState<Self>>(&self) -> ParseResult<A>
fn read_root<A: DeserialWithState<Self>>(&self) -> ParseResult<A>
Read and deserialize the state stored at the root of the state trie. If such a state does not exist, or cannot be deserialized into the provided type, then this returns an error.
sourcefn write_root<A: Serial>(&mut self, new_state: &A)
fn write_root<A: Serial>(&mut self, new_state: &A)
Serialize and write the state at the root of the state trie. Dual to
read_root
.