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

Implement PrimaryKey for all &T where T implements PrimaryKey. #30

Merged
merged 3 commits into from
Mar 14, 2023

Conversation

0xekez
Copy link
Contributor

@0xekez 0xekez commented Jan 31, 2023

    for<'a> &'a (K, u64): PrimaryKey<'a>,

This allows having type constraints like this for types that wrap storage-plus. In english: for all lifetimes 'a &'a (K, u64) is a valid key. I need this generic in order to implement cw-future-map.

@0xekez
Copy link
Contributor Author

0xekez commented Feb 7, 2023

i am going to fork pending some resolution to how to land PRs into CosmWasm. no bad vibes, and i'd like to continue to contribute to and work with the CosmWasm repositories when there is a way to do so again.

relevant PRs:

Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

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

Thank you for this PR.

To be honest, I am not entirely sure if using references for primary keys is a good thing. What's the best practive for e.g. Map<Addr> vs. Map<&Addr>? You will not be able to deserialize into a key that is a reference.

@maurolacy do you know why both impl<'a> PrimaryKey<'a> for Addr and impl<'a> PrimaryKey<'a> for &'a Addr exist and if that's desired?

@webmaster128 webmaster128 requested a review from maurolacy March 2, 2023 13:41
@0xekez
Copy link
Contributor Author

0xekez commented Mar 2, 2023

@webmaster128 my understanding is that because serialization takes a reference to the type being serialized, it is more efficient to use references as keys as (1) the underlying serialization doesn't change and (2) you can avoid moves (and in turn clones) when calling methods on the map.

Copy link
Collaborator

@chipshort chipshort left a comment

Choose a reason for hiding this comment

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

Thanks, nice generalization.
Only change I'd require is the borrow because clippy will complain about it.

src/keys.rs Outdated Show resolved Hide resolved
src/keys.rs Outdated Show resolved Hide resolved
@webmaster128 webmaster128 reopened this Mar 6, 2023
@webmaster128
Copy link
Member

webmaster128 commented Mar 6, 2023

Not sure what kind of issues the CI has or had. Somehow the GitHub connection got lost. But in #31 it worked today, also for fork PRs.

When the changes requested by @chipshort are in and the CI is happy this is good to go IMO.

@maurolacy
Copy link
Contributor

@webmaster128 my understanding is that because serialization takes a reference to the type being serialized, it is more efficient to use references as keys as (1) the underlying serialization doesn't change and (2) you can avoid moves (and in turn clones) when calling methods on the map.

This is correct. Using references is a good option for serialising stuff, while deserialization is always to an owned type, so, using references doesn't impact it.

Just seen this. Will take a detailed look and comment back.

@0xekez
Copy link
Contributor Author

0xekez commented Mar 8, 2023

for some additional context, here is the map that i was able to make with these changes (we forked to include this change while the ATOM proposal was pending): cw-wormhole.

@chipshort - I've made your requested changes in the most recent commit. thank you!

Copy link
Contributor

@maurolacy maurolacy left a comment

Choose a reason for hiding this comment

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

LGTM.

Comment on lines +142 to +143
type Prefix = <T as PrimaryKey<'a>>::Prefix;
type SubPrefix = <T as PrimaryKey<'a>>::SubPrefix;
Copy link
Contributor

@maurolacy maurolacy Mar 9, 2023

Choose a reason for hiding this comment

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

As mentioned, the type cast is not needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
type Prefix = <T as PrimaryKey<'a>>::Prefix;
type SubPrefix = <T as PrimaryKey<'a>>::SubPrefix;
type Prefix = T::Prefix;
type SubPrefix = T::SubPrefix;

T: Prefixer<'a>,
{
fn prefix(&self) -> Vec<Key> {
<T as Prefixer<'a>>::prefix(self)
Copy link
Contributor

@maurolacy maurolacy Mar 9, 2023

Choose a reason for hiding this comment

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

Same here:

Suggested change
<T as Prefixer<'a>>::prefix(self)
T::prefix(self)

type SuperSuffix = T::SuperSuffix;

fn key(&self) -> Vec<Key> {
<T as PrimaryKey<'a>>::key(self)
Copy link
Contributor

@maurolacy maurolacy Mar 9, 2023

Choose a reason for hiding this comment

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

And here:

Suggested change
<T as PrimaryKey<'a>>::key(self)
T::key(self)

@maurolacy maurolacy merged commit 6db957c into CosmWasm:main Mar 14, 2023
@webmaster128 webmaster128 added this to the 1.1 milestone Jun 7, 2023
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.

4 participants