Struct ink_storage::Mapping
source · [−]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
sourceimpl<K, V> Mapping<K, V> where
K: PackedLayout,
V: PackedLayout,
impl<K, V> Mapping<K, V> where
K: PackedLayout,
V: PackedLayout,
sourcepub fn insert<Q, R>(&mut self, key: Q, value: &R) where
Q: EncodeLike<K>,
R: EncodeLike<V> + PackedLayout,
pub fn insert<Q, R>(&mut self, key: Q, value: &R) where
Q: EncodeLike<K>,
R: EncodeLike<V> + PackedLayout,
Insert the given value
to the contract storage.
sourcepub fn get<Q>(&self, key: Q) -> Option<V> where
Q: EncodeLike<K>,
pub fn get<Q>(&self, key: Q) -> Option<V> where
Q: EncodeLike<K>,
Get the value
at key
from the contract storage.
Returns None
if no value
exists at the given key
.
sourcepub fn remove<Q>(&self, key: Q) where
Q: EncodeLike<K>,
pub fn remove<Q>(&self, key: Q) where
Q: EncodeLike<K>,
Clears the value at key
from storage.
Trait Implementations
sourceimpl<K, V> Default for Mapping<K, V>
impl<K, V> Default for Mapping<K, V>
We implement this manually because the derived implementation adds trait bounds.
sourceimpl<K, V> SpreadAllocate for Mapping<K, V>
impl<K, V> SpreadAllocate for Mapping<K, V>
sourcefn allocate_spread(ptr: &mut KeyPtr) -> Self
fn allocate_spread(ptr: &mut KeyPtr) -> Self
Default initializes the implementing type using spread layout. Read more
sourceimpl<K, V> SpreadLayout for Mapping<K, V>
impl<K, V> SpreadLayout for Mapping<K, V>
sourceconst REQUIRES_DEEP_CLEAN_UP: bool
const REQUIRES_DEEP_CLEAN_UP: bool
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
sourcefn pull_spread(ptr: &mut KeyPtr) -> Self
fn pull_spread(ptr: &mut KeyPtr) -> Self
Pulls an instance of Self
from the contract storage. Read more
sourcefn push_spread(&self, ptr: &mut KeyPtr)
fn push_spread(&self, ptr: &mut KeyPtr)
Pushes an instance of Self
to the contract storage. Read more
sourcefn clear_spread(&self, ptr: &mut KeyPtr)
fn clear_spread(&self, ptr: &mut KeyPtr)
Clears an instance of Self
from the contract storage. Read more
sourceimpl<K, V> StorageLayout for Mapping<K, V> where
K: TypeInfo + 'static,
V: TypeInfo + 'static,
impl<K, V> StorageLayout for Mapping<K, V> where
K: TypeInfo + 'static,
V: TypeInfo + 'static,
Auto Trait Implementations
impl<K, V> RefUnwindSafe for Mapping<K, V>
impl<K, V> Send for Mapping<K, V>
impl<K, V> Sync for Mapping<K, V>
impl<K, V> Unpin for Mapping<K, V>
impl<K, V> UnwindSafe for Mapping<K, V>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more