diff --git a/rust/map.rs b/rust/map.rs index bbb5bfc677..a0e0c9e449 100644 --- a/rust/map.rs +++ b/rust/map.rs @@ -6,7 +6,7 @@ // https://developers.google.com/open-source/licenses/bsd use crate::{ - IntoProxied, Mut, MutProxied, MutProxy, Proxied, View, ViewProxy, + IntoProxied, Mut, MutProxied, MutProxy, Proxied, Proxy, View, ViewProxy, __internal::Private, __runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter}, }; @@ -112,7 +112,7 @@ impl + ?Sized> MutProxied for Map = MapMut<'msg, K, V> where K: 'msg, V: 'msg; } -impl<'msg, K: Proxied + ?Sized, V: ProxiedInMapValue + ?Sized> ViewProxy<'msg> +impl<'msg, K: Proxied + ?Sized, V: ProxiedInMapValue + ?Sized> Proxy<'msg> for MapView<'msg, K, V> { type Proxied = Map; @@ -130,6 +130,11 @@ impl<'msg, K: Proxied + ?Sized, V: ProxiedInMapValue + ?Sized> ViewProxy<'msg } impl<'msg, K: Proxied + ?Sized, V: ProxiedInMapValue + ?Sized> ViewProxy<'msg> + for MapView<'msg, K, V> +{ +} + +impl<'msg, K: Proxied + ?Sized, V: ProxiedInMapValue + ?Sized> Proxy<'msg> for MapMut<'msg, K, V> { type Proxied = Map; diff --git a/rust/primitive.rs b/rust/primitive.rs index b5f4b38108..ab50352f14 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::Private; -use crate::{IntoProxied, Proxied, View, ViewProxy}; +use crate::{IntoProxied, Proxied, Proxy, View, ViewProxy}; macro_rules! impl_singular_primitives { ($($t:ty),*) => { @@ -14,7 +14,7 @@ macro_rules! impl_singular_primitives { type View<'msg> = $t; } - impl<'msg> ViewProxy<'msg> for $t { + impl<'msg> Proxy<'msg> for $t { type Proxied = $t; fn as_view(&self) -> View<'_, Self::Proxied> { @@ -26,6 +26,8 @@ macro_rules! impl_singular_primitives { } } + impl<'msg> ViewProxy<'msg> for $t {} + impl IntoProxied<$t> for $t { fn into_proxied(self, _private: Private) -> $t { self diff --git a/rust/proxied.rs b/rust/proxied.rs index b1903a4e43..e90dbe5805 100644 --- a/rust/proxied.rs +++ b/rust/proxied.rs @@ -56,7 +56,7 @@ pub trait Proxied: Sized { /// 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> + Copy + Send + type View<'msg>: ViewProxy<'msg, Proxied = Self> where Self: 'msg; } @@ -90,11 +90,12 @@ pub type View<'msg, T> = ::View<'msg>; #[allow(dead_code)] pub type Mut<'msg, T> = ::Mut<'msg>; -/// Declares conversion operations common to all views. +/// 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 ViewProxy<'msg>: 'msg + Sync + Unpin + Sized + Debug { +pub trait Proxy<'msg>: 'msg + Sync + Unpin + Sized + Debug { type Proxied: 'msg + Proxied + ?Sized; /// Converts a borrow into a `View` with the lifetime of that borrow. @@ -148,11 +149,14 @@ pub trait ViewProxy<'msg>: 'msg + Sync + Unpin + Sized + Debug { 'msg: 'shorter; } -/// Declares operations common to all mutators. +/// Declares conversion operations common to view proxies. +pub trait ViewProxy<'msg>: Proxy<'msg> + Copy + 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>: ViewProxy<'msg> +pub trait MutProxy<'msg>: Proxy<'msg> where Self::Proxied: MutProxied, { @@ -259,7 +263,7 @@ mod tests { } } - impl<'msg> ViewProxy<'msg> for MyProxiedView<'msg> { + impl<'msg> Proxy<'msg> for MyProxiedView<'msg> { type Proxied = MyProxied; fn as_view(&self) -> View<'msg, MyProxied> { @@ -274,12 +278,14 @@ mod tests { } } + impl<'msg> ViewProxy<'msg> for MyProxiedView<'msg> {} + #[derive(Debug)] struct MyProxiedMut<'msg> { my_proxied_ref: &'msg mut MyProxied, } - impl<'msg> ViewProxy<'msg> for MyProxiedMut<'msg> { + impl<'msg> Proxy<'msg> for MyProxiedMut<'msg> { type Proxied = MyProxied; fn as_view(&self) -> View<'_, MyProxied> { diff --git a/rust/repeated.rs b/rust/repeated.rs index 99c935b4f4..692a24971f 100644 --- a/rust/repeated.rs +++ b/rust/repeated.rs @@ -15,7 +15,7 @@ use std::iter::FusedIterator; use std::marker::PhantomData; use crate::{ - IntoProxied, Mut, MutProxied, MutProxy, Proxied, View, ViewProxy, + IntoProxied, Mut, MutProxied, MutProxy, Proxied, Proxy, View, ViewProxy, __internal::Private, __runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField}, }; @@ -397,7 +397,7 @@ where type Mut<'msg> = RepeatedMut<'msg, T> where Repeated: 'msg; } -impl<'msg, T> ViewProxy<'msg> for RepeatedView<'msg, T> +impl<'msg, T> Proxy<'msg> for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg, { @@ -417,7 +417,9 @@ where } } -impl<'msg, T> ViewProxy<'msg> for RepeatedMut<'msg, T> +impl<'msg, T> ViewProxy<'msg> for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg {} + +impl<'msg, T> Proxy<'msg> for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg, { diff --git a/rust/shared.rs b/rust/shared.rs index 4494ee7019..ec31d65a01 100644 --- a/rust/shared.rs +++ b/rust/shared.rs @@ -27,7 +27,9 @@ pub mod __public { pub use crate::map::{Map, MapIter, MapMut, MapView, ProxiedInMapValue}; pub use crate::optional::Optional; pub use crate::proto; - pub use crate::proxied::{IntoProxied, Mut, MutProxied, MutProxy, Proxied, View, ViewProxy}; + pub use crate::proxied::{ + IntoProxied, Mut, MutProxied, MutProxy, Proxied, Proxy, View, ViewProxy, + }; pub use crate::repeated::{ ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView, }; diff --git a/rust/string.rs b/rust/string.rs index a94f4efc00..6ebdd37af1 100644 --- a/rust/string.rs +++ b/rust/string.rs @@ -11,7 +11,7 @@ use crate::__internal::Private; use crate::__runtime::{InnerProtoString, PtrAndLen, RawMessage}; -use crate::{IntoProxied, Mut, MutProxied, MutProxy, Optional, Proxied, View, ViewProxy}; +use crate::{IntoProxied, Mut, MutProxied, MutProxy, Optional, Proxied, Proxy, View, ViewProxy}; use std::borrow::Cow; use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}; use std::convert::{AsMut, AsRef}; @@ -114,7 +114,7 @@ impl IntoProxied for Arc<[u8]> { } } -impl<'msg> ViewProxy<'msg> for &'msg [u8] { +impl<'msg> Proxy<'msg> for &'msg [u8] { type Proxied = ProtoBytes; fn as_view(&self) -> &[u8] { @@ -129,6 +129,8 @@ impl<'msg> ViewProxy<'msg> for &'msg [u8] { } } +impl<'msg> ViewProxy<'msg> for &'msg [u8] {} + /// The bytes were not valid UTF-8. #[derive(Debug, PartialEq)] pub struct Utf8Error(pub(crate) ()); @@ -479,7 +481,7 @@ impl Proxied for ProtoString { type View<'msg> = &'msg ProtoStr; } -impl<'msg> ViewProxy<'msg> for &'msg ProtoStr { +impl<'msg> Proxy<'msg> for &'msg ProtoStr { type Proxied = ProtoString; fn as_view(&self) -> &ProtoStr { @@ -494,6 +496,8 @@ impl<'msg> ViewProxy<'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/rust/test/shared/accessors_repeated_test.rs b/rust/test/shared/accessors_repeated_test.rs index 9f70074bf4..bfec487ce9 100644 --- a/rust/test/shared/accessors_repeated_test.rs +++ b/rust/test/shared/accessors_repeated_test.rs @@ -7,7 +7,7 @@ use googletest::prelude::*; use paste::paste; -use protobuf::ViewProxy; +use protobuf::Proxy; use unittest_rust_proto::{test_all_types, test_all_types::NestedMessage, TestAllTypes}; macro_rules! generate_repeated_numeric_test { diff --git a/rust/upb.rs b/rust/upb.rs index a9c5581214..2c0084da36 100644 --- a/rust/upb.rs +++ b/rust/upb.rs @@ -10,7 +10,7 @@ use crate::__internal::{Enum, Private}; use crate::{ IntoProxied, Map, MapIter, MapMut, MapView, Mut, ProtoBytes, ProtoStr, ProtoString, Proxied, - ProxiedInMapValue, ProxiedInRepeated, Repeated, RepeatedMut, RepeatedView, View, ViewProxy, + ProxiedInMapValue, ProxiedInRepeated, Repeated, RepeatedMut, RepeatedView, View, }; use core::fmt::Debug; use std::alloc::Layout; diff --git a/src/google/protobuf/compiler/rust/enum.cc b/src/google/protobuf/compiler/rust/enum.cc index 74613aa8f2..a2b1143146 100644 --- a/src/google/protobuf/compiler/rust/enum.cc +++ b/src/google/protobuf/compiler/rust/enum.cc @@ -405,7 +405,7 @@ void GenerateEnumDefinition(Context& ctx, const EnumDescriptor& desc) { type View<'a> = $name$; } - impl $pb$::ViewProxy<'_> for $name$ { + impl $pb$::Proxy<'_> for $name$ { type Proxied = $name$; fn as_view(&self) -> $name$ { @@ -417,6 +417,8 @@ void GenerateEnumDefinition(Context& ctx, const EnumDescriptor& desc) { } } + impl $pb$::ViewProxy<'_> for $name$ {} + unsafe impl $pb$::ProxiedInRepeated for $name$ { fn repeated_new(_private: $pbi$::Private) -> $pb$::Repeated { $pbr$::new_enum_repeated($pbi$::Private) diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 54cac79093..20217a1ae5 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -260,7 +260,7 @@ void IntoProxiedForMessage(Context& ctx, const Descriptor& msg) { impl<'msg> $pb$::IntoProxied<$Msg$> for $Msg$Mut<'msg> { fn into_proxied(self, _private: $pbi$::Private) -> $Msg$ { - $pb$::IntoProxied::into_proxied($pb$::ViewProxy::into_view(self), _private) + $pb$::IntoProxied::into_proxied($pb$::Proxy::into_view(self), _private) } } @@ -289,7 +289,7 @@ void IntoProxiedForMessage(Context& ctx, const Descriptor& msg) { impl<'msg> $pb$::IntoProxied<$Msg$> for $Msg$Mut<'msg> { fn into_proxied(self, _private: $pbi$::Private) -> $Msg$ { - $pb$::IntoProxied::into_proxied($pb$::ViewProxy::into_view(self), _private) + $pb$::IntoProxied::into_proxied($pb$::Proxy::into_view(self), _private) } } @@ -323,7 +323,7 @@ void MessageMergeFrom(Context& ctx, const Descriptor& msg) { {"merge_from_thunk", ThunkName(ctx, msg, "merge_from")}, }, R"rs( - pub fn merge_from<'src>(&mut self, src: impl $pb$::ViewProxy<'src, Proxied = $Msg$>) { + pub fn merge_from<'src>(&mut self, src: impl $pb$::Proxy<'src, Proxied = $Msg$>) { // SAFETY: self and src are both valid `$Msg$`s. unsafe { $merge_from_thunk$(self.raw_msg(), src.as_view().raw_msg()); @@ -337,7 +337,7 @@ void MessageMergeFrom(Context& ctx, const Descriptor& msg) { {"minitable", UpbMinitableName(msg)}, }, R"rs( - pub fn merge_from<'src>(&mut self, src: impl $pb$::ViewProxy<'src, Proxied = $Msg$>) { + pub fn merge_from<'src>(&mut self, src: impl $pb$::Proxy<'src, Proxied = $Msg$>) { // SAFETY: self and src are both valid `$Msg$`s. unsafe { assert!( @@ -1029,7 +1029,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { // - `$Msg$View` does not use thread-local data. unsafe impl Send for $Msg$View<'_> {} - impl<'msg> $pb$::ViewProxy<'msg> for $Msg$View<'msg> { + impl<'msg> $pb$::Proxy<'msg> for $Msg$View<'msg> { type Proxied = $Msg$; fn as_view(&self) -> $pb$::View<'msg, $Msg$> { @@ -1040,6 +1040,8 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { } } + impl<'msg> $pb$::ViewProxy<'msg> for $Msg$View<'msg> {} + $into_proxied_impl$ $repeated_impl$ @@ -1088,11 +1090,11 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { } pub fn serialize(&self) -> Result, $pb$::SerializeError> { - $pb$::ViewProxy::as_view(self).serialize() + $pb$::Proxy::as_view(self).serialize() } pub fn to_owned(&self) -> $Msg$ { - $pb$::ViewProxy::as_view(self).to_owned() + $pb$::Proxy::as_view(self).to_owned() } $msg_merge_from$ @@ -1117,7 +1119,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { fn into_mut<'shorter>(self) -> $pb$::Mut<'shorter, $Msg$> where 'msg : 'shorter { self } } - impl<'msg> $pb$::ViewProxy<'msg> for $Msg$Mut<'msg> { + impl<'msg> $pb$::Proxy<'msg> for $Msg$Mut<'msg> { type Proxied = $Msg$; fn as_view(&self) -> $pb$::View<'_, $Msg$> { $Msg$View { msg: self.raw_msg(), _phantom: $std$::marker::PhantomData } @@ -1166,7 +1168,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { $Msg$Mut::new($pbi$::Private, &mut self.inner) } - pub fn merge_from<'src>(&mut self, src: impl $pb$::ViewProxy<'src, Proxied = $Msg$>) { + pub fn merge_from<'src>(&mut self, src: impl $pb$::Proxy<'src, Proxied = $Msg$>) { self.as_mut().merge_from(src); }