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

Add index getter to Variant #110

Merged
merged 3 commits into from
Jul 9, 2021
Merged

Add index getter to Variant #110

merged 3 commits into from
Jul 9, 2021

Conversation

ascjones
Copy link
Contributor

@ascjones ascjones commented Jul 9, 2021

No description provided.

@ascjones ascjones changed the title Add index getter to Variant Add index getter to Variant Jul 9, 2021
@ascjones ascjones merged commit 749b02f into master Jul 9, 2021
@ascjones ascjones deleted the aj-variant-index branch July 9, 2021 08:47
Comment on lines +233 to +236
/// Returns the index of the variant, if specified.
pub fn index(&self) -> Option<u8> {
self.index
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the difference here from the discriminant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The discriminant is only valid for "C Like" enums.

If both are specified then the index takes precedence for encoding. We still retain the discriminant though for e.g. reconstructing the original type.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah okay I see. Although we should keep in mind that there is a Rust RFC to allow for discriminants for data variants. Although then #[scale(index = N)] would still override those which should be fine.

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 that would still hold true. You are correct that index specifically refers to the value from the attribute. And it can be different from the discriminator, but will take precedence for encoding.

Copy link
Contributor

Choose a reason for hiding this comment

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

keep in mind that there is a Rust RFC

If/when that lands and is stabilised we could deprecate the #[scale(index = N)] attribute and only use discriminants. That would be nice (and a breaking change both to us and parity-scale-codec).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discriminant removed in #112

@@ -230,7 +230,7 @@ where
&self.fields
}

/// Returns the index of the variant.
/// Returns the index of the variant, if specified.
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

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.

3 participants