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

feat!: ser/de enum discriminant #138

Merged
merged 2 commits into from
Apr 17, 2023

Conversation

encody
Copy link
Contributor

@encody encody commented Apr 14, 2023

This is a breaking change, since it will now serialize some enums (those that have manually specified discriminants) differently.

@encody encody requested a review from frol as a code owner April 14, 2023 04:12
@encody encody linked an issue Apr 14, 2023 that may be closed by this pull request
@frol frol changed the title feat: ser/de enum discriminant feat!: ser/de enum discriminant Apr 17, 2023
Copy link
Collaborator

@frol frol left a comment

Choose a reason for hiding this comment

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

@encody Thanks for the PR! Is it blocking you now, should I cut the release ASAP?

@frol frol merged commit 7472db7 into master Apr 17, 2023
@encody
Copy link
Contributor Author

encody commented Apr 18, 2023

@frol It is not blocking, just a matter of convenience.

@frol frol mentioned this pull request May 31, 2023
@frol frol deleted the 137-enum-discriminant-is-not-serialized-when-specified branch June 2, 2023 20:03
@mina86
Copy link
Contributor

mina86 commented Jun 5, 2023

Unless I’m misunderstanding something, this sounds like a terrible idea since users have no way to opt out of this change. Anyone who used borsh for enums which included variants with specified value is now unable to upgrade. This should have been done as a derive annotation, such as #[borsh(use_discriminants)].

@frol
Copy link
Collaborator

frol commented Jun 6, 2023

@mina86 Having a mismatch between the enum discriminant value and borsh serialized value could have silently led people to wrong assumptions. It is indeed a limitation that you cannot opt out, but why would you really? What is the use-case when you want the enum discriminant value not to match the borsh-discriminant?

@frol
Copy link
Collaborator

frol commented Jun 6, 2023

Anyone who used borsh for enums which included variants with specified value is now unable to upgrade.

The way to resolve it is to remove the explicit values assigned to the enum variants on the borsh-serialized enum and create a separate enum with the explicitly specified enum values. Overall, I agree that it is unfortunate that explicit values were not taken into account from the beginning, and now we face this breaking change.

@mina86
Copy link
Contributor

mina86 commented Jun 6, 2023

What is the use-case when you want the enum discriminant value not to match the borsh-discriminant?

The use case is an existing code base with an enum where this is the case. It doesn’t matter if it makes sense. It’s also possible that authors of such code base would have preferred behaviour from this PR, but that wasn’t the behaviour so now to maintain compatibility they have to stick to the old behaviour.

The way to resolve it is to remove the explicit values assigned to the enum variants on the borsh-serialized enum and create a separate enum with the explicitly specified enum values.

Part of the problem is that this is now a silent failure. The code will compile just fine and only break in production. If upgrading to newest borsh broke compilation in places where doing so would break compatibility this would be a different matter.

@frol
Copy link
Collaborator

frol commented Jun 6, 2023

@mina86 I am happy to hear your suggestions and review PRs. Unfortunately, there are no active contributors to borsh :-(

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.

Enum discriminant is not serialized when specified
3 participants