Skip to content

Commit

Permalink
Introduce index() and index_assign() to StorageMap and showcase nested
Browse files Browse the repository at this point in the history
storage vecs using both `get/push` and the `[]` operator.
  • Loading branch information
mohammadfawaz committed Mar 25, 2023
1 parent c7a2e45 commit 9dab5f8
Show file tree
Hide file tree
Showing 63 changed files with 2,471 additions and 0 deletions.
548 changes: 548 additions & 0 deletions sway-lib-std/src/experimental/storage.sw

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-F1A9B05C249770CB'

[[package]]
name = 'std'
source = 'path+from-root-F1A9B05C249770CB'
dependencies = ['core']

[[package]]
name = 'svec_array'
source = 'member'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "svec_array"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
contract;

use core::experimental::storage::*;
use std::experimental::storage::*;

abi MyContract {
#[storage(read, write)]
fn push(value: [u8; 3]);

#[storage(read, write)]
fn pop() -> [u8; 3];

#[storage(read)]
fn get(index: u64) -> [u8; 3];

#[storage(read, write)]
fn remove(index: u64) -> [u8; 3];

#[storage(read, write)]
fn swap_remove(index: u64) -> [u8; 3];

#[storage(read, write)]
fn set(index: u64, value: [u8; 3]);

#[storage(read, write)]
fn insert(index: u64, value: [u8; 3]);

#[storage(read)]
fn len() -> u64;

#[storage(read)]
fn is_empty() -> bool;

#[storage(write)]
fn clear();
}

storage {
my_vec: StorageVec<[u8; 3]> = StorageVec {},
}

impl MyContract for Contract {
#[storage(read, write)]
fn push(value: [u8; 3]) {
storage.my_vec.push(value);
}

#[storage(read, write)]
fn pop() -> [u8; 3] {
storage.my_vec.pop().unwrap()
}

#[storage(read)]
fn get(index: u64) -> [u8; 3] {
storage.my_vec.get(index).unwrap().read()
}

#[storage(read, write)]
fn remove(index: u64) -> [u8; 3] {
storage.my_vec.remove(index)
}

#[storage(read, write)]
fn swap_remove(index: u64) -> [u8; 3] {
storage.my_vec.swap_remove(index)
}

#[storage(read, write)]
fn set(index: u64, value: [u8; 3]) {
storage.my_vec.set(index, value);
}

#[storage(read, write)]
fn insert(index: u64, value: [u8; 3]) {
storage.my_vec.insert(index, value);
}

#[storage(read)]
fn len() -> u64 {
storage.my_vec.len()
}

#[storage(read)]
fn is_empty() -> bool {
storage.my_vec.is_empty()
}

#[storage(write)]
fn clear() {
storage.my_vec.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-BAD4826C86E6CE3D'

[[package]]
name = 'std'
source = 'path+from-root-BAD4826C86E6CE3D'
dependencies = ['core']

[[package]]
name = 'svec_b256'
source = 'member'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "svec_b256"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
contract;

use core::experimental::storage::*;
use std::experimental::storage::*;

abi MyContract {
#[storage(read, write)]
fn push(value: b256);

#[storage(read, write)]
fn pop() -> b256;

#[storage(read)]
fn get(index: u64) -> b256;

#[storage(read, write)]
fn remove(index: u64) -> b256;

#[storage(read, write)]
fn swap_remove(index: u64) -> b256;

#[storage(read, write)]
fn set(index: u64, value: b256);

#[storage(read, write)]
fn insert(index: u64, value: b256);

#[storage(read)]
fn len() -> u64;

#[storage(read)]
fn is_empty() -> bool;

#[storage(write)]
fn clear();
}

storage {
my_vec: StorageVec<b256> = StorageVec {},
}

impl MyContract for Contract {
#[storage(read, write)]
fn push(value: b256) {
storage.my_vec.push(value);
}

#[storage(read, write)]
fn pop() -> b256 {
storage.my_vec.pop().unwrap()
}

#[storage(read)]
fn get(index: u64) -> b256 {
storage.my_vec.get(index).unwrap().read()
}

#[storage(read, write)]
fn remove(index: u64) -> b256 {
storage.my_vec.remove(index)
}

#[storage(read, write)]
fn swap_remove(index: u64) -> b256 {
storage.my_vec.swap_remove(index)
}

#[storage(read, write)]
fn set(index: u64, value: b256) {
storage.my_vec.set(index, value);
}

#[storage(read, write)]
fn insert(index: u64, value: b256) {
storage.my_vec.insert(index, value);
}

#[storage(read)]
fn len() -> u64 {
storage.my_vec.len()
}

#[storage(read)]
fn is_empty() -> bool {
storage.my_vec.is_empty()
}

#[storage(write)]
fn clear() {
storage.my_vec.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-112F6FFCFCECE066'

[[package]]
name = 'std'
source = 'path+from-root-112F6FFCFCECE066'
dependencies = ['core']

[[package]]
name = 'svec_bool'
source = 'member'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "svec_bool"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
contract;

use core::experimental::storage::*;
use std::experimental::storage::*;

abi MyContract {
#[storage(read, write)]
fn push(value: bool);

#[storage(read, write)]
fn pop() -> bool;

#[storage(read)]
fn get(index: u64) -> bool;

#[storage(read, write)]
fn remove(index: u64) -> bool;

#[storage(read, write)]
fn swap_remove(index: u64) -> bool;

#[storage(read, write)]
fn set(index: u64, value: bool);

#[storage(read, write)]
fn insert(index: u64, value: bool);

#[storage(read)]
fn len() -> u64;

#[storage(read)]
fn is_empty() -> bool;

#[storage(write)]
fn clear();
}

storage {
my_vec: StorageVec<bool> = StorageVec {},
}

impl MyContract for Contract {
#[storage(read, write)]
fn push(value: bool) {
storage.my_vec.push(value);
}

#[storage(read, write)]
fn pop() -> bool {
storage.my_vec.pop().unwrap()
}

#[storage(read)]
fn get(index: u64) -> bool {
storage.my_vec.get(index).unwrap().read()
}

#[storage(read, write)]
fn remove(index: u64) -> bool {
storage.my_vec.remove(index)
}

#[storage(read, write)]
fn swap_remove(index: u64) -> bool {
storage.my_vec.swap_remove(index)
}

#[storage(read, write)]
fn set(index: u64, value: bool) {
storage.my_vec.set(index, value);
}

#[storage(read, write)]
fn insert(index: u64, value: bool) {
storage.my_vec.insert(index, value);
}

#[storage(read)]
fn len() -> u64 {
storage.my_vec.len()
}

#[storage(read)]
fn is_empty() -> bool {
storage.my_vec.is_empty()
}

#[storage(write)]
fn clear() {
storage.my_vec.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-BA7DA646BFFC59D3'

[[package]]
name = 'std'
source = 'path+from-root-BA7DA646BFFC59D3'
dependencies = ['core']

[[package]]
name = 'svec_enum'
source = 'member'
dependencies = ['std']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "svec_enum"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Loading

0 comments on commit 9dab5f8

Please sign in to comment.