pub struct Mapping<K, V> { /* private fields */ }
Expand description

A mapping of key-value pairs directly into contract storage.

Important

If you use this data structure you must use the function ink_lang::utils::initialize_contract in your contract’s constructors!

Note that in order to use this function your contract’s storage struct must implement the SpreadAllocate trait.

This is an example of how you can do this:


use ink_storage::{traits::SpreadAllocate, Mapping};

#[ink(storage)]
#[derive(SpreadAllocate)]
pub struct MyContract {
    balances: Mapping<AccountId, Balance>,
}

impl MyContract {
    #[ink(constructor)]
    pub fn new() -> Self {
        ink_lang::utils::initialize_contract(Self::new_init)
    }

    /// Default initializes the contract.
    fn new_init(&mut self) {
        let caller = Self::env().caller();
        let value: Balance = Default::default();
        self.balances.insert(&caller, &value);
    }
}

More usage examples can be found in the ink! examples.

Implementations

Insert the given value to the contract storage.

Insert the given value to the contract storage.

Returns the size of the pre-existing value at the specified key if any.

Get the value at key from the contract storage.

Returns None if no value exists at the given key.

Get the size of a value stored at key in the contract storage.

Returns None if no value exists at the given key.

Checks if a value is stored at the given key in the contract storage.

Returns None if no value exists at the given key.

Clears the value at key from storage.

Trait Implementations

Formats the value using the given formatter. Read more

We implement this manually because the derived implementation adds trait bounds.

Returns the “default value” for a type. Read more

Default initializes the implementing type using spread layout. Read more

The footprint of the type. Read more

Indicates whether a type requires deep clean-up of its state meaning that a clean-up routine has to decode an entity into an instance in order to eventually recurse upon its tear-down. This is not required for the majority of primitive data types such as i32, however types such as storage::Box that might want to forward the clean-up procedure to their inner T require a deep clean-up. Read more

Pulls an instance of Self from the contract storage. Read more

Pushes an instance of Self to the contract storage. Read more

Clears an instance of Self from the contract storage. Read more

Returns the static storage layout of Self. Read more

The type identifying for which type info is provided. Read more

Returns the static type identifier for Self.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.