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

Add new() function to StorageKey #5096

merged 4 commits into from
Sep 9, 2023

Conversation

bitzoic
Copy link
Member

@bitzoic bitzoic commented Sep 6, 2023

Description

When creating complex storage types, storing of a StorageKey is sometimes needed. In these cases a new() function is appropriate.

For example, when we have the following storage block:

storage {
    name: StorageMap<u64, StorageKey<StorageString>> = StorageMap {},
}

In order to store a StorageString in a StorageMap, the StorageKey<StorageString> annotation is needed.

From there, we can store a String by creating a StorageKey, storing a String at said key, and then storing the StorageKey in the StorageMap:

let string_key: StorageKey<StorageString> = StorageKey {
    slot: sha256(0),
    offset: 0,
    field_id: sha256(1),
};
string_key.write_slice(String::from_ascii_str("my_name"));
storage.name.insert(0, string_key);

The introduction of new() would change this to the following:

let string_key: StorageKey<StorageString> = StorageKey::new(
     sha256(0), 
     0, 
     sha256(1)
);
string_key.write_slice(String::from_ascii_str("my_name"));
storage.name.insert(0, string_key);

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@bitzoic bitzoic added lib: std Standard library storage Anything related to how we treat the storage in Sway labels Sep 6, 2023
@bitzoic bitzoic requested review from IGI-111 and a team September 6, 2023 12:31
@bitzoic bitzoic self-assigned this Sep 6, 2023
@bitzoic bitzoic changed the title Add new() function to StorageKey Add new() function to StorageKey Sep 6, 2023
K1-R1
K1-R1 previously approved these changes Sep 6, 2023
Copy link
Contributor

@Braqzen Braqzen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused about your comments in the issue regarding the usage of a String and a StorageString. The type in the map is StorageString but you create a string that gets assigned to some arbitrary key and then via magic it becomes the StorageString type.

I've not looking into handling StorageKeys so there's probably something going on there.

sway-lib-std/src/storage/storage_key.sw Outdated Show resolved Hide resolved
@bitzoic bitzoic enabled auto-merge (squash) September 8, 2023 09:38
@bitzoic bitzoic merged commit e00597c into master Sep 9, 2023
28 checks passed
@bitzoic bitzoic deleted the bitzoic-storage-key branch September 9, 2023 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib: std Standard library storage Anything related to how we treat the storage in Sway
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants