Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Allow to name the generic for storages in #[pallet::storage] #8751

Merged
4 commits merged into from
May 19, 2021

Conversation

gui1117
Copy link
Contributor

@gui1117 gui1117 commented May 6, 2021

This PR make the macro handle to give named generics.

So you can declare like this:

#[pallet::storage]
pub(super) type MyStorage<T> = StorageMap<Hasher = Blake2_128Concat, Key = u32, Value = u32>;
#[pallet::storage]
pub(super) type MyStorage<T> = StorageDoubleMap<
    Hasher1 = Blake2_128Concat, 
    Key1 = u32, 
    Hasher2 = Blake2_128Concat, 
    Key2 = u32, 
    Value = u32,
    QueryKind = ValueQuery
>;

NOTE: no need to write Prefix = _,

NOTE: no need to declare them in order, and no need to declare all of them. For instance if user want to set OnEmpty generic, it can avoid declaring QueryKind, the default query kind will be expanded by the macro

context

This is motivated from feedback about readability.
This is also motivated in order to add yet another generic for storages: MaxValues which would tell the storage the expected number of max values #8729 #8735

@github-actions github-actions bot added the A0-please_review Pull request needs code review. label May 6, 2021
@gui1117 gui1117 added B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed. labels May 6, 2021
@shawntabrizi
Copy link
Member

shawntabrizi commented May 6, 2021

Why are we allowed to drop this here: NOTE: no need to write Prefix = _,

Obviously it is preferred to not have _ in the front, but why not in the original syntax too?

@gui1117
Copy link
Contributor Author

gui1117 commented May 7, 2021

Actually the exact specification should be discussed, this is my thoughts:

  • We do not mix both declaration (no partly named, partly unnamed): this is because in the long term I expect ppl to use named one, and mixing would be more confusing than helpful. But we could do something like python named argument which allows to specify unnamed and named ones.

  • For named arguments, only the mandatory ones must be specified, all the generic which have a default in the struct (e.g. OnEmpty=GetDefault) can be omit. Because a user could specify OnEmpty but not QueryKind, that means the macro have to expand QueryKind with the correct default type in order to be able to set the OnEmpty as given by the user.
    The main reason for this is so that people can later specify MaxValues without having to specify OnEmpty.
    Considering this I thought Prefix can be considered as "non mandatory" and the macro will always expand this generic correctly. I feel because we introduce custom handling of arguments it makes sense to drop Prefix = _ as people can expect the macro to handle stuff.

  • Also I allowed named argument to be specified in any order but this is probably personal sensitivity and I can see argument for fix order.

@cla-bot-2021
Copy link

cla-bot-2021 bot commented May 7, 2021

@thiolliere No commands could be detected from your comment.

@shawntabrizi
Copy link
Member

allowed named argument to be specified in any order

Would this be allowed for a regular rust type?

@bkchr
Copy link
Member

bkchr commented May 10, 2021

allowed named argument to be specified in any order

Would this be allowed for a regular rust type?

When you for example have some trait and want to set the associated types in a where bound, you can also give them in any order.

Requiring an order for stuff that is uniquely named is weird and just complicates the whole process again.

@gui1117
Copy link
Contributor Author

gui1117 commented May 13, 2021

the other idea which I don't prefer:
we can make argument generally more named by wrapping them in some types, also grouping them, something like:

type Foo<T> = StorageMap<_, Key<Twox128Concat, u32>, ValueNonOption<u32, OnEmpty3U32>, MaxValues<ConstU32<300>>>;

type Foo2<T> = StorageMap<_, Key<Twox128Concat, u32>, ValueNonOption<u32>, MaxValues<ConstU32<300>>>;

type Foo3<T> = StorageMap<_, Key<Twox128Concat, u32>, ValueOption<u32>, MaxValues<ConstU32<300>>>;

But I think named generic is more understandable than rust wrapper which add one layer of complexity in the types.

@shawntabrizi
Copy link
Member

yeah i agree, what exists now is better

@shawntabrizi shawntabrizi requested a review from bkchr May 13, 2021 15:02
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Some nitpicks, otherwise looks good

error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
--> $DIR/storage_ensure_span_are_ok_on_wrong_gen.rs:20:12
|
20 | #[pallet::storage]
Copy link
Member

Choose a reason for hiding this comment

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

Not such a good diagnostic. Can we not use the correct span here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes we can point to the type alias instead of the storage attribute.

note that this not due to this PR, it is because we use the span of the attribute when expanding the storage metadata and getter. I think we can do in a follow-up i'll do an issue and look at it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to improve a bit in this draft #8850

Copy link
Member

Choose a reason for hiding this comment

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

Perfect! :)

frame/support/procedural/src/pallet/parse/storage.rs Outdated Show resolved Hide resolved
@bkchr
Copy link
Member

bkchr commented May 19, 2021

bot merge

@ghost
Copy link

ghost commented May 19, 2021

Trying merge.

@ghost ghost merged commit f61d2fd into master May 19, 2021
@ghost ghost deleted the gui-named-storage-generic branch May 19, 2021 07:11
nazar-pc pushed a commit to autonomys/substrate that referenced this pull request Aug 8, 2021
…ytech#8751)

* implement named generic for storages

* fix error message on unexpected name for generic
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants