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

Add new() function to StorageKey #5096

Merged
merged 4 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions sway-lib-std/src/storage/storage_key.sw
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,43 @@ impl<T> StorageKey<T> {
pub fn write(self, value: T) {
write(self.slot, self.offset, value);
}

/// Create a new `StorageKey`.
///
/// # Arguments
///
/// * `slot`: [b256] - The assigned location in storage for the new `StorageKey`.
/// * `offset`: [u64] - The assigned offset based on the data structure `T` for the new `StorageKey`.
/// * `field_id`: [b256] - A unqiue identifier for the new `StorageKey`.
bitzoic marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Returns
///
/// * [StorageKey] - The newly create `StorageKey`.
///
/// # Examples
///
/// ```sway
/// use std::{constants::ZERO_B256, hash::sha256};
///
/// fn foo() {
/// let my_key = StorageKey::<u64>::new(ZERO_B256, 0, sha256(ZERO_B256));
/// assert(my_key.slot == ZERO_B256);
/// }
/// ```
pub fn new(slot: b256, offset: u64, field_id: b256) -> Self {
Self {
slot, offset, field_id
}
}
}

#[test]
fn test_storage_key_new() {
use ::constants::ZERO_B256;
use ::assert::assert;

let key = StorageKey::<u64>::new(ZERO_B256, 0, ZERO_B256);
assert(key.slot == ZERO_B256);
assert(key.offset == 0);
assert(key.field_id == ZERO_B256);
}
6 changes: 1 addition & 5 deletions sway-lib-std/src/storage/storage_map.sw
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ impl<K, V> StorageKey<StorageMap<K, V>> where K: Hash {
/// }
/// ```
pub fn get(self, key: K) -> StorageKey<V> where K: Hash {
StorageKey {
slot: sha256((key, self.field_id)),
offset: 0,
field_id: sha256((key, self.field_id)),
}
bitzoic marked this conversation as resolved.
Show resolved Hide resolved
StorageKey::<V>::new(sha256((key, self.field_id)), 0, sha256((key, self.field_id)))
}

/// Clears a value previously stored using a key
Expand Down
18 changes: 3 additions & 15 deletions sway-lib-std/src/storage/storage_vec.sw
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,7 @@ impl<V> StorageKey<StorageVec<V>> {
return None;
}

Some(StorageKey {
slot: sha256((index, self.field_id)),
offset: 0,
field_id: sha256((index, self.field_id)),
})
Some(StorageKey::<V>::new(sha256((index, self.field_id)), 0, sha256((index, self.field_id))))
}

/// Removes the element in the given index and moves all the elements in the following indexes
Expand Down Expand Up @@ -575,11 +571,7 @@ impl<V> StorageKey<StorageVec<V>> {
pub fn first(self) -> Option<StorageKey<V>> {
match read::<u64>(self.field_id, 0).unwrap_or(0) {
0 => None,
_ => Some(StorageKey {
slot: sha256((0, self.field_id)),
offset: 0,
field_id: sha256((0, self.field_id)),
}),
_ => Some(StorageKey::<V>::new(sha256((0, self.field_id)), 0, sha256((0, self.field_id)))),
}
}

Expand Down Expand Up @@ -614,11 +606,7 @@ impl<V> StorageKey<StorageVec<V>> {
pub fn last(self) -> Option<StorageKey<V>> {
match read::<u64>(self.field_id, 0).unwrap_or(0) {
0 => None,
len => Some(StorageKey {
slot: sha256((len - 1, self.field_id)),
offset: 0,
field_id: sha256((0, self.field_id)),
}),
len => Some(StorageKey::<V>::new(sha256((len - 1, self.field_id)), 0, sha256((0, self.field_id)))),
}
}

Expand Down
Loading