Restructure the ViewProxy versus MutProxy trait setup.

Rather than two traits (MutProxy subtrait of ViewProxy), instead have three traits (MutProxy subtrait of Proxy, and ViewProxy subtrait of Proxy).

This makes things more consistent, including that (MutProxied subtraits Proxied) is now more parallel to (MutProxy subtraits Proxy).

ViewProxy is largely a marker trait here but leaves a spot for methods that should be on ViewProxies but not MutProxies if those do show up later.

PiperOrigin-RevId: 653661953
pull/17522/head
Protobuf Team Bot 7 months ago committed by Copybara-Service
parent 01e8cc0b30
commit cf948e4a81
  1. 9
      rust/map.rs
  2. 6
      rust/primitive.rs
  3. 20
      rust/proxied.rs
  4. 8
      rust/repeated.rs
  5. 4
      rust/shared.rs
  6. 10
      rust/string.rs
  7. 2
      rust/test/shared/accessors_repeated_test.rs
  8. 2
      rust/upb.rs
  9. 4
      src/google/protobuf/compiler/rust/enum.cc
  10. 20
      src/google/protobuf/compiler/rust/message.cc

@ -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<K: Proxied + ?Sized, V: ProxiedInMapValue<K> + ?Sized> MutProxied for Map<K
type Mut<'msg> = MapMut<'msg, K, V> where K: 'msg, V: 'msg;
}
impl<'msg, K: Proxied + ?Sized, V: ProxiedInMapValue<K> + ?Sized> ViewProxy<'msg>
impl<'msg, K: Proxied + ?Sized, V: ProxiedInMapValue<K> + ?Sized> Proxy<'msg>
for MapView<'msg, K, V>
{
type Proxied = Map<K, V>;
@ -130,6 +130,11 @@ impl<'msg, K: Proxied + ?Sized, V: ProxiedInMapValue<K> + ?Sized> ViewProxy<'msg
}
impl<'msg, K: Proxied + ?Sized, V: ProxiedInMapValue<K> + ?Sized> ViewProxy<'msg>
for MapView<'msg, K, V>
{
}
impl<'msg, K: Proxied + ?Sized, V: ProxiedInMapValue<K> + ?Sized> Proxy<'msg>
for MapMut<'msg, K, V>
{
type Proxied = Map<K, V>;

@ -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

@ -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> = <T as Proxied>::View<'msg>;
#[allow(dead_code)]
pub type Mut<'msg, T> = <T as MutProxied>::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> {

@ -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<T>: '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,
{

@ -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,
};

@ -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<ProtoBytes> 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

@ -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 {

@ -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;

@ -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<Self> {
$pbr$::new_enum_repeated($pbi$::Private)

@ -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<Vec<u8>, $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);
}

Loading…
Cancel
Save