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

Core Lib Documentation: add missing import + remove nested mappings in Map #7051

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 18 additions & 14 deletions corelib/src/starknet/storage/map.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,31 @@
//! Basic usage with a single mapping:
//!
//! ```
//! use core::starknet::ContractAddress;
//! use core::starknet::storage::{Map, StorageMapReadAccess, StoragePathEntry,
//! StoragePointerReadAccess};
//!
//! #[storage]
//! struct Storage {
//! balances: Map<ContractAddress, u256>,
//! }
//!
//! fn read_storage(self: @ContractState) {
//! fn read_storage(self: @ContractState, address: ContractAddress) {
//! let balance = self.balances.read(address);
//! let balance = self.balances.entry(address).read();
//! }
//! ```
//!
//! Nested mappings:
//! For nested mappings, only path-based access is available:
//!
//! ```
//! #[storage]
//! struct Storage {
//! allowances: Map<ContractAddress, Map<ContractAddress, u256>>,
//! }
//!
//! fn read_storage(self: @ContractState) {
//! fn read_storage(self: @ContractState, owner: ContractAddress, spender: ContractAddress) {
//! let allowance = self.allowances.entry(owner).entry(spender).read();
//! let allowance = self.allowances.read(owner, spender);
//! }
//! ```

Expand All @@ -97,18 +100,17 @@ use super::{
/// # Examples
///
/// ```
/// use core::starknet::ContractAddress;
/// use core::starknet::storage::{Map, StorageMapReadAccess};
///
/// #[storage]
/// struct Storage {
/// balances: Map<ContractAddress, u256>,
/// allowances: Map<ContractAddress, Map<ContractAddress, u256>>,
/// }
///
/// fn read_storage(self: @ContractState) {
/// fn read_storage(self: @ContractState, address: ContractAddress) {
/// // Read from single mapping
/// let balance = self.balances.read(address);
///
/// // Read from nested mapping
/// let allowance = self.allowances.read(owner, spender);
/// }
/// ```
pub trait StorageMapReadAccess<TMemberState> {
Expand All @@ -124,18 +126,17 @@ pub trait StorageMapReadAccess<TMemberState> {
/// # Examples
///
/// ```
/// use core::starknet::ContractAddress;
/// use core::starknet::storage::{Map, StorageMapWriteAccess};
///
/// #[storage]
/// struct Storage {
/// balances: Map<ContractAddress, u256>,
/// allowances: Map<ContractAddress, Map<ContractAddress, u256>>,
/// }
///
/// fn write_storage(ref self: ContractState) {
/// fn write_storage(ref self: ContractState, address: ContractAddress) {
/// // Write to single mapping
/// self.balances.write(address, 100);
///
/// // Write to nested mapping
/// self.allowances.write(owner, spender, 50);
/// }
/// ```
pub trait StorageMapWriteAccess<TMemberState> {
Expand All @@ -153,6 +154,9 @@ pub trait StorageMapWriteAccess<TMemberState> {
/// # Examples
///
/// ```
/// use core::starknet::ContractAddress;
/// use core::starknet::storage::{Map, StoragePathEntry};
///
/// #[storage]
/// struct Storage {
/// balances: Map<ContractAddress, u256>,
Expand Down