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

Handle #[codec(index = …)] in regular enums #80

Merged
merged 87 commits into from
Jun 6, 2021

Conversation

dvdplm
Copy link
Contributor

@dvdplm dvdplm commented Mar 23, 2021

A Variant can have fields of 3 different kinds: Named, Unnamed or Unit. In this PR I introduce indexed variations of these, so we have VariantsBuilder::variant/VariantsBuilder::indexed_variant and VariantsBuilder::variant_unit/VariantsBuilder::indexed_variant_unit.

This PR stores the index in a new index member of Variant, which is probably a bit contentious given it increases the size of the registry.
We could also store the index in the discriminant field, which would be compatible with the direction the Rust language is heading with RFC 2363 (surfacing discriminants for all enum variants, c-style or not).

It is currently possible to set both the discriminant and the index on a c-style enum (with priority given to the index attribute):

#[derive(Encode)]
enum Foo {
	A,
	#[codec(index = 2)]
	B = 8,
	C
}

I think that the more cautious path is to have both index and discriminant, mimicing parity-scale-infos behaviour.

Builds on top of #44

Closes #79

ascjones and others added 30 commits December 7, 2020 11:49
By default, parity-scale-codec does not provide Encode/Decode impls for an owned String.
This is only provided under the "full" feature which is not used by the substrate runtime,
because it should not be used for consensus critical code. So in order for the CompactForm
to be integrated into the substrate runtime, or wherever the "full" feature cannot be used,
then we must parameterize the `String` type so that it can be both an `&'static str` on the
runtime side where it is encoded, and a `String` in client/consuming code where it is decoded.
Add a `compact` member to `Field` to indicate it is `scale_codec::Compact`
Handle `codec(skip)` and `codec(index = $int)` attributes
Ensure we're working with an outer attribute
match v.fields {
let v_name = quote! {::core::stringify!(#ident) };
let docs = utils::get_doc_literals(&v.attrs);
let index = utils::maybe_index(v).map(|i| quote!(.index(#i)));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's hard to overstate how much nicer this is compared to what I had.

Copy link
Contributor

Choose a reason for hiding this comment

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

That was one of my goals with the builder pattern changes, so thanks!, The main priorities being type safe construction and friendliness for the derive macro. The tradeoff being that it is not fantastic for constructing type infos by hand, but we are assuming that is not very common.

ascjones added a commit that referenced this pull request Jun 6, 2021
* Capture docs for types, fields, and enum variants

* Add docs to derive tests

* Fmt

* Fmt

* Make clippy happy

* Fix struct docs tests

* Fix struct docs tests

* Add missing Vec import

* Fix test

* Clear docs

* Promote build to it's own mod, split fields and variant builders

* Refactor variant construction with builder

* Fix up derive and unit tests with Variant builder

* Fmt

* Condense build module back to single file

* Fix up doc tests

* Fix up README

* Define initial field builder

* Fix up field building

* Fix tests

* Fully qualify calls to stringify

* Default impl for FieldBuilder

* Fix up default impl

* Fix spelling errors

* Remove clear_docs

* Fully qualify module_path

* Update derive/src/lib.rs

Co-authored-by: Robin Freyler <[email protected]>

* Use callback for variant builder

* Update README

* Remove leading space in doc comments

* Satisfy clippy

* Add test for doc capture

* Rename index back to discriminant, changes for this will be in #80

* Rename index back to discriminant, changes for this will be in #80

* Update src/build.rs

Co-authored-by: David <[email protected]>

* Update src/build.rs

Co-authored-by: David <[email protected]>

Co-authored-by: Robin Freyler <[email protected]>
Co-authored-by: David <[email protected]>
Copy link
Contributor

@ascjones ascjones left a comment

Choose a reason for hiding this comment

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

LGTM

match v.fields {
let v_name = quote! {::core::stringify!(#ident) };
let docs = utils::get_doc_literals(&v.attrs);
let index = utils::maybe_index(v).map(|i| quote!(.index(#i)));
Copy link
Contributor

Choose a reason for hiding this comment

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

That was one of my goals with the builder pattern changes, so thanks!, The main priorities being type safe construction and friendliness for the derive macro. The tradeoff being that it is not fantastic for constructing type infos by hand, but we are assuming that is not very common.

@ascjones ascjones merged commit ce199ab into master Jun 6, 2021
@ascjones ascjones deleted the dp-handle-scale-index-in-regular-enums branch June 6, 2021 15:57
@ascjones ascjones mentioned this pull request Jun 25, 2021
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.

Handle #[codec(index = $int)] on regular enums
2 participants