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

A more lightweight, informative error value in TryFrom<i32> for enums #1010

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mzabaluev
Copy link
Contributor

DecodeError as the error type of the generated TryFrom<i32> conversions for enum values is problematic:

  • It's overweight for the call sites converting a field value to its representative domain-safe enum, as it's generally meant to convey information on the wire message. Construction of a DecodeError value makes an allocation even with a static message.
  • It erases information about the raw value, both programmatically (there's only a cause string) and as currently generated, from the displayed message (could use format! in the conversion impls, but that'll cost another allocation).

Add a new UnknownEnumValue error type to prost.
Change the TryFrom<i32> conversions generated for enums to use that as the associated Error type.
The error value carries the original integer as the public inner member.

As this is a breaking change, it's not meant for 0.12, but for a future version.

Add a new UnknownEnumValue error type to prost,
change the TryFrom<i32> conversions generated for enums to use that
as the associated Error type. The error value carries the original
integer, making it also more informative than DecodeError.
Copy link
Collaborator

@caspermeijn caspermeijn left a comment

Choose a reason for hiding this comment

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

I understand why you need this. I have two open question:

  • Should this be a separated struct or should the i32 be part of DecodeError somehow?
  • It seems DecodeError mostly filled with static strings. Do we want to change that to a non_exthaustive enum instead of a string?

@mzabaluev
Copy link
Contributor Author

I think the TryFrom impl generated for an enum is a separate case from the protobuf message decoding error.
It arises in a different context: the consumer tries to convert a field of an already decoded message to its nominal enum type.
DecodeError, for its part, has all the data to represent a failure arising when a message is decoded.

@caspermeijn
Copy link
Collaborator

I think the TryFrom impl generated for an enum is a separate case from the protobuf message decoding error. It arises in a different context: the consumer tries to convert a field of an already decoded message to its nominal enum type. DecodeError, for its part, has all the data to represent a failure arising when a message is decoded.

That makes sense. I do think that DecodeError should be changed to a non_exthaustive enum instead of a string, but that is an issue for another day.

Copy link
Collaborator

@caspermeijn caspermeijn left a comment

Choose a reason for hiding this comment

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

Approved, but needs to wait for the next major release.

Thanks for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants