Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation of ink_storage::Mapping #1138

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ quickcheck_macros = "1.0"
itertools = "0.10"
paste = "1.0"

ink_lang = { version = "3.0.0-rc8", path = "../lang/", default-features = false }

[features]
default = ["std"]
std = [
Expand Down
48 changes: 48 additions & 0 deletions crates/storage/src/lazy/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,54 @@ use ink_env::hash::{
use ink_primitives::Key;

/// 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`](https://paritytech.github.io/ink/ink_lang/utils/fn.initialize_contract.html)
/// in your contract's constructors!
///
/// Note that in order to use this function your contract's storage struct must implement the
/// [`SpreadAllocate`](crate::traits::SpreadAllocate) trait.
///
/// This is an example of how you can do this:
/// ```rust
/// # use ink_lang as ink;
/// # use ink_env::{
/// # Environment,
/// # DefaultEnvironment,
/// # };
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
///
/// # #[ink::contract]
/// # mod my_module {
/// 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);
/// }
/// # #[ink(message)]
/// # pub fn my_message(&self) { }
/// }
/// # }
/// ```
///
/// More usage examples can be found [in the ink! examples](https://github.com/paritytech/ink/tree/master/examples).
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct Mapping<K, V> {
offset_key: Key,
Expand Down