Re:Rust for JavaScript Developers – Pattern Matching and Enums
by onei on Sunday July 12, 2020 @01:55PM
(#23813239)
I don't think that's protobuf's fault, it's simply a side effect of the common pattern in C of using memset when you initialise structs. I don't think there's a really good way around it other than remembering to think like a C programmer every now and then.
I guess one option is to replace the endpoints that use the enum, but that seems heavy handed if it's wide spread.
The way protobuf handles enums, it recommends having an "UNKNOWN" as value 0, as when deserializing from the wire any unknown enum value gets turned into value 0.
So it's recommended to have something like
enum FailReason { UNKNOWN_ERROR = 0, ERROR_1 = 1, ERROR_2 = 2, etc }
But someone long ago made the enum I was dealing with look like this
enum { SUCCESS = 0, ERROR_1 = 1, ERROR_2 = 2, etc. }
But this means I can't easily add a new error case, as it'll get interpreted as a success unless I carefully roll out the change to the receiving servers first.
But if I wasn't dealing with a distributed system I would like to have exhaustive checking.