From d2b2ef34ba536f56f3c05b76bb2c421b5eaefe85 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Tue, 30 Jan 2024 12:27:49 -0800 Subject: [PATCH] Add SettableValue for owned Msg. Right now this just does as_view().set_on(), but can be optimized to avoid a copy in a followup. PiperOrigin-RevId: 602807430 --- rust/test/shared/accessors_test.rs | 14 ++++++++++---- src/google/protobuf/compiler/rust/message.cc | 18 +++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/rust/test/shared/accessors_test.rs b/rust/test/shared/accessors_test.rs index 1a85a13acc..c5e11e7449 100644 --- a/rust/test/shared/accessors_test.rs +++ b/rust/test/shared/accessors_test.rs @@ -9,7 +9,7 @@ use googletest::prelude::*; use matchers::{is_set, is_unset}; -use protobuf::Optional; +use protobuf::{MutProxy, Optional}; use unittest_proto::{TestAllTypes, TestAllTypes_}; #[test] @@ -668,16 +668,22 @@ fn test_nonempty_default_string_accessors() { #[test] fn test_singular_msg_field() { - use TestAllTypes_::{NestedMessageMut, NestedMessageView}; + use TestAllTypes_::*; let mut msg = TestAllTypes::new(); let msg_view: NestedMessageView = msg.optional_nested_message(); // testing reading an int inside a view assert_that!(msg_view.bb(), eq(0)); - let msg_mut: NestedMessageMut = msg.optional_nested_message_mut().or_default(); + let mut nested_msg_mut: NestedMessageMut = msg.optional_nested_message_mut().or_default(); // test reading an int inside a mut - assert_that!(msg_mut.bb(), eq(0)); + assert_that!(nested_msg_mut.bb(), eq(0)); + + // Test setting an owned NestedMessage onto another message. + let mut new_nested = NestedMessage::new(); + new_nested.bb_mut().set(7); + nested_msg_mut.set(new_nested); + assert_that!(nested_msg_mut.bb(), eq(7)); } #[test] diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 4ffb149b45..01015965bb 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -192,7 +192,7 @@ void MessageDrop(Context& ctx, const Descriptor& msg) { )rs"); } -void MessageSettableValue(Context& ctx, const Descriptor& msg) { +void MessageSettableValueForView(Context& ctx, const Descriptor& msg) { switch (ctx.opts().kernel) { case Kernel::kCpp: ctx.Emit({{"copy_from_thunk", ThunkName(ctx, msg, "copy_from")}}, R"rs( @@ -519,7 +519,8 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { AccessorCase::MUT); } }}, - {"settable_impl", [&] { MessageSettableValue(ctx, msg); }}, + {"settable_impl_for_view", + [&] { MessageSettableValueForView(ctx, msg); }}, {"repeated_impl", [&] { MessageProxiedInRepeated(ctx, msg); }}, {"unwrap_upb", [&] { @@ -655,7 +656,18 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { } } - $settable_impl$ + $settable_impl_for_view$ + + impl $pb$::SettableValue<$Msg$> for $Msg$ { + fn set_on<'dst>( + self, _private: $pbi$::Private, mutator: $pb$::Mut<'dst, $Msg$>) + where $Msg$: 'dst { + //~ TODO: b/320701507 - This current will copy the message and then + //~ drop it, this copy would be avoided on upb kernel. + self.as_view().set_on($pbi$::Private, mutator); + } + } + $repeated_impl$ #[derive(Debug)]