Skip to main content

StorageMap

Struct StorageMap 

Source
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 implement Codec32 for 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>

Source

pub const fn new(namespace: Namespace) -> Self

Creates a new map with the specified namespace.

§Arguments
  • namespace - Unique 32-byte identifier for this map
Source

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.

Source

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.

Source

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.

Source

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

Source

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.

Source

pub fn insert_in<B: StorageBackend>( &self, backend: &mut B, key: &[u8], value: &V, ) -> Result<()>

Inserts a key-value pair (with explicit backend).

Source

pub fn remove_in<B: StorageBackend>( &self, backend: &mut B, key: &[u8], ) -> Result<()>

Removes a key-value pair (with explicit backend).

Source

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.

Source

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

Source

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

Source

pub fn remove_typed_key_in<B: StorageBackend, K: BytesCodec>( &self, backend: &mut B, key: &K, ) -> Result<()>

Removes a typed key (with explicit backend).

Source

pub fn contains_key(&self, key: &[u8]) -> Result<bool>

Checks if a key exists (production, uses HostStorage).

Source

pub fn get(&self, key: &[u8]) -> Result<Option<V>>

Gets the value for a key (production, uses HostStorage).

Source

pub fn insert(&self, key: &[u8], value: &V) -> Result<()>

Inserts a key-value pair (production, uses HostStorage).

Source

pub fn remove(&self, key: &[u8]) -> Result<()>

Removes a key-value pair (production, uses HostStorage).

Source

pub fn contains_typed_key<K: BytesCodec>(&self, key: &K) -> Result<bool>

Checks if a typed key exists (production, uses HostStorage).

Source

pub fn get_typed_key<K: BytesCodec>(&self, key: &K) -> Result<Option<V>>

Gets the value for a typed key (production, uses HostStorage).

Source

pub fn insert_typed_key<K: BytesCodec>(&self, key: &K, value: &V) -> Result<()>

Inserts a typed key-value pair (production, uses HostStorage).

Source

pub fn remove_typed_key<K: BytesCodec>(&self, key: &K) -> Result<()>

Removes a typed key (production, uses HostStorage).

Auto Trait Implementations§

§

impl<V> Freeze for StorageMap<V>

§

impl<V> RefUnwindSafe for StorageMap<V>
where V: RefUnwindSafe,

§

impl<V> Send for StorageMap<V>
where V: Send,

§

impl<V> Sync for StorageMap<V>
where V: Sync,

§

impl<V> Unpin for StorageMap<V>
where V: Unpin,

§

impl<V> UnsafeUnpin for StorageMap<V>

§

impl<V> UnwindSafe for StorageMap<V>
where V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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, U> Into<U> for T
where 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 for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.