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

struct as mapping key #49

Merged
merged 3 commits into from
Jul 16, 2023
Merged

struct as mapping key #49

merged 3 commits into from
Jul 16, 2023

Conversation

StarkFishinator
Copy link
Collaborator

No description provided.

mod StructAsMappingKey {
use starknet::get_caller_address;
use starknet::ContractAddress;
use poseidon::poseidon_hash_span;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is this the correct function that I should use? @enitrat

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes


#[external]
fn set(key: User, value: felt252) {
_map::write(poseidon_hash_span(key.span()), value);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It does not work as it expects a span, but span is only for arrays and key is not an array (it's a struct), so I guess we have to use another function

Copy link
Collaborator

Choose a reason for hiding this comment

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

your struct needs to derive Serde and then you can call Serde::<User>::serialize(key,serialized_struct) to make it an array (check corelib to see the correct syntax to use Serde

@enitrat
Copy link
Collaborator

enitrat commented Jun 30, 2023

Awesome! It works when deriving the Drop trait. Could you create a file with tests for the CI, as it's done in other examples?
Also, for the markdown file with explanations, you can structure it this way:

  • Introduction (present a use case that validates this requirement - can you think of one in a context where it makes sense to hash fields together and use it as a key?)
  • Motivation (why are we doing this? e.g. we can't use structs as mapping keys)
  • Solution (serialize fields, hash them together, and use it as key)
  • Example

This would be the perfect recipe for an easy-to-understand and meaningful example!

@enitrat enitrat force-pushed the feat---struct-as-mapping-key branch from 4ccf1a5 to 3ac2965 Compare July 16, 2023 13:43
@enitrat enitrat marked this pull request as ready for review July 16, 2023 13:43
@enitrat enitrat force-pushed the feat---struct-as-mapping-key branch from 3ac2965 to 64aa1fa Compare July 16, 2023 13:44
@enitrat enitrat merged commit d3cd560 into main Jul 16, 2023
@julio4 julio4 deleted the feat---struct-as-mapping-key branch August 23, 2023 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants