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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.9.2] - 2021-07-09
### Added

- Add index getter to Variant [(#110)](https://github.com/paritytech/scale-info/pull/110)

## [0.9.1] - 2021-07-06
### Fixed

- Option constructor macro hygiene [(#108)](https://github.com/paritytech/scale-info/pull/108)

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "scale-info"
version = "0.9.1"
version = "0.9.2"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

Expand Down
5 changes: 5 additions & 0 deletions src/ty/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ where
&self.fields
}

/// 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.

👍

pub fn index(&self) -> Option<u8> {
self.index
}
Comment on lines +233 to +236
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


/// Returns the discriminant of the variant.
pub fn discriminant(&self) -> Option<u64> {
self.discriminant
Expand Down