From f72c45ff7587dc8a7706126e18157e92a575b09d Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 14 Mar 2024 11:33:12 -0700 Subject: [PATCH] Change .submsg_mut() to return $Msg$Mut instead of FieldEntry<$Msg$>. PiperOrigin-RevId: 615849358 --- rust/test/shared/accessors_test.rs | 17 ++++++++--------- .../compiler/rust/accessors/singular_message.cc | 16 ++++++++++++++-- src/google/protobuf/compiler/rust/message.cc | 3 +++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/rust/test/shared/accessors_test.rs b/rust/test/shared/accessors_test.rs index 85c9222e42..ad9068103d 100644 --- a/rust/test/shared/accessors_test.rs +++ b/rust/test/shared/accessors_test.rs @@ -524,7 +524,7 @@ fn test_message_opt_set() { assert_that!(msg.optional_nested_message_opt().is_set(), eq(true)); msg.clear_optional_nested_message(); - assert_that!(msg.optional_nested_message_mut().is_set(), eq(false)); + assert_that!(msg.optional_nested_message_opt().is_set(), eq(false)); } #[test] @@ -532,28 +532,27 @@ fn test_setting_submsg() { let mut msg = TestAllTypes::new(); let submsg = TestAllTypes_::NestedMessage::new(); - let fieldentry = msg.optional_nested_message_mut(); - assert_that!(fieldentry.is_set(), eq(false)); + assert_that!(msg.optional_nested_message_opt().is_set(), eq(false)); - fieldentry.or_default().set(submsg); + msg.set_optional_nested_message(submsg); // confirm that invoking .set on a submsg indeed flips the set bit - assert_that!(msg.optional_nested_message_mut().is_set(), eq(true)); + assert_that!(msg.optional_nested_message_opt().is_set(), eq(true)); msg.clear_optional_nested_message(); - assert_that!(msg.optional_nested_message_mut().is_set(), eq(false)); + assert_that!(msg.optional_nested_message_opt().is_set(), eq(false)); } #[test] fn test_msg_or_default() { let mut msg = TestAllTypes::new(); - assert_that!(msg.optional_nested_message_mut().is_set(), eq(false)); + assert_that!(msg.optional_nested_message_opt().is_set(), eq(false)); let _ = msg.optional_nested_message_mut().or_default(); // confirm that that or_default makes the field Present - assert_that!(msg.optional_nested_message_mut().is_set(), eq(true)); + assert_that!(msg.optional_nested_message_opt().is_set(), eq(true)); msg.clear_optional_nested_message(); - assert_that!(msg.optional_nested_message_mut().is_set(), eq(false)); + assert_that!(msg.optional_nested_message_opt().is_set(), eq(false)); } #[test] diff --git a/src/google/protobuf/compiler/rust/accessors/singular_message.cc b/src/google/protobuf/compiler/rust/accessors/singular_message.cc index c8e44b99af..ba99e0b5f6 100644 --- a/src/google/protobuf/compiler/rust/accessors/singular_message.cc +++ b/src/google/protobuf/compiler/rust/accessors/singular_message.cc @@ -73,7 +73,18 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field, return; } ctx.Emit({}, R"rs( - pub fn $raw_field_name$_mut(&mut self) + pub fn $raw_field_name$_mut(&mut self) -> $msg_type$Mut<'_> { + self.$raw_field_name$_entry().or_default() + } + )rs"); + }}, + {"private_getter_entry", + [&] { + if (accessor_case == AccessorCase::VIEW) { + return; + } + ctx.Emit({}, R"rs( + fn $raw_field_name$_entry(&mut self) -> $pb$::FieldEntry<'_, $msg_type$> { static VTABLE: $pbr$::MessageVTable = $pbr$::MessageVTable::new($pbi$::Private, @@ -109,7 +120,7 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field, pub fn set_$raw_field_name$(&mut self, val: impl $pb$::SettableValue<$msg_type$>) { //~ TODO: Optimize this to not go through the //~ FieldEntry. - self.$raw_field_name$_mut().set(val); + self.$raw_field_name$_entry().set(val); } )rs"); }}, @@ -124,6 +135,7 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field, R"rs( $getter$ $getter_mut$ + $private_getter_entry$ $getter_opt$ $setter$ $clearer$ diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 0d52bbf98e..5fb82e8f86 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -986,6 +986,9 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { Self{ inner: $pbr$::MutatorMessageRef::new(_private, msg) } } + #[deprecated = "This .or_default() is a no-op, usages can be safely removed"] + pub fn or_default(self) -> Self { self } + fn raw_msg(&self) -> $pbi$::RawMessage { self.inner.msg() }