Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
2a7e168
Initial commit - storagevec
SwayStar123 Jun 19, 2022
23f925f
added an error and comments and documentation
SwayStar123 Jun 19, 2022
81a0ea6
fixed the order of the Result types
SwayStar123 Jun 19, 2022
7985f55
added suggested functions
SwayStar123 Jun 19, 2022
28e9be3
removed unncessary code
SwayStar123 Jun 19, 2022
6b629db
renamed remove_index
SwayStar123 Jun 19, 2022
a4a9093
added swap remove and removed unncessary annotations
SwayStar123 Jun 19, 2022
f09060f
moved storage_vec to storage and started on tests
SwayStar123 Jun 22, 2022
9c2a914
built some of the contract -- WIP
SwayStar123 Jun 22, 2022
37ed447
made it so swap_remove returns the removed element
SwayStar123 Jun 22, 2022
51961a7
removed extra ) and added all the test functions
SwayStar123 Jun 22, 2022
c42229d
fixed a syntax error
SwayStar123 Jun 22, 2022
8b059b3
changed store to storage
SwayStar123 Jun 22, 2022
082ded9
removed all other types for now
SwayStar123 Jun 22, 2022
f6315fa
made storagevecerror public
SwayStar123 Jun 22, 2022
9ddaf7d
removed unncessary code to streamline bugfixing
SwayStar123 Jun 22, 2022
b22e00d
fixed annotations
SwayStar123 Jun 22, 2022
049b162
made all the test contracts
SwayStar123 Jun 22, 2022
cbfbedc
changed build.sh to include all the storage_vec projs
SwayStar123 Jun 23, 2022
75c414b
updated fuels version
SwayStar123 Jun 23, 2022
0da645a
unwrapped all results and options in the contract
SwayStar123 Jun 30, 2022
da514b7
reduced fuels version
SwayStar123 Jun 30, 2022
5f50e40
merge master to storage_vec branch (#2183)
SwayStar123 Jun 30, 2022
c04be39
merge master to storage_vec (#2184)
SwayStar123 Jun 30, 2022
3ad6fca
removed unncessary use
SwayStar123 Jun 30, 2022
6bcfac1
Merge branch 'master' into storage_vec-swaystar123
SwayStar123 Jun 30, 2022
859b0f6
added one test
SwayStar123 Jun 30, 2022
7092230
deleted all contracts except u8s
SwayStar123 Jun 30, 2022
ed76626
fixed bug in pop
SwayStar123 Jun 30, 2022
f90d304
failing test for some reason
SwayStar123 Jun 30, 2022
b192910
.
SwayStar123 Jun 30, 2022
3293293
fixed some brackets
SwayStar123 Jun 30, 2022
7f23319
fixed a mistake of > where it should be >=
SwayStar123 Jul 2, 2022
0ad4143
added 2 remaining tests
SwayStar123 Jul 2, 2022
1c32489
Update test/src/sdk-harness/test_artifacts/storage_vec/svec_u8/Cargo.…
SwayStar123 Jul 4, 2022
9dc6a5c
removed storagevecerrors
SwayStar123 Jul 4, 2022
a106f1c
assert was the other way round
SwayStar123 Jul 4, 2022
f5b78c8
added a variable for repeated code
SwayStar123 Jul 4, 2022
d242c9c
merged use
SwayStar123 Jul 4, 2022
c9ce95f
formatting
SwayStar123 Jul 4, 2022
578d9e3
expanded the testing
SwayStar123 Jul 6, 2022
27979d8
rearranged file structure
SwayStar123 Jul 6, 2022
d9f17e3
cargo fmt
SwayStar123 Jul 6, 2022
f51da9f
adjusted assert for removes
SwayStar123 Jul 7, 2022
57e4aef
removed non test and shortened the contract code
SwayStar123 Jul 7, 2022
6e65c39
added cant_get test
SwayStar123 Jul 7, 2022
5796f40
added a todo
SwayStar123 Jul 7, 2022
bcae640
Improved documentation
SwayStar123 Jul 9, 2022
a6b6ab8
updated tests with preconditional tests
SwayStar123 Jul 9, 2022
bfae279
added initializer
SwayStar123 Jul 9, 2022
2ea82e7
Merge branch 'master' into storage_vec-swaystar123
SwayStar123 Jul 9, 2022
06805d8
updated functions with sdk release
SwayStar123 Jul 9, 2022
5adddc5
mdae build.sh more generic
SwayStar123 Jul 9, 2022
a1b3843
fixed build script
SwayStar123 Jul 9, 2022
4b52750
fix 2 electric boogaloo
SwayStar123 Jul 9, 2022
3d62d03
updated remove and insert tests
SwayStar123 Jul 11, 2022
ab32539
added len checks
SwayStar123 Jul 11, 2022
b13a0a3
FINALLY ADDED TESTS FOR ALL TYPES OMG
SwayStar123 Jul 11, 2022
d514b19
Merge branch 'master' into storage_vec-swaystar123
SwayStar123 Jul 11, 2022
d6601a2
Merge branch 'master' into storage_vec-swaystar123
SwayStar123 Jul 12, 2022
e2d1deb
alphabetical order
SwayStar123 Jul 13, 2022
f39495d
Merge branch 'storage_vec-swaystar123' of https://github.com/FuelLabs…
SwayStar123 Jul 13, 2022
01eb00a
cargo fmt + changed the b256 consts
SwayStar123 Jul 13, 2022
7468ad6
removed unncessary len checks
SwayStar123 Jul 13, 2022
114e498
Merge branch 'master' into storage_vec-swaystar123
SwayStar123 Jul 13, 2022
477b5b0
Merge branch 'master' into storage_vec-swaystar123
adlerjohn Jul 14, 2022
de75364
Merge branch 'master' into storage_vec-swaystar123
mohammadfawaz Jul 14, 2022
a73f659
Merge branch 'master' into storage_vec-swaystar123
SwayStar123 Jul 14, 2022
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
84 changes: 84 additions & 0 deletions sway-lib-std/src/vec.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ library vec;

use ::alloc::{alloc, realloc};
use ::intrinsics::size_of;
use ::hash::sha256;
use ::mem::{read, write};
use ::option::Option;
use ::result::Result;
use ::storage::{store, get};

struct RawVec<T> {
ptr: u64,
Expand Down Expand Up @@ -142,3 +145,84 @@ impl<T> Vec<T> {
self.len == 0
}
}

/// TODO: Add proper errors

pub struct StorageVec<V> {}

impl<V> StorageVec<V> {
#[storage(read, write)]
fn push(self, value: V) {
let len = get::<u64>(__get_storage_key());
let key = sha256((len, __get_storage_key()));
store::<V>(key, value);
store(__get_storage_key(), len + 1);
}

#[storage(read, write)]
fn pop(self) -> Option<V> {
let len = get::<u64>(__get_storage_key());
if len == 0 {
return Option::None;
}

let key = sha256((len, __get_storage_key()));
store(__get_storage_key(), len - 1);

Option::Some(get::<V>(key))
}

#[storage(read)]
fn get(self, index: u64) -> Option<V> {
let len = get::<u64>(__get_storage_key());
if len <= index {
return Option::None;
}

let key = sha256((index, __get_storage_key()));
Option::Some(get::<V>(key))
}

#[storage(read, write)]
fn remove_index(self, index: u64) -> Result<(), ()> {
let len = get::<u64>(__get_storage_key());
if len <= index {
return Result::Err(());
}

let mut count = index + 1;
while count < len {
let key = sha256((count - 1, __get_storage_key()));
// shifts all the values down one index
store::<V>(key, get::<V>(sha256((count, __get_storage_key()))));

count += 1;
}

store(__get_storage_key(), len - 1);
Result::Ok(())
}

#[storage(read, write)]
fn insert(self, index: u64, value: V) -> Result<(), ()> {
let len = get::<u64>(__get_storage_key());
if index >= len {
return Result::Err(());
}

let mut count = len;
while count > index {
let key = sha256((count + 1, __get_storage_key()));
// shifts all the values up one index
store::<V>(key, get::<V>(sha256((count, __get_storage_key()))));

count -= 1
}

let key = sha256((index, __get_storage_key()));
store::<V>(key, value);

store(__get_storage_key(), len + 1);
Result::Ok(())
}
}