-
Notifications
You must be signed in to change notification settings - Fork 583
A more lightweight, informative error value in TryFrom<i32> for enums
#1010
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
Conversation
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.
caspermeijn
left a comment
There was a problem hiding this 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
i32be 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?
|
I think the |
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. |
caspermeijn
left a comment
There was a problem hiding this 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.
|
@mzabaluev The main |
It's easy to do, but I will wait on #1068 to make sure tests pass. |
That PR is merged now |
Done against 683091a |
caspermeijn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution
DecodeErroras the error type of the generatedTryFrom<i32>conversions for enum values is problematic:DecodeErrorvalue makes an allocation even with a static message.format!in the conversion impls, but that'll cost another allocation).Add a new
UnknownEnumValueerror type toprost.Change the
TryFrom<i32>conversions generated for enums to use that as the associatedErrortype.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.