Split Proxied into Proxied and MutProxied traits.

Proxied is not marked as Sized yet, because ProtoStr is still dynamically sized. We will wait for clarity for the string types before marking Proxied Sized.

PiperOrigin-RevId: 627707544
pull/16615/head
Jakob Buchgraber 7 months ago committed by Copybara-Service
parent 2c16decbce
commit 1a7ce61061
  1. 5
      rust/map.rs
  2. 7
      rust/optional.rs
  3. 7
      rust/primitive.rs
  4. 8
      rust/proto_macro.rs
  5. 33
      rust/proxied.rs
  6. 8
      rust/repeated.rs
  7. 2
      rust/shared.rs
  8. 10
      rust/string.rs
  9. 6
      rust/vtable.rs
  10. 3
      src/google/protobuf/compiler/rust/enum.cc
  11. 3
      src/google/protobuf/compiler/rust/message.cc

@ -6,7 +6,7 @@
// https://developers.google.com/open-source/licenses/bsd // https://developers.google.com/open-source/licenses/bsd
use crate::{ use crate::{
Mut, MutProxy, Proxied, SettableValue, View, ViewProxy, Mut, MutProxied, MutProxy, Proxied, SettableValue, View, ViewProxy,
__internal::Private, __internal::Private,
__runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter}, __runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter},
}; };
@ -95,6 +95,9 @@ where
impl<K: Proxied + ?Sized, V: ProxiedInMapValue<K> + ?Sized> Proxied for Map<K, V> { impl<K: Proxied + ?Sized, V: ProxiedInMapValue<K> + ?Sized> Proxied for Map<K, V> {
type View<'msg> = MapView<'msg, K, V> where K: 'msg, V: 'msg; type View<'msg> = MapView<'msg, K, V> where K: 'msg, V: 'msg;
}
impl<K: Proxied + ?Sized, V: ProxiedInMapValue<K> + ?Sized> MutProxied for Map<K, V> {
type Mut<'msg> = MapMut<'msg, K, V> where K: 'msg, V: 'msg; type Mut<'msg> = MapMut<'msg, K, V> where K: 'msg, V: 'msg;
} }

@ -10,7 +10,9 @@
#![allow(unused)] #![allow(unused)]
use crate::__internal::Private; use crate::__internal::Private;
use crate::{Mut, MutProxy, Proxied, ProxiedWithPresence, SettableValue, View, ViewProxy}; use crate::{
Mut, MutProxied, MutProxy, Proxied, ProxiedWithPresence, SettableValue, View, ViewProxy,
};
use std::convert::{AsMut, AsRef}; use std::convert::{AsMut, AsRef};
use std::fmt::{self, Debug}; use std::fmt::{self, Debug};
use std::panic; use std::panic;
@ -495,6 +497,9 @@ mod tests {
impl Proxied for VtableProxied { impl Proxied for VtableProxied {
type View<'msg> = VtableProxiedView; type View<'msg> = VtableProxiedView;
}
impl MutProxied for VtableProxied {
type Mut<'msg> = VtableProxiedMut<'msg>; type Mut<'msg> = VtableProxiedMut<'msg>;
} }

@ -10,7 +10,9 @@ use std::fmt::Debug;
use crate::__internal::Private; use crate::__internal::Private;
use crate::__runtime::InnerPrimitiveMut; use crate::__runtime::InnerPrimitiveMut;
use crate::vtable::{PrimitiveWithRawVTable, ProxiedWithRawVTable, RawVTableOptionalMutatorData}; use crate::vtable::{PrimitiveWithRawVTable, ProxiedWithRawVTable, RawVTableOptionalMutatorData};
use crate::{Mut, MutProxy, Proxied, ProxiedWithPresence, SettableValue, View, ViewProxy}; use crate::{
Mut, MutProxied, MutProxy, Proxied, ProxiedWithPresence, SettableValue, View, ViewProxy,
};
/// A mutator for a primitive (numeric or enum) value of `T`. /// A mutator for a primitive (numeric or enum) value of `T`.
/// ///
@ -98,6 +100,9 @@ macro_rules! impl_singular_primitives {
$( $(
impl Proxied for $t { impl Proxied for $t {
type View<'msg> = $t; type View<'msg> = $t;
}
impl MutProxied for $t {
type Mut<'msg> = PrimitiveMut<'msg, $t>; type Mut<'msg> = PrimitiveMut<'msg, $t>;
} }

@ -53,13 +53,13 @@ macro_rules! proto_internal {
// nested message // nested message
(@msg $msg:ident $submsg:ident : $($msgtype:ident)::+ { $field:ident : $($value:tt)* }) => { (@msg $msg:ident $submsg:ident : $($msgtype:ident)::+ { $field:ident : $($value:tt)* }) => {
{ {
let mut $msg: <$($msgtype)::+ as $crate::Proxied>::Mut<'_> = $crate::__internal::paste!($msg.[<$submsg _mut>]()); let mut $msg: <$($msgtype)::+ as $crate::MutProxied>::Mut<'_> = $crate::__internal::paste!($msg.[<$submsg _mut>]());
proto_internal!(@msg $msg $field : $($value)*); proto_internal!(@msg $msg $field : $($value)*);
} }
}; };
(@msg $msg:ident $submsg:ident : ::$($msgtype:ident)::+ { $field:ident : $($value:tt)* }) => { (@msg $msg:ident $submsg:ident : ::$($msgtype:ident)::+ { $field:ident : $($value:tt)* }) => {
{ {
let mut $msg: <::$($msgtype)::+ as $crate::Proxied>::Mut<'_> = $crate::__internal::paste!($msg.[<$submsg _mut>]()); let mut $msg: <::$($msgtype)::+ as $crate::MutProxied>::Mut<'_> = $crate::__internal::paste!($msg.[<$submsg _mut>]());
proto_internal!(@msg $msg $field : $($value)*); proto_internal!(@msg $msg $field : $($value)*);
} }
}; };
@ -77,12 +77,12 @@ macro_rules! proto_internal {
// empty nested message // empty nested message
(@msg $msg:ident $submsg:ident : $($msgtype:ident)::+ { }) => { (@msg $msg:ident $submsg:ident : $($msgtype:ident)::+ { }) => {
{ {
let mut $msg: <$($msgtype)::+ as $crate::Proxied>::Mut<'_> = $crate::__internal::paste!($msg.[<$submsg _mut>]()); let mut $msg: <$($msgtype)::+ as $crate::MutProxied>::Mut<'_> = $crate::__internal::paste!($msg.[<$submsg _mut>]());
} }
}; };
(@msg $msg:ident $submsg:ident : ::$($msgtype:ident)::+ { }) => { (@msg $msg:ident $submsg:ident : ::$($msgtype:ident)::+ { }) => {
{ {
let mut $msg: <::$($msgtype)::+ as $crate::Proxied>::Mut<'_> = $crate::__internal::paste!($msg.[<$submsg _mut>]()); let mut $msg: <::$($msgtype)::+ as $crate::MutProxied>::Mut<'_> = $crate::__internal::paste!($msg.[<$submsg _mut>]());
} }
}; };

@ -51,18 +51,25 @@ use std::fmt::Debug;
/// A type that can be accessed through a reference-like proxy. /// A type that can be accessed through a reference-like proxy.
/// ///
/// An instance of a `Proxied` can be accessed /// An instance of a `Proxied` can be accessed immutably via `Proxied::View`.
/// immutably via `Proxied::View` and mutably via `Proxied::Mut`.
/// ///
/// All Protobuf field types implement `Proxied`. /// All Protobuf field types implement `Proxied`.
pub trait Proxied { pub trait Proxied {
/// The proxy type that provides shared access to a `T`, like a `&'msg T`. /// The proxy type that provides shared access to a `T`, like a `&'msg T`.
/// ///
/// Most code should use the type alias [`View`]. /// Most code should use the type alias [`View`].
type View<'msg>: ViewProxy<'msg, Proxied = Self> + Copy + Send + SettableValue<Self> type View<'msg>: ViewProxy<'msg, Proxied = Self> + Copy + Send
where where
Self: 'msg; Self: 'msg;
}
/// A type that can be be accessed through a reference-like proxy.
///
/// An instance of a `MutProxied` can be accessed mutably via `MutProxied::Mut`
/// and immutably via `MutProxied::View`.
///
/// `MutProxied` is implemented by message, map and repeated field types.
pub trait MutProxied: Proxied {
/// The proxy type that provides exclusive mutable access to a `T`, like a /// The proxy type that provides exclusive mutable access to a `T`, like a
/// `&'msg mut T`. /// `&'msg mut T`.
/// ///
@ -83,7 +90,7 @@ pub type View<'msg, T> = <T as Proxied>::View<'msg>;
/// ///
/// This is more concise than fully spelling the associated type. /// This is more concise than fully spelling the associated type.
#[allow(dead_code)] #[allow(dead_code)]
pub type Mut<'msg, T> = <T as Proxied>::Mut<'msg>; pub type Mut<'msg, T> = <T as MutProxied>::Mut<'msg>;
/// Declares conversion operations common to all views. /// Declares conversion operations common to all views.
/// ///
@ -147,7 +154,10 @@ pub trait ViewProxy<'msg>: 'msg + Sync + Unpin + Sized + Debug {
/// ///
/// This trait is intentionally made non-object-safe to prevent a potential /// This trait is intentionally made non-object-safe to prevent a potential
/// future incompatible change. /// future incompatible change.
pub trait MutProxy<'msg>: ViewProxy<'msg> { pub trait MutProxy<'msg>: ViewProxy<'msg>
where
Self::Proxied: MutProxied,
{
/// Gets an immutable view of this field. This is shorthand for `as_view`. /// 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 /// This provides a shorter lifetime than `into_view` but can also be called
@ -211,7 +221,7 @@ pub trait MutProxy<'msg>: ViewProxy<'msg> {
/// ///
/// All scalar and message types implement `ProxiedWithPresence`, while repeated /// All scalar and message types implement `ProxiedWithPresence`, while repeated
/// types don't. /// types don't.
pub trait ProxiedWithPresence: Proxied { pub trait ProxiedWithPresence: MutProxied {
/// The data necessary to store a present field mutator proxying `Self`. /// The data necessary to store a present field mutator proxying `Self`.
/// This is the contents of `PresentField<'msg, Self>`. /// This is the contents of `PresentField<'msg, Self>`.
type PresentMutData<'msg>: MutProxy<'msg, Proxied = Self>; type PresentMutData<'msg>: MutProxy<'msg, Proxied = Self>;
@ -233,7 +243,7 @@ pub trait ProxiedWithPresence: Proxied {
/// Values that can be used to set a field of `T`. /// Values that can be used to set a field of `T`.
pub trait SettableValue<T>: Sized pub trait SettableValue<T>: Sized
where where
T: Proxied + ?Sized, T: MutProxied + ?Sized,
{ {
/// Consumes `self` to set the given mutator to the value of `self`. /// Consumes `self` to set the given mutator to the value of `self`.
#[doc(hidden)] #[doc(hidden)]
@ -308,6 +318,9 @@ mod tests {
impl Proxied for MyProxied { impl Proxied for MyProxied {
type View<'msg> = MyProxiedView<'msg>; type View<'msg> = MyProxiedView<'msg>;
}
impl MutProxied for MyProxied {
type Mut<'msg> = MyProxiedMut<'msg>; type Mut<'msg> = MyProxiedMut<'msg>;
} }
@ -460,7 +473,7 @@ mod tests {
y: &'b View<'a, T>, y: &'b View<'a, T>,
) -> [View<'b, T>; 2] ) -> [View<'b, T>; 2]
where where
T: Proxied, T: MutProxied,
'a: 'b, 'a: 'b,
{ {
// `[x, y]` fails to compile because `'a` is not the same as `'b` and the `View` // `[x, y]` fails to compile because `'a` is not the same as `'b` and the `View`
@ -509,7 +522,7 @@ mod tests {
fn reborrow_generic_mut_into_view<'a, 'b, T>(x: Mut<'a, T>, y: View<'b, T>) -> [View<'b, T>; 2] fn reborrow_generic_mut_into_view<'a, 'b, T>(x: Mut<'a, T>, y: View<'b, T>) -> [View<'b, T>; 2]
where where
T: Proxied, T: MutProxied,
'a: 'b, 'a: 'b,
{ {
[x.into_view(), y] [x.into_view(), y]
@ -529,7 +542,7 @@ mod tests {
fn reborrow_generic_mut_into_mut<'a, 'b, T>(x: Mut<'a, T>, y: Mut<'b, T>) -> [Mut<'b, T>; 2] fn reborrow_generic_mut_into_mut<'a, 'b, T>(x: Mut<'a, T>, y: Mut<'b, T>) -> [Mut<'b, T>; 2]
where where
T: Proxied, T: MutProxied,
'a: 'b, 'a: 'b,
{ {
// `[x, y]` fails to compile because `'a` is not the same as `'b` and the `Mut` // `[x, y]` fails to compile because `'a` is not the same as `'b` and the `Mut`

@ -15,7 +15,7 @@ use std::iter::FusedIterator;
use std::marker::PhantomData; use std::marker::PhantomData;
use crate::{ use crate::{
Mut, MutProxy, Proxied, SettableValue, View, ViewProxy, Mut, MutProxied, MutProxy, Proxied, SettableValue, View, ViewProxy,
__internal::Private, __internal::Private,
__runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField}, __runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField},
}; };
@ -314,6 +314,12 @@ where
T: ProxiedInRepeated + ?Sized, T: ProxiedInRepeated + ?Sized,
{ {
type View<'msg> = RepeatedView<'msg, T> where Repeated<T>: 'msg; type View<'msg> = RepeatedView<'msg, T> where Repeated<T>: 'msg;
}
impl<T> MutProxied for Repeated<T>
where
T: ProxiedInRepeated + ?Sized,
{
type Mut<'msg> = RepeatedMut<'msg, T> where Repeated<T>: 'msg; type Mut<'msg> = RepeatedMut<'msg, T> where Repeated<T>: 'msg;
} }

@ -28,7 +28,7 @@ pub mod __public {
pub use crate::primitive::PrimitiveMut; pub use crate::primitive::PrimitiveMut;
pub use crate::proto; pub use crate::proto;
pub use crate::proxied::{ pub use crate::proxied::{
Mut, MutProxy, Proxied, ProxiedWithPresence, SettableValue, View, ViewProxy, Mut, MutProxied, MutProxy, Proxied, ProxiedWithPresence, SettableValue, View, ViewProxy,
}; };
pub use crate::repeated::{ pub use crate::repeated::{
ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView, ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView,

@ -15,8 +15,8 @@ use crate::__runtime::{
}; };
use crate::macros::impl_forwarding_settable_value; use crate::macros::impl_forwarding_settable_value;
use crate::{ use crate::{
AbsentField, FieldEntry, Mut, MutProxy, Optional, PresentField, Proxied, ProxiedWithPresence, AbsentField, FieldEntry, Mut, MutProxied, MutProxy, Optional, PresentField, Proxied,
SettableValue, View, ViewProxy, ProxiedWithPresence, SettableValue, View, ViewProxy,
}; };
use std::borrow::Cow; use std::borrow::Cow;
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}; use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
@ -123,6 +123,9 @@ impl AsRef<[u8]> for BytesMut<'_> {
impl Proxied for [u8] { impl Proxied for [u8] {
type View<'msg> = &'msg [u8]; type View<'msg> = &'msg [u8];
}
impl MutProxied for [u8] {
type Mut<'msg> = BytesMut<'msg>; type Mut<'msg> = BytesMut<'msg>;
} }
@ -465,6 +468,9 @@ impl Ord for ProtoStr {
impl Proxied for ProtoStr { impl Proxied for ProtoStr {
type View<'msg> = &'msg ProtoStr; type View<'msg> = &'msg ProtoStr;
}
impl MutProxied for ProtoStr {
type Mut<'msg> = ProtoStrMut<'msg>; type Mut<'msg> = ProtoStrMut<'msg>;
} }

@ -11,7 +11,7 @@ use crate::__runtime::{
RawMessage, RawMessage,
}; };
use crate::{ use crate::{
AbsentField, FieldEntry, Mut, MutProxy, Optional, PresentField, PrimitiveMut, Proxied, AbsentField, FieldEntry, Mut, MutProxied, MutProxy, Optional, PresentField, PrimitiveMut,
ProxiedWithPresence, View, ViewProxy, ProxiedWithPresence, View, ViewProxy,
}; };
use std::fmt::{self, Debug}; use std::fmt::{self, Debug};
@ -23,7 +23,7 @@ use std::ptr::NonNull;
/// ///
/// This vtable should consist of `unsafe fn`s that call thunks that operate on /// This vtable should consist of `unsafe fn`s that call thunks that operate on
/// `RawMessage`. The structure of this vtable is different per proxied type. /// `RawMessage`. The structure of this vtable is different per proxied type.
pub trait ProxiedWithRawVTable: Proxied { pub trait ProxiedWithRawVTable: MutProxied {
/// The vtable for get/set access, stored in static memory. /// The vtable for get/set access, stored in static memory.
type VTable: Debug + 'static; type VTable: Debug + 'static;
@ -443,7 +443,7 @@ pub trait PrimitiveWithRawVTable:
+ ProxiedWithPresence + ProxiedWithPresence
+ Sync + Sync
+ Send + Send
+ for<'msg> Proxied<View<'msg> = Self, Mut<'msg> = PrimitiveMut<'msg, Self>> + for<'msg> MutProxied<View<'msg> = Self, Mut<'msg> = PrimitiveMut<'msg, Self>>
{ {
} }

@ -393,6 +393,9 @@ void GenerateEnumDefinition(Context& ctx, const EnumDescriptor& desc) {
impl $pb$::Proxied for $name$ { impl $pb$::Proxied for $name$ {
type View<'a> = $name$; type View<'a> = $name$;
}
impl $pb$::MutProxied for $name$ {
type Mut<'a> = $pb$::PrimitiveMut<'a, $name$>; type Mut<'a> = $pb$::PrimitiveMut<'a, $name$>;
} }

@ -865,6 +865,9 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
impl $pb$::Proxied for $Msg$ { impl $pb$::Proxied for $Msg$ {
type View<'msg> = $Msg$View<'msg>; type View<'msg> = $Msg$View<'msg>;
}
impl $pb$::MutProxied for $Msg$ {
type Mut<'msg> = $Msg$Mut<'msg>; type Mut<'msg> = $Msg$Mut<'msg>;
} }

Loading…
Cancel
Save