Skip to content

Commit

Permalink
Implement Default for enums in proto2
Browse files Browse the repository at this point in the history
Previously `Default` was implemented only for enums in proto3.

`Default` implemented unconditionally to simplify certain generic
operations, e. g. reading a map.  Also, note that even in proto2
some operations fallback to first enum value, e. g. `get_xxx` for
unset field, so this implementation is not completely unreasonable.
  • Loading branch information
stepancheg committed Jan 29, 2019
1 parent 4665607 commit ced9ac9
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions protobuf-codegen/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,8 @@ impl<'a> EnumGen<'a> {
self.write_impl_enum(w);
w.write_line("");
self.write_impl_copy(w);
if self.enum_with_scope.scope.file_scope.syntax() == Syntax::PROTO3 {
w.write_line("");
self.write_impl_default(w);
}
w.write_line("");
self.write_impl_default(w);
w.write_line("");
self.write_impl_value(w);
}
Expand Down Expand Up @@ -259,13 +257,23 @@ impl<'a> EnumGen<'a> {
}

fn write_impl_default(&self, w: &mut CodeWriter) {
assert!(self.enum_with_scope.scope.file_scope.syntax() == Syntax::PROTO3);
let first_value = &self.enum_with_scope.values()[0];
if first_value.get_number() != 0 {
// This warning is emitted only for proto2
// (because in proto3 first enum variant number is always 0).
// `Default` implemented unconditionally to simplify certain
// generic operations, e. g. reading a map.
// Also, note that even in proto2 some operations fallback to
// first enum value, e. g. `get_xxx` for unset field,
// so this implementation is not completely unreasonable.
w.comment("Note, `Default` is implemented although default value is not 0");
}
w.impl_for_block("::std::default::Default", &self.type_name, |w| {
w.def_fn("default() -> Self", |w| {
w.write_line(&format!(
"{}::{}",
&self.type_name,
&self.enum_with_scope.values()[0].rust_name()
&first_value.rust_name()
))
});
});
Expand Down

0 comments on commit ced9ac9

Please sign in to comment.