From ea241fe5aa1307def9bb59462d39c8ba728611fd Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Tue, 16 Jul 2024 11:25:04 -0700 Subject: [PATCH] Add Message, MessageMut, MessageView traits. PiperOrigin-RevId: 652916543 --- rust/BUILD | 1 + rust/codegen_traits.rs | 17 +++++++++++++++++ rust/shared.rs | 2 ++ src/google/protobuf/compiler/rust/message.cc | 6 ++++++ 4 files changed, 26 insertions(+) create mode 100644 rust/codegen_traits.rs diff --git a/rust/BUILD b/rust/BUILD index 89880004ac..2b273172d5 100644 --- a/rust/BUILD +++ b/rust/BUILD @@ -47,6 +47,7 @@ rust_library( # # shared.rs is the root of the crate and has public items re-exported in protobuf.rs for user use. PROTOBUF_SHARED = [ + "codegen_traits.rs", "enum.rs", "internal.rs", "primitive.rs", diff --git a/rust/codegen_traits.rs b/rust/codegen_traits.rs new file mode 100644 index 0000000000..89c2db871d --- /dev/null +++ b/rust/codegen_traits.rs @@ -0,0 +1,17 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2024 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +//! Traits that are implemeted by codegen types. + +use crate::Proxied; +use std::fmt::Debug; + +pub trait Message: Default + Debug + Proxied + Send + Sync {} + +pub trait MessageView: Debug + Send + Sync {} + +pub trait MessageMut: Debug + Sync {} diff --git a/rust/shared.rs b/rust/shared.rs index eb8fd21479..403fcb0ea5 100644 --- a/rust/shared.rs +++ b/rust/shared.rs @@ -22,6 +22,7 @@ use std::fmt; /// These are the items protobuf users can access directly. #[doc(hidden)] pub mod __public { + pub use crate::codegen_traits::{Message, MessageMut, MessageView}; pub use crate::r#enum::{Enum, UnknownEnumValue}; pub use crate::map::{Map, MapIter, MapMut, MapView, ProxiedInMapValue}; pub use crate::optional::Optional; @@ -51,6 +52,7 @@ pub mod __runtime; #[path = "upb.rs"] pub mod __runtime; +mod codegen_traits; #[path = "enum.rs"] mod r#enum; mod map; diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 44f6ca8be3..54cac79093 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -951,6 +951,8 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { inner: $pbr$::MessageInner } + impl $pb$::Message for $Msg$ {} + impl std::fmt::Debug for $Msg$ { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { $Msg::debug$ @@ -988,6 +990,8 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { _phantom: $Phantom$<&'msg ()>, } + impl $pb$::MessageView for $Msg$View<'_> {} + impl std::fmt::Debug for $Msg$View<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { $Msg::debug$ @@ -1047,6 +1051,8 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { inner: $pbr$::MutatorMessageRef<'msg>, } + impl $pb$::MessageMut for $Msg$Mut<'_> {} + impl std::fmt::Debug for $Msg$Mut<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { $Msg::debug$