From 7b42f1c08b534f9e43629284e22acce6b13e7136 Mon Sep 17 00:00:00 2001 From: Kevin King Date: Fri, 12 Jan 2024 15:16:21 -0800 Subject: [PATCH] Add `Msg::as_{view,mut}()` PiperOrigin-RevId: 597969809 --- rust/test/shared/accessors_test.rs | 12 ++++++------ .../compiler/rust/accessors/singular_message.cc | 4 ++-- src/google/protobuf/compiler/rust/message.cc | 14 +++++++++++++- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/rust/test/shared/accessors_test.rs b/rust/test/shared/accessors_test.rs index f7ee0c0fb1..1896728ca3 100644 --- a/rust/test/shared/accessors_test.rs +++ b/rust/test/shared/accessors_test.rs @@ -10,7 +10,7 @@ use googletest::prelude::*; use matchers::{is_set, is_unset}; use protobuf::Optional; -use unittest_proto::proto2_unittest::{NestedTestAllTypes, TestAllTypes, TestAllTypes_}; +use unittest_proto::proto2_unittest::{TestAllTypes, TestAllTypes_}; #[test] fn test_default_accessors() { @@ -948,11 +948,11 @@ fn test_oneof_default_mut_accessors() { #[test] fn test_set_message_from_view() { use protobuf::MutProxy; - let mut m1 = NestedTestAllTypes::new(); - let mut m2 = NestedTestAllTypes::new(); - m2.payload_mut().optional_int32_mut().set(1); + let mut m1 = TestAllTypes::new(); + m1.optional_int32_mut().set(1); + let mut m2 = TestAllTypes::new(); + m2.as_mut().set(m1.as_view()); - m1.payload_mut().set(m2.payload()); - assert_that!(m1.payload().optional_int32(), eq(1)); + assert_that!(m2.optional_int32(), eq(1i32)); } diff --git a/src/google/protobuf/compiler/rust/accessors/singular_message.cc b/src/google/protobuf/compiler/rust/accessors/singular_message.cc index 36774db24f..2aff965616 100644 --- a/src/google/protobuf/compiler/rust/accessors/singular_message.cc +++ b/src/google/protobuf/compiler/rust/accessors/singular_message.cc @@ -63,12 +63,12 @@ void SingularMessage::InMsgImpl(Context& ctx, let submsg = unsafe { $getter_mut_thunk$(self.inner.msg, self.inner.arena.raw()) }; - $msg_type$Mut::new($pbi$::Private, &mut self.inner, submsg) + $msg_type$Mut::from_parent($pbi$::Private, &mut self.inner, submsg) )rs"); } else { ctx.Emit({}, R"rs( let submsg = unsafe { $getter_mut_thunk$(self.inner.msg) }; - $msg_type$Mut::new($pbi$::Private, &mut self.inner, submsg) + $msg_type$Mut::from_parent($pbi$::Private, &mut self.inner, submsg) )rs"); } }}, diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index a334f60ca6..ba3db3d5c6 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -497,7 +497,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { impl<'a> $Msg$Mut<'a> { #[doc(hidden)] - pub fn new(_private: $pbi$::Private, + pub fn from_parent(_private: $pbi$::Private, parent: &'a mut $pbr$::MessageInner, msg: $pbi$::RawMessage) -> Self { @@ -506,6 +506,10 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { $pbi$::Private, parent, msg) } } + #[doc(hidden)] + pub fn new(_private: $pbi$::Private, msg: &'a mut $pbr$::MessageInner) -> Self { + Self{ inner: $pbr$::MutatorMessageRef::new(_private, msg) } + } $accessor_fns_for_muts$ } @@ -544,6 +548,14 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { $Msg::deserialize$ } + pub fn as_view(&self) -> $Msg$View { + $Msg$View::new($pbi$::Private, self.inner.msg) + } + + pub fn as_mut(&mut self) -> $Msg$Mut { + $Msg$Mut::new($pbi$::Private, &mut self.inner) + } + $accessor_fns$ $oneof_accessor_fns$