Skip to content

Commit

Permalink
Add StorageMap to the standard library prelude (FuelLabs#3133)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadfawaz authored Oct 26, 2022
1 parent 74e6377 commit b709d67
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 22 deletions.
6 changes: 1 addition & 5 deletions docs/src/common-collections/storage_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ Storage maps are useful when you want to look up data not by using an index, as

Similarly to `StorageVec<T>`, `StorageMap<K, V>` can only be used in a contract because only contracts are allowed to access persistent storage.

In order to use `StorageMap<K, V>`, you must first import `StorageMap` as follows:

```sway
{{#include ../../../examples/storage_map/src/main.sw:storage_map_import}}
```
`StorageMap<T>` is included in the [standard library prelude](../introduction/standard_library.md#standard-library-prelude) which means that there is no need to import it manually.

## Creating a New Storage Map

Expand Down
5 changes: 3 additions & 2 deletions docs/src/introduction/standard_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ The standard library is made implicitly available to all Forc projects created u
Importing items from the standard library can be done using the `use` keyword, just as importing items from any Sway project. For example:

```sway
use std::storage::StorageMap;
use std::storage::StorageVec;
```

This imports the `StorageMap` type into the current namespace.
This imports the `StorageVec` type into the current namespace.

## Standard Library Prelude

Expand All @@ -30,6 +30,7 @@ The current version of the prelude lives in [`std::prelude`](https://github.com/
- [`std::contract_id::ContractId`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/contract_id.sw), a wrapper around the `b256` type representing the ID of a contract.
- [`std::identity::Identity`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/identity.sw), an enum with two possible variants: `Address: Address` and `ContractId: ContractId`.
- [`std::vec::Vec`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/vec.sw), a growable, heap-allocated vector.
- [`std::storage::StorageMap`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/storage.sw), a key-value mapping in contract storage.
- [`std::option::Option`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/option.sw), an enum which expresses the presence or absence of a value.
- [`std::result::Result`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/result.sw), an enum for functions that may succeed or fail.
- [`std::assert::assert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw), a function that reverts the VM if the condition provided to it is `false`.
Expand Down
1 change: 0 additions & 1 deletion examples/storage_map/Forc.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[[package]]
name = 'core'
source = 'path+from-root-E6A0C4C40B2C3193'
dependencies = []

[[package]]
name = 'std'
Expand Down
3 changes: 0 additions & 3 deletions examples/storage_map/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
contract;

// ANCHOR: storage_map_import
use std::storage::StorageMap;
// ANCHOR_END: storage_map_import
use std::logging::log;

storage {
Expand Down
1 change: 0 additions & 1 deletion examples/subcurrency/Forc.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[[package]]
name = 'core'
source = 'path+from-root-237819BD98C87E84'
dependencies = []

[[package]]
name = 'std'
Expand Down
2 changes: 1 addition & 1 deletion examples/subcurrency/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ANCHOR: body
contract;

use std::{chain::auth::{AuthError, msg_sender}, hash::sha256, logging::log, storage::StorageMap};
use std::{chain::auth::{AuthError, msg_sender}, hash::sha256, logging::log};

////////////////////////////////////////
// Event declarations
Expand Down
1 change: 0 additions & 1 deletion sway-lib-std/src/address.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
library address;
//! A wrapper around the b256 type to help enhance type-safety.
/// The Address type, a struct wrappper around the inner `value`.
pub struct Address {
value: b256,
Expand Down
11 changes: 8 additions & 3 deletions sway-lib-std/src/prelude.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ library prelude;
//! Defines the Sway standard library prelude.
//! The prelude consists of implicitly available items,
//! for which `use` is not required.
/* Blockchain types */
use ::address::Address;
use ::contract_id::ContractId;
use ::identity::Identity;

/* Collections */
use ::storage::StorageMap;
use ::vec::Vec;

/* Error handling */
use ::assert::assert;
use ::option::Option;
use ::result::Result;
use ::assert::assert;
use ::revert::require;
use ::revert::revert;
use ::revert::{require, revert};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract;

use std::storage::{StorageMap, StorageVec};
use std::storage::StorageVec;

struct Wrapper {
map1: StorageMap<u64, u64>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
contract;

use std::storage::StorageMap;

struct Data {
value: u64,
}
Expand Down
2 changes: 0 additions & 2 deletions test/src/sdk-harness/test_projects/storage_map/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
contract;

use std::storage::StorageMap;

pub struct Struct {
x: u32,
y: b256,
Expand Down

0 comments on commit b709d67

Please sign in to comment.