-
Notifications
You must be signed in to change notification settings - Fork 30
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
Conversation
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: |
There was a problem hiding this 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 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. |
There was a problem hiding this 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.
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. |
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. |
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! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
type Prefix = <T as PrimaryKey<'a>>::Prefix; | ||
type SubPrefix = <T as PrimaryKey<'a>>::SubPrefix; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here:
<T as Prefixer<'a>>::prefix(self) | |
T::prefix(self) |
type SuperSuffix = T::SuperSuffix; | ||
|
||
fn key(&self) -> Vec<Key> { | ||
<T as PrimaryKey<'a>>::key(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here:
<T as PrimaryKey<'a>>::key(self) | |
T::key(self) |
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 implementcw-future-map
.