From 1ad628a18eda3714b83c69c05ab994dcbac6d8b8 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Tue, 16 Jan 2024 08:41:33 -0500 Subject: [PATCH] Default to packed repeated primitive fields in proto3. Fixes #704. In proto3, repeated primitive fields should be packed by default: https://protobuf.dev/programming-guides/encoding/#packed --- protobuf-codegen/src/gen/field/mod.rs | 31 ++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/protobuf-codegen/src/gen/field/mod.rs b/protobuf-codegen/src/gen/field/mod.rs index d6a5e44a4..79a16e05e 100644 --- a/protobuf-codegen/src/gen/field/mod.rs +++ b/protobuf-codegen/src/gen/field/mod.rs @@ -188,7 +188,36 @@ impl<'a> FieldGen<'a> { FieldKind::Repeated(RepeatedField { elem, - packed: field.field.proto().options.get_or_default().packed(), + packed: field + .field + .proto() + .options + .get_or_default() + .packed + .unwrap_or(match field.message.scope.file_scope.syntax() { + Syntax::Proto2 => false, + // in proto3, repeated primitive types are packed by default + Syntax::Proto3 => match field.field.proto().type_() { + Type::TYPE_DOUBLE + | Type::TYPE_FLOAT + | Type::TYPE_INT64 + | Type::TYPE_UINT64 + | Type::TYPE_INT32 + | Type::TYPE_FIXED64 + | Type::TYPE_FIXED32 + | Type::TYPE_BOOL + | Type::TYPE_UINT32 + | Type::TYPE_SFIXED32 + | Type::TYPE_SFIXED64 + | Type::TYPE_SINT32 + | Type::TYPE_SINT64 => true, + Type::TYPE_STRING + | Type::TYPE_GROUP + | Type::TYPE_MESSAGE + | Type::TYPE_BYTES + | Type::TYPE_ENUM => false, + }, + }), }) } RuntimeFieldType::Singular(..) => {