From e618b282d44dbf487aeba5da839dd7a0127e9891 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 24 Jul 2024 08:58:57 -0700 Subject: [PATCH] Add AsView and AsMut implementations to owned messages. Add subtraits to ensure: - All Messages impl AsView + AsMut - All ViewProxies impl AsView - All MutProxies impl AsView + AsMut PiperOrigin-RevId: 655585740 --- rust/codegen_traits.rs | 15 ++++++++------- src/google/protobuf/compiler/rust/message.cc | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/rust/codegen_traits.rs b/rust/codegen_traits.rs index c48c5c02a1..d1a07dea25 100644 --- a/rust/codegen_traits.rs +++ b/rust/codegen_traits.rs @@ -7,7 +7,7 @@ //! Traits that are implemeted by codegen types. -use crate::{MutProxied, MutProxy, ViewProxy}; +use crate::{AsMut, AsView, MutProxied, MutProxy, ViewProxy}; use create::Parse; use read::Serialize; use std::fmt::Debug; @@ -18,10 +18,10 @@ pub trait Message: MutProxied // Create traits: + create::Parse + Default // Read traits: - + Debug + Serialize + + Debug + Serialize + AsView // Write traits: // TODO: Msg should impl Clear. - + ClearAndParse + + ClearAndParse + AsMut // Thread safety: + Send + Sync // Copy/Clone: @@ -31,11 +31,11 @@ pub trait Message: MutProxied /// A trait that all generated message views implement. pub trait MessageView<'msg>: ViewProxy<'msg, Proxied = Self::Message> // Read traits: - + Debug + Serialize + + Debug + Serialize + AsView // Thread safety: + Send + Sync - // Copy/Clone: - + Copy + Clone + // Copy/Clone: + + Copy + Clone { #[doc(hidden)] type Message: Message; @@ -45,8 +45,9 @@ pub trait MessageView<'msg>: ViewProxy<'msg, Proxied = Self::Message> pub trait MessageMut<'msg>: MutProxy<'msg, MutProxied = Self::Message> // Read traits: - + Debug + Serialize + + Debug + Serialize + AsView // Write traits: + + AsMut // TODO: MsgMut should impl Clear and ClearAndParse. // Thread safety: + Sync diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index b0a16df41c..94f67a7fc2 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -1246,6 +1246,20 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { } } + impl $pb$::AsView for $Msg$ { + type Proxied = Self; + fn as_view(&self) -> $Msg$View { + self.as_view() + } + } + + impl $pb$::AsMut for $Msg$ { + type MutProxied = Self; + fn as_mut(&mut self) -> $Msg$Mut { + self.as_mut() + } + } + extern "C" { $Msg_externs$