From 42c215718ec2425e9784ebc59f150ae47585bad4 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Fri, 16 Feb 2024 00:19:48 -0800 Subject: [PATCH] Add serialize(&self) on MsgView and MsgMut (in addition to the preexisting owned Message) PiperOrigin-RevId: 607593581 --- rust/test/shared/serialization_test.rs | 9 ++++++++- src/google/protobuf/compiler/rust/message.cc | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/rust/test/shared/serialization_test.rs b/rust/test/shared/serialization_test.rs index 39cfdd6d71..cb3a189dd3 100644 --- a/rust/test/shared/serialization_test.rs +++ b/rust/test/shared/serialization_test.rs @@ -10,9 +10,16 @@ use unittest_proto::TestAllTypes; #[test] fn serialize_zero_length() { - let msg = TestAllTypes::new(); + let mut msg = TestAllTypes::new(); + let serialized = msg.serialize(); assert_that!(serialized.len(), eq(0)); + + let serialized = msg.as_view().serialize(); + assert_that!(serialized.len(), eq(0)); + + let serialized = msg.as_mut().serialize(); + assert_that!(serialized.len(), eq(0)); } #[test] diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index b0429d7d93..a44a4b94f1 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -904,6 +904,10 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { self.msg } + pub fn serialize(&self) -> $pbr$::SerializedData { + $Msg::serialize$ + } + $accessor_fns_for_views$ } @@ -1034,6 +1038,10 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { self.inner } + pub fn serialize(&self) -> $pbr$::SerializedData { + $pb$::ViewProxy::as_view(self).serialize() + } + $raw_arena_getter_for_msgmut$ $accessor_fns_for_muts$ @@ -1079,7 +1087,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { $raw_arena_getter_for_message$ pub fn serialize(&self) -> $pbr$::SerializedData { - $Msg::serialize$ + self.as_view().serialize() } pub fn deserialize(&mut self, data: &[u8]) -> Result<(), $pb$::ParseError> { $Msg::deserialize$