From 7e9d0c1630a51b2964ff1f359bc9a0e0bd4dfe78 Mon Sep 17 00:00:00 2001 From: Hong Shin Date: Mon, 17 Jul 2023 09:35:00 -0700 Subject: [PATCH] Add V0 support for fixed ints (32 and 64) PiperOrigin-RevId: 548716260 --- rust/test/shared/accessors_test.rs | 33 +++++++++++++++++++ .../compiler/rust/accessors/accessors.cc | 2 ++ src/google/protobuf/compiler/rust/naming.cc | 6 ++++ 3 files changed, 41 insertions(+) diff --git a/rust/test/shared/accessors_test.rs b/rust/test/shared/accessors_test.rs index 2a59458cd9..af18fbe4d0 100644 --- a/rust/test/shared/accessors_test.rs +++ b/rust/test/shared/accessors_test.rs @@ -34,6 +34,8 @@ use unittest_proto::proto2_unittest::TestAllTypes; #[test] fn test_default_accessors() { let msg = TestAllTypes::new(); + assert_eq!(msg.default_fixed32(), 47); + assert_eq!(msg.default_fixed64(), 48); assert_eq!(msg.default_int32(), 41); assert_eq!(msg.default_int64(), 42); assert_eq!(msg.default_sint32(), -45); @@ -45,6 +47,37 @@ fn test_default_accessors() { assert_eq!(msg.default_bool(), true); } +#[test] +fn test_optional_fixed32_accessors() { + let mut msg = TestAllTypes::new(); + assert_eq!(msg.optional_fixed32_opt(), None); + assert_eq!(msg.optional_fixed32(), 0); + + msg.optional_fixed32_set(Some(99)); + assert_eq!(msg.optional_fixed32_opt(), Some(99)); + assert_eq!(msg.optional_fixed32(), 99); + + msg.optional_fixed32_set(None); + assert_eq!(msg.optional_fixed32_opt(), None); + + assert_eq!(msg.optional_fixed32(), 0); +} + +#[test] +fn test_optional_fixed64_accessors() { + let mut msg = TestAllTypes::new(); + assert_eq!(msg.optional_fixed64_opt(), None); + assert_eq!(msg.optional_fixed64(), 0); + + msg.optional_fixed64_set(Some(2000)); + assert_eq!(msg.optional_fixed64_opt(), Some(2000)); + assert_eq!(msg.optional_fixed64(), 2000); + + msg.optional_fixed64_set(None); + assert_eq!(msg.optional_fixed64_opt(), None); + assert_eq!(msg.optional_fixed64(), 0); +} + #[test] fn test_optional_int32_accessors() { let mut msg = TestAllTypes::new(); diff --git a/src/google/protobuf/compiler/rust/accessors/accessors.cc b/src/google/protobuf/compiler/rust/accessors/accessors.cc index 6523cd0139..627afd2729 100644 --- a/src/google/protobuf/compiler/rust/accessors/accessors.cc +++ b/src/google/protobuf/compiler/rust/accessors/accessors.cc @@ -51,6 +51,8 @@ std::unique_ptr AccessorGenerator::For( switch (field.desc().type()) { case FieldDescriptor::TYPE_INT32: case FieldDescriptor::TYPE_INT64: + case FieldDescriptor::TYPE_FIXED32: + case FieldDescriptor::TYPE_FIXED64: case FieldDescriptor::TYPE_SINT32: case FieldDescriptor::TYPE_SINT64: case FieldDescriptor::TYPE_UINT32: diff --git a/src/google/protobuf/compiler/rust/naming.cc b/src/google/protobuf/compiler/rust/naming.cc index 7bb7d491ad..224fd3f0aa 100644 --- a/src/google/protobuf/compiler/rust/naming.cc +++ b/src/google/protobuf/compiler/rust/naming.cc @@ -110,6 +110,10 @@ absl::string_view PrimitiveRsTypeName(Context field) { switch (field.desc().type()) { case FieldDescriptor::TYPE_BOOL: return "bool"; + case FieldDescriptor::TYPE_FIXED32: + return "i32"; + case FieldDescriptor::TYPE_FIXED64: + return "i64"; case FieldDescriptor::TYPE_INT32: return "i32"; case FieldDescriptor::TYPE_INT64: @@ -141,6 +145,8 @@ bool IsSupportedFieldType(Context field) { // cord or string_piece) in V0 API. !field.desc().options().has_ctype() && (field.desc().type() == FieldDescriptor::TYPE_BOOL || + field.desc().type() == FieldDescriptor::TYPE_FIXED32 || + field.desc().type() == FieldDescriptor::TYPE_FIXED64 || field.desc().type() == FieldDescriptor::TYPE_INT32 || field.desc().type() == FieldDescriptor::TYPE_INT64 || field.desc().type() == FieldDescriptor::TYPE_SINT32 ||