From 2238cdc091ed7e8a5079d480611fdf37d8bfb22c Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Mon, 13 May 2024 10:55:38 -0700 Subject: [PATCH] Add impl Clone for Rust proto owned messages. PiperOrigin-RevId: 633265290 --- rust/test/shared/accessors_test.rs | 12 ++++++++++++ src/google/protobuf/compiler/rust/message.cc | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/rust/test/shared/accessors_test.rs b/rust/test/shared/accessors_test.rs index e14bcafc4c..2c324515be 100644 --- a/rust/test/shared/accessors_test.rs +++ b/rust/test/shared/accessors_test.rs @@ -779,6 +779,18 @@ fn test_submsg_setter() { assert_that!(parent.optional_nested_message().bb(), eq(7)); } +#[test] +fn test_clone() { + let mut m = TestAllTypes::new(); + m.set_optional_int32(42); + let clone = m.clone(); + assert_that!(clone.optional_int32(), eq(42)); + m.clear_optional_int32(); + assert_that!(m.has_optional_int32(), eq(false)); + assert_that!(clone.has_optional_int32(), eq(true)); + assert_that!(clone.optional_int32(), eq(42)); +} + #[test] fn test_to_owned() { let mut m = TestAllTypes::new(); diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index f6e472886a..69d6f3ff06 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -1102,6 +1102,12 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { } } + impl $std$::clone::Clone for $Msg$ { + fn clone(&self) -> Self { + self.as_view().to_owned() + } + } + extern "C" { $Msg_externs$