Skip to content

Add index getter to Variant#110

Merged
ascjones merged 3 commits intomasterfrom
aj-variant-index
Jul 9, 2021
Merged

Add index getter to Variant#110
ascjones merged 3 commits intomasterfrom
aj-variant-index

Conversation

@ascjones
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

}

/// Returns the index of the variant.
/// Returns the index of the variant, if specified.
Copy link
Copy Markdown
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