Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SwayStar123 committed Jan 8, 2024
1 parent bd06ad5 commit 4040f62
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 61 deletions.
61 changes: 61 additions & 0 deletions sway-lib-std/src/storage/storage_map.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library;

use ::hash::*;
use ::option::Option;
use ::storage::storage_api::*;
use ::storage::storage_key::*;

Expand Down Expand Up @@ -125,4 +126,64 @@ where
let key = sha256((key, self.slot));
clear::<V>(key, 0)
}

/// Inserts a key-value pair into the map, if a value does not already exist for the key.
///
/// # Arguments
///
/// * `key`: [K] - The key to which the value is paired.
/// * `value`: [V] - The value to be stored.
///
/// # Returns
///
/// * [Option<V>] - The value previously stored at `key`, if any.
///
/// # Number of Storage Accesses
///
/// * Reads: `1`
/// * Writes: `1`
///
/// # Examples
///
/// ```sway
/// storage {
/// map: StorageMap<u64, bool> = StorageMap {}
/// }
///
/// fn foo() {
/// let key = 5_u64;
/// let value = true;
/// storage.map.insert(key, value);
/// let retrieved_value = storage.map.get(key).read();
/// assert(value == retrieved_value);
///
/// let new_value = false;
/// let old_value = storage.map.try_insert(key, new_value);
/// assert(old_value == Option::Some(value)); // The old value is returned.
/// let retrieved_value = storage.map.get(key).read();
/// assert(value == retrieved_value); // New value was not inserted, as a value already existed.
///
/// let key2 = 10_u64;
/// let old_value = storage.map.try_insert(key2, new_value);
/// assert(old_value == Option::None); // No old value is returned.
/// let retrieved_value = storage.map.get(key2).read();
/// assert(new_value == retrieved_value); // New value was inserted, as no value existed prior.
/// }
/// ```
#[storage(read, write)]
pub fn try_insert(self, key: K, value: V) -> Option<V>
where K: Hash,
{
let key = sha256((key, self.field_id));

let val = read::<V>(key, 0);

match val {
Option::Some(v) => {Option::Some(v)},
Option::None => {
write::<V>(key, 0, value);
Option::None
}
}
}
}
1 change: 1 addition & 0 deletions test/src/sdk-harness/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/src/sdk-harness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sha2 = "0.10"
sha3 = "0.10.1"
tai64 = { version = "4.0", features = ["serde"] }
tokio = { version = "1.12", features = ["rt", "macros"] }
paste = "1.0.14"

[[test]]
harness = true
Expand Down
86 changes: 43 additions & 43 deletions test/src/sdk-harness/test_projects/harness.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
// Add test modules here:

mod abi_impl_methods_callable;
mod auth;
mod block;
mod call_frames;
mod configurables_in_contract;
mod configurables_in_script;
mod context;
mod contract_bytecode;
mod ec_recover;
mod ec_recover_and_match_predicate;
mod evm;
mod evm_ec_recover;
mod exponentiation;
mod generics_in_abi;
mod hashing;
mod logging;
mod low_level_call;
mod low_level_call_bytes;
mod messages;
mod methods;
mod option_field_order;
mod option_in_abi;
mod parsing_logs;
mod predicate_data_simple;
mod predicate_data_struct;
mod registers;
mod result_in_abi;
mod script_data;
mod storage;
mod storage_access;
mod storage_bytes;
mod storage_init;
// mod abi_impl_methods_callable;
// mod auth;
// mod block;
// mod call_frames;
// mod configurables_in_contract;
// mod configurables_in_script;
// mod context;
// mod contract_bytecode;
// mod ec_recover;
// mod ec_recover_and_match_predicate;
// mod evm;
// mod evm_ec_recover;
// mod exponentiation;
// mod generics_in_abi;
// mod hashing;
// mod logging;
// mod low_level_call;
// mod low_level_call_bytes;
// mod messages;
// mod methods;
// mod option_field_order;
// mod option_in_abi;
// mod parsing_logs;
// mod predicate_data_simple;
// mod predicate_data_struct;
// mod registers;
// mod result_in_abi;
// mod script_data;
// mod storage;
// mod storage_access;
// mod storage_bytes;
// mod storage_init;
mod storage_map;
mod storage_map_nested;
mod storage_string;
mod storage_vec;
mod storage_vec_nested;
mod storage_vec_to_vec;
mod superabi;
mod superabi_supertrait;
mod token_ops;
mod tx_fields;
mod type_aliases;
mod vec_in_abi;
// mod storage_map_nested;
// mod storage_string;
// mod storage_vec;
// mod storage_vec_nested;
// mod storage_vec_to_vec;
// mod superabi;
// mod superabi_supertrait;
// mod token_ops;
// mod tx_fields;
// mod type_aliases;
// mod vec_in_abi;
2 changes: 2 additions & 0 deletions test/src/sdk-harness/test_projects/storage_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use fuels::{
types::{Bits256, SizedAsciiString},
};

pub mod try_insert;

abigen!(Contract(
name = "TestStorageMapContract",
abi = "test_projects/storage_map/out/debug/storage_map-abi.json",
Expand Down
Loading

0 comments on commit 4040f62

Please sign in to comment.