From 94360aacfbb7f77edef73216c83a68af27ccaeb8 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Wed, 8 Oct 2025 10:11:47 -0700 Subject: [PATCH] Remove the Proxy, ViewProxy, and MutProxy traits These traits don't seem to be doing a whole lot, so it seems worthwhile to remove them and simplify things a bit. PiperOrigin-RevId: 816759211 --- rust/codegen_traits.rs | 4 +- rust/cord.rs | 9 +-- rust/map.rs | 11 +--- rust/optional.rs | 14 +++- rust/primitive.rs | 7 +- rust/proxied.rs | 67 ++++---------------- rust/repeated.rs | 11 +--- rust/shared.rs | 3 +- rust/string.rs | 11 +--- src/google/protobuf/compiler/rust/enum.cc | 3 - src/google/protobuf/compiler/rust/message.cc | 6 -- 11 files changed, 31 insertions(+), 115 deletions(-) diff --git a/rust/codegen_traits.rs b/rust/codegen_traits.rs index 6a09f77197c7e..68dba5927d711 100644 --- a/rust/codegen_traits.rs +++ b/rust/codegen_traits.rs @@ -7,8 +7,8 @@ //! Traits that are implemented by codegen types. +use crate::MutProxied; use crate::__internal::SealedInternal; -use crate::{MutProxied, MutProxy, ViewProxy}; use create::Parse; use interop::{MessageMutInterop, MessageViewInterop, OwnedMessageInterop}; use read::Serialize; @@ -35,7 +35,6 @@ pub trait Message: SealedInternal /// A trait that all generated message views implement. pub trait MessageView<'msg>: SealedInternal - + ViewProxy<'msg, Proxied = Self::Message> // Read traits: + Debug + Serialize + Default // Thread safety: @@ -51,7 +50,6 @@ pub trait MessageView<'msg>: SealedInternal /// A trait that all generated message muts implement. pub trait MessageMut<'msg>: SealedInternal - + MutProxy<'msg, MutProxied = Self::Message> // Read traits: + Debug + Serialize // Write traits: diff --git a/rust/cord.rs b/rust/cord.rs index 19fdb0ba5a16c..9d36634974f3c 100644 --- a/rust/cord.rs +++ b/rust/cord.rs @@ -6,10 +6,7 @@ // https://developers.google.com/open-source/licenses/bsd use crate::__internal::{Private, SealedInternal}; -use crate::{ - AsView, IntoProxied, IntoView, ProtoBytes, ProtoStr, ProtoString, Proxied, Proxy, View, - ViewProxy, -}; +use crate::{AsView, IntoProxied, IntoView, ProtoBytes, ProtoStr, ProtoString, Proxied, View}; use paste::paste; use std::cmp::PartialEq; use std::ops::Deref; @@ -41,10 +38,6 @@ macro_rules! impl_cord_types { } } - impl<'msg> Proxy<'msg> for [< $t Cow>]<'msg> {} - - impl<'msg> ViewProxy<'msg> for [< $t Cow>]<'msg> {} - impl<'msg> AsView for [< $t Cow>]<'msg> { type Proxied = [< $t Cord>]; diff --git a/rust/map.rs b/rust/map.rs index e157952031bfc..fd844e56413e2 100644 --- a/rust/map.rs +++ b/rust/map.rs @@ -6,8 +6,7 @@ // https://developers.google.com/open-source/licenses/bsd use crate::{ - AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, MutProxy, Proxied, - Proxy, View, ViewProxy, + AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, Proxied, View, __internal::runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter}, __internal::{Private, SealedInternal}, }; @@ -141,8 +140,6 @@ impl> AsMut for Map { impl<'msg, K: Proxied, V: ProxiedInMapValue> SealedInternal for MapView<'msg, K, V> {} -impl<'msg, K: Proxied, V: ProxiedInMapValue> Proxy<'msg> for MapView<'msg, K, V> {} - impl<'msg, K: Proxied, V: ProxiedInMapValue> AsView for MapView<'msg, K, V> { type Proxied = Map; @@ -160,12 +157,8 @@ impl<'msg, K: Proxied, V: ProxiedInMapValue> IntoView<'msg> for MapView<'msg, } } -impl<'msg, K: Proxied, V: ProxiedInMapValue> ViewProxy<'msg> for MapView<'msg, K, V> {} - impl<'msg, K: Proxied, V: ProxiedInMapValue> SealedInternal for MapMut<'msg, K, V> {} -impl<'msg, K: Proxied, V: ProxiedInMapValue> Proxy<'msg> for MapMut<'msg, K, V> {} - impl<'msg, K: Proxied, V: ProxiedInMapValue> AsView for MapMut<'msg, K, V> { type Proxied = Map; @@ -200,8 +193,6 @@ impl<'msg, K: Proxied, V: ProxiedInMapValue> IntoMut<'msg> for MapMut<'msg, K } } -impl<'msg, K: Proxied, V: ProxiedInMapValue> MutProxy<'msg> for MapMut<'msg, K, V> {} - impl Map where K: Proxied, diff --git a/rust/optional.rs b/rust/optional.rs index 221f4d9ed2f29..12029e2abdf15 100644 --- a/rust/optional.rs +++ b/rust/optional.rs @@ -10,7 +10,7 @@ #![allow(unused)] use crate::__internal::Private; -use crate::{Mut, MutProxied, MutProxy, Proxied, View, ViewProxy}; +use crate::{Mut, MutProxied, Proxied, View}; use std::convert::{AsMut, AsRef}; use std::fmt::{self, Debug}; use std::panic; @@ -51,12 +51,20 @@ impl Optional { /// Constructs an `Optional` with a `T` value and presence bit. pub fn new(val: T, is_set: bool) -> Self { - if is_set { Optional::Set(val) } else { Optional::Unset(val) } + if is_set { + Optional::Set(val) + } else { + Optional::Unset(val) + } } /// Converts into an `Option` of the set value, ignoring any unset value. pub fn into_option(self) -> Option { - if let Optional::Set(x) = self { Some(x) } else { None } + if let Optional::Set(x) = self { + Some(x) + } else { + None + } } /// Returns if the field is set. diff --git a/rust/primitive.rs b/rust/primitive.rs index bf5af332a9793..17803b2888aa9 100644 --- a/rust/primitive.rs +++ b/rust/primitive.rs @@ -5,7 +5,7 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd use crate::__internal::SealedInternal; -use crate::{AsView, IntoView, Proxied, Proxy, ViewProxy}; +use crate::{AsView, IntoView, Proxied}; macro_rules! impl_singular_primitives { ($($t:ty),*) => { @@ -16,9 +16,6 @@ macro_rules! impl_singular_primitives { type View<'msg> = $t; } - impl<'msg> Proxy<'msg> for $t { - } - impl AsView for $t { type Proxied = $t; @@ -35,8 +32,6 @@ macro_rules! impl_singular_primitives { } } - impl<'msg> ViewProxy<'msg> for $t {} - // ProxiedInRepeated is implemented in {cpp,upb}.rs )* } diff --git a/rust/proxied.rs b/rust/proxied.rs index 07c1753a85c6f..f5068393e7326 100644 --- a/rust/proxied.rs +++ b/rust/proxied.rs @@ -45,20 +45,17 @@ //! indirection between the user and the internal memory representation. use crate::__internal::{Private, SealedInternal}; -use std::fmt::Debug; /// A type that can be accessed through a reference-like proxy. /// /// An instance of a `Proxied` can be accessed immutably via `Proxied::View`. /// /// All Protobuf field types implement `Proxied`. -pub trait Proxied: SealedInternal + AsView + Sized { +pub trait Proxied: SealedInternal + AsView + Sized + 'static { /// The proxy type that provides shared access to a `T`, like a `&'msg T`. /// /// Most code should use the type alias [`View`]. - type View<'msg>: ViewProxy<'msg, Proxied = Self> - where - Self: 'msg; + type View<'msg>: AsView + IntoView<'msg>; } /// A type that can be be accessed through a reference-like proxy. @@ -67,14 +64,12 @@ pub trait Proxied: SealedInternal + AsView + Sized { /// and immutably via `MutProxied::View`. /// /// `MutProxied` is implemented by message, map and repeated field types. -pub trait MutProxied: SealedInternal + Proxied + AsMut { +pub trait MutProxied: SealedInternal + Proxied + AsMut + 'static { /// The proxy type that provides exclusive mutable access to a `T`, like a /// `&'msg mut T`. /// /// Most code should use the type alias [`Mut`]. - type Mut<'msg>: MutProxy<'msg, MutProxied = Self> - where - Self: 'msg; + type Mut<'msg>: AsMut + IntoMut<'msg> + IntoView<'msg>; } /// A proxy type that provides shared access to a `T`, like a `&'msg T`. @@ -91,10 +86,10 @@ pub type View<'msg, T> = ::View<'msg>; pub type Mut<'msg, T> = ::Mut<'msg>; /// Used to semantically do a cheap "to-reference" conversion. This is -/// implemented on both owned `Proxied` types as well as ViewProxy and MutProxy +/// implemented on both owned `Proxied` types as well as view and mut proxy /// types. /// -/// On ViewProxy this will behave as a reborrow into a shorter lifetime. +/// On a view proxy this will behave as a reborrow into a shorter lifetime. pub trait AsView: SealedInternal { type Proxied: Proxied; @@ -120,12 +115,12 @@ pub trait AsView: SealedInternal { fn as_view(&self) -> View<'_, Self::Proxied>; } -/// Used to turn another 'borrow' into a ViewProxy. +/// Used to turn another 'borrow' into a view proxy. /// -/// On a MutProxy this borrows to a View (semantically matching turning a `&mut +/// On a mut proxy this borrows to a View (semantically matching turning a `&mut /// T` into a `&T`). /// -/// On a ViewProxy this will behave as a reborrow into a shorter lifetime +/// On a view proxy this will behave as a reborrow into a shorter lifetime /// (semantically matching a `&'a T` into a `&'b T` where `'a: 'b`). pub trait IntoView<'msg>: SealedInternal + AsView { /// Converts into a `View` with a potentially shorter lifetime. @@ -159,9 +154,9 @@ pub trait IntoView<'msg>: SealedInternal + AsView { } /// Used to semantically do a cheap "to-mut-reference" conversion. This is -/// implemented on both owned `Proxied` types as well as MutProxy types. +/// implemented on both owned `Proxied` types as well as mut proxy types. /// -/// On MutProxy this will behave as a reborrow into a shorter lifetime. +/// On a mut proxy this will behave as a reborrow into a shorter lifetime. pub trait AsMut: SealedInternal + AsView { type MutProxied: MutProxied; @@ -169,9 +164,9 @@ pub trait AsMut: SealedInternal + AsView { fn as_mut(&mut self) -> Mut<'_, Self::MutProxied>; } -/// Used to turn another 'borrow' into a MutProxy. +/// Used to turn another 'borrow' into a mut proxy. /// -/// On a MutProxy this will behave as a reborrow into a shorter lifetime +/// On a mut proxy this will behave as a reborrow into a shorter lifetime /// (semantically matching a `&mut 'a T` into a `&mut 'b T` where `'a: 'b`). pub trait IntoMut<'msg>: SealedInternal + AsMut { /// Converts into a `Mut` with a potentially shorter lifetime. @@ -201,34 +196,6 @@ pub trait IntoMut<'msg>: SealedInternal + AsMut { 'msg: 'shorter; } -/// Declares conversion operations common to all proxies (both views and mut -/// proxies). -/// -/// This trait is intentionally made non-object-safe to prevent a potential -/// future incompatible change. -pub trait Proxy<'msg>: - SealedInternal + 'msg + IntoView<'msg> + Sync + Unpin + Sized + Debug -{ -} - -/// Declares conversion operations common to view proxies. -pub trait ViewProxy<'msg>: SealedInternal + Proxy<'msg> + Send {} - -/// Declares operations common to all mut proxies. -/// -/// This trait is intentionally made non-object-safe to prevent a potential -/// future incompatible change. -pub trait MutProxy<'msg>: SealedInternal + Proxy<'msg> + AsMut + IntoMut<'msg> { - /// Gets an immutable view of this field. This is shorthand for `as_view`. - /// - /// This provides a shorter lifetime than `into_view` but can also be called - /// multiple times - if the result of `get` is not living long enough - /// for your use, use that instead. - fn get(&self) -> View<'_, Self::Proxied> { - self.as_view() - } -} - /// A value to `Proxied`-value conversion that consumes the input value. /// /// All setter functions accept types that implement `IntoProxied`. The purpose @@ -306,10 +273,6 @@ mod tests { } } - impl<'msg> Proxy<'msg> for MyProxiedView<'msg> {} - - impl<'msg> ViewProxy<'msg> for MyProxiedView<'msg> {} - impl<'msg> AsView for MyProxiedView<'msg> { type Proxied = MyProxied; @@ -334,8 +297,6 @@ mod tests { impl<'msg> SealedInternal for MyProxiedMut<'msg> {} - impl<'msg> Proxy<'msg> for MyProxiedMut<'msg> {} - impl<'msg> AsView for MyProxiedMut<'msg> { type Proxied = MyProxied; @@ -370,8 +331,6 @@ mod tests { } } - impl<'msg> MutProxy<'msg> for MyProxiedMut<'msg> {} - #[gtest] fn test_as_view() { let my_proxied = MyProxied { val: "Hello World".to_string() }; diff --git a/rust/repeated.rs b/rust/repeated.rs index 326d1bbaded16..ea27262334ade 100644 --- a/rust/repeated.rs +++ b/rust/repeated.rs @@ -15,8 +15,7 @@ use std::iter::FusedIterator; use std::marker::PhantomData; use crate::{ - AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, MutProxy, Proxied, - Proxy, View, ViewProxy, + AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, Proxied, View, __internal::runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField}, __internal::{Private, SealedInternal}, }; @@ -460,8 +459,6 @@ where impl<'msg, T> SealedInternal for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg {} -impl<'msg, T> Proxy<'msg> for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg {} - impl<'msg, T> AsView for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg, @@ -487,12 +484,8 @@ where } } -impl<'msg, T> ViewProxy<'msg> for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg {} - impl<'msg, T> SealedInternal for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg {} -impl<'msg, T> Proxy<'msg> for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg {} - impl<'msg, T> AsView for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg, @@ -543,8 +536,6 @@ where } } -impl<'msg, T> MutProxy<'msg> for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg {} - impl<'msg, T> iter::Iterator for RepeatedIter<'msg, T> where T: ProxiedInRepeated + 'msg, diff --git a/rust/shared.rs b/rust/shared.rs index 61476dbb3f28c..ae79f0c831916 100644 --- a/rust/shared.rs +++ b/rust/shared.rs @@ -25,8 +25,7 @@ pub use crate::cord::{ProtoBytesCow, ProtoStringCow}; pub use crate::map::{Map, MapIter, MapMut, MapView, ProxiedInMapValue}; pub use crate::optional::Optional; pub use crate::proxied::{ - AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Proxied, Proxy, View, - ViewProxy, + AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProxied, Proxied, View, }; pub use crate::r#enum::{Enum, UnknownEnumValue}; pub use crate::repeated::{ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView}; diff --git a/rust/string.rs b/rust/string.rs index b2ea5ff545d5c..eb01271201273 100644 --- a/rust/string.rs +++ b/rust/string.rs @@ -12,8 +12,7 @@ use crate::__internal::runtime::InnerProtoString; use crate::__internal::{Private, SealedInternal}; use crate::{ - utf8::Utf8Chunks, AsView, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Optional, Proxied, - Proxy, View, ViewProxy, + utf8::Utf8Chunks, AsView, IntoProxied, IntoView, Mut, MutProxied, Optional, Proxied, View, }; use std::borrow::Cow; use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}; @@ -131,8 +130,6 @@ impl IntoProxied for Arc<[u8]> { impl SealedInternal for &[u8] {} -impl<'msg> Proxy<'msg> for &'msg [u8] {} - impl AsView for &[u8] { type Proxied = ProtoBytes; @@ -150,8 +147,6 @@ impl<'msg> IntoView<'msg> for &'msg [u8] { } } -impl<'msg> ViewProxy<'msg> for &'msg [u8] {} - /// The bytes were not valid UTF-8. #[derive(Debug, PartialEq)] pub struct Utf8Error { @@ -534,8 +529,6 @@ impl AsView for ProtoString { } } -impl<'msg> Proxy<'msg> for &'msg ProtoStr {} - impl AsView for &ProtoStr { type Proxied = ProtoString; @@ -553,8 +546,6 @@ impl<'msg> IntoView<'msg> for &'msg ProtoStr { } } -impl<'msg> ViewProxy<'msg> for &'msg ProtoStr {} - /// Implements `PartialCmp` and `PartialEq` for the `lhs` against the `rhs` /// using `AsRef<[u8]>`. // TODO: consider improving to not require a `<()>` if no generics are diff --git a/src/google/protobuf/compiler/rust/enum.cc b/src/google/protobuf/compiler/rust/enum.cc index dcb2f50c6c009..b7a6ec4acd45a 100644 --- a/src/google/protobuf/compiler/rust/enum.cc +++ b/src/google/protobuf/compiler/rust/enum.cc @@ -277,9 +277,6 @@ void GenerateEnumDefinition(Context& ctx, const EnumDescriptor& desc, type View<'a> = $name$; } - impl $pb$::Proxy<'_> for $name$ {} - impl $pb$::ViewProxy<'_> for $name$ {} - impl $pb$::AsView for $name$ { type Proxied = $name$; diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 7d0ad29ec0d8a..3a4e6bb01fdda 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -608,9 +608,6 @@ void GenerateRs(Context& ctx, const Descriptor& msg, const upb::DefPool& pool) { // - `$Msg$View` does not use thread-local data. unsafe impl Send for $Msg$View<'_> {} - impl<'msg> $pb$::Proxy<'msg> for $Msg$View<'msg> {} - impl<'msg> $pb$::ViewProxy<'msg> for $Msg$View<'msg> {} - impl<'msg> $pb$::AsView for $Msg$View<'msg> { type Proxied = $Msg$; fn as_view(&self) -> $pb$::View<'msg, $Msg$> { @@ -683,9 +680,6 @@ void GenerateRs(Context& ctx, const Descriptor& msg, const upb::DefPool& pool) { // - `$Msg$Mut` does not perform any shared mutation. unsafe impl Sync for $Msg$Mut<'_> {} - impl<'msg> $pb$::Proxy<'msg> for $Msg$Mut<'msg> {} - impl<'msg> $pb$::MutProxy<'msg> for $Msg$Mut<'msg> {} - impl<'msg> $pb$::AsView for $Msg$Mut<'msg> { type Proxied = $Msg$; fn as_view(&self) -> $pb$::View<'_, $Msg$> {