pub struct StorageMap<V: Codec32> { /* private fields */ }Expand description
A persistent key-value map stored in contract storage.
StorageMap provides a HashMap-like interface over contract storage slots.
Each entry uses two slots: one for existence check, one for the value.
§Type Parameters
V- Value type, must implementCodec32for 32-byte encoding
§Storage Layout
For each key, two slots are derived:
derive_slot(namespace, ["map:exists", key])- Existence flag (1 byte)derive_slot(namespace, ["map:value", key])- Encoded value (32 bytes)
§Example
const NS_BALANCES: Namespace = Namespace([1u8; 32]);
let balances = StorageMap::<u64>::new(NS_BALANCES);
// Insert
balances.insert(b"alice", &1000)?;
// Get
let balance = balances.get(b"alice")?.unwrap_or(0);
// Check existence
if balances.contains_key(b"alice")? {
// Key exists
}
// Remove
balances.remove(b"alice")?;Implementations§
Source§impl<V: Codec32> StorageMap<V>
impl<V: Codec32> StorageMap<V>
Sourcepub const fn new(namespace: Namespace) -> Self
pub const fn new(namespace: Namespace) -> Self
Creates a new map with the specified namespace.
§Arguments
namespace- Unique 32-byte identifier for this map
Sourcepub fn exists_slot_for(&self, key: &[u8]) -> [u8; 32]
pub fn exists_slot_for(&self, key: &[u8]) -> [u8; 32]
Computes the existence slot for a given key.
Returns the storage slot address where the existence flag is stored.
Sourcepub fn value_slot_for(&self, key: &[u8]) -> [u8; 32]
pub fn value_slot_for(&self, key: &[u8]) -> [u8; 32]
Computes the value slot for a given key.
Returns the storage slot address where the encoded value is stored.
Sourcepub fn slots_for_key(&self, key: &[u8]) -> ([u8; 32], [u8; 32])
pub fn slots_for_key(&self, key: &[u8]) -> ([u8; 32], [u8; 32])
Returns both slots (existence, value) for a given key.
Useful for manifest generation.
Sourcepub fn contains_key_in<B: StorageBackend>(
&self,
backend: &B,
key: &[u8],
) -> Result<bool>
pub fn contains_key_in<B: StorageBackend>( &self, backend: &B, key: &[u8], ) -> Result<bool>
Checks if a key exists in the map (with explicit backend).
Sourcepub fn get_in<B: StorageBackend>(
&self,
backend: &B,
key: &[u8],
) -> Result<Option<V>>
pub fn get_in<B: StorageBackend>( &self, backend: &B, key: &[u8], ) -> Result<Option<V>>
Gets the value for a key (with explicit backend).
Returns None if the key doesn’t exist.
Sourcepub fn insert_in<B: StorageBackend>(
&self,
backend: &mut B,
key: &[u8],
value: &V,
) -> Result<()>
pub fn insert_in<B: StorageBackend>( &self, backend: &mut B, key: &[u8], value: &V, ) -> Result<()>
Inserts a key-value pair (with explicit backend).
Sourcepub fn remove_in<B: StorageBackend>(
&self,
backend: &mut B,
key: &[u8],
) -> Result<()>
pub fn remove_in<B: StorageBackend>( &self, backend: &mut B, key: &[u8], ) -> Result<()>
Removes a key-value pair (with explicit backend).
Sourcepub fn contains_typed_key_in<B: StorageBackend, K: BytesCodec>(
&self,
backend: &B,
key: &K,
) -> Result<bool>
pub fn contains_typed_key_in<B: StorageBackend, K: BytesCodec>( &self, backend: &B, key: &K, ) -> Result<bool>
Checks if a typed key exists (with explicit backend).
The key is encoded using BytesCodec before lookup.
Sourcepub fn get_typed_key_in<B: StorageBackend, K: BytesCodec>(
&self,
backend: &B,
key: &K,
) -> Result<Option<V>>
pub fn get_typed_key_in<B: StorageBackend, K: BytesCodec>( &self, backend: &B, key: &K, ) -> Result<Option<V>>
Gets the value for a typed key (with explicit backend).
Sourcepub fn insert_typed_key_in<B: StorageBackend, K: BytesCodec>(
&self,
backend: &mut B,
key: &K,
value: &V,
) -> Result<()>
pub fn insert_typed_key_in<B: StorageBackend, K: BytesCodec>( &self, backend: &mut B, key: &K, value: &V, ) -> Result<()>
Inserts a typed key-value pair (with explicit backend).
Sourcepub fn remove_typed_key_in<B: StorageBackend, K: BytesCodec>(
&self,
backend: &mut B,
key: &K,
) -> Result<()>
pub fn remove_typed_key_in<B: StorageBackend, K: BytesCodec>( &self, backend: &mut B, key: &K, ) -> Result<()>
Removes a typed key (with explicit backend).
Sourcepub fn contains_key(&self, key: &[u8]) -> Result<bool>
pub fn contains_key(&self, key: &[u8]) -> Result<bool>
Checks if a key exists (production, uses HostStorage).
Sourcepub fn get(&self, key: &[u8]) -> Result<Option<V>>
pub fn get(&self, key: &[u8]) -> Result<Option<V>>
Gets the value for a key (production, uses HostStorage).
Sourcepub fn insert(&self, key: &[u8], value: &V) -> Result<()>
pub fn insert(&self, key: &[u8], value: &V) -> Result<()>
Inserts a key-value pair (production, uses HostStorage).
Sourcepub fn remove(&self, key: &[u8]) -> Result<()>
pub fn remove(&self, key: &[u8]) -> Result<()>
Removes a key-value pair (production, uses HostStorage).
Sourcepub fn contains_typed_key<K: BytesCodec>(&self, key: &K) -> Result<bool>
pub fn contains_typed_key<K: BytesCodec>(&self, key: &K) -> Result<bool>
Checks if a typed key exists (production, uses HostStorage).
Sourcepub fn get_typed_key<K: BytesCodec>(&self, key: &K) -> Result<Option<V>>
pub fn get_typed_key<K: BytesCodec>(&self, key: &K) -> Result<Option<V>>
Gets the value for a typed key (production, uses HostStorage).
Sourcepub fn insert_typed_key<K: BytesCodec>(&self, key: &K, value: &V) -> Result<()>
pub fn insert_typed_key<K: BytesCodec>(&self, key: &K, value: &V) -> Result<()>
Inserts a typed key-value pair (production, uses HostStorage).
Sourcepub fn remove_typed_key<K: BytesCodec>(&self, key: &K) -> Result<()>
pub fn remove_typed_key<K: BytesCodec>(&self, key: &K) -> Result<()>
Removes a typed key (production, uses HostStorage).