Skip to content

Commit

Permalink
Remove gencode that uses vtables for string/bytes field accessors
Browse files Browse the repository at this point in the history
It's now unused.

PiperOrigin-RevId: 627626514
  • Loading branch information
buchgr authored and Copybara-Service committed Apr 24, 2024
1 parent 2c16dec commit 5a5c91d
Show file tree
Hide file tree
Showing 17 changed files with 211 additions and 269 deletions.
16 changes: 7 additions & 9 deletions rust/cpp.rs
Expand Up @@ -171,6 +171,13 @@ impl Deref for SerializedData {
}
}

// TODO: remove after IntoProxied has been implemented for bytes.
impl AsRef<[u8]> for SerializedData {
fn as_ref(&self) -> &[u8] {
self
}
}

impl Drop for SerializedData {
fn drop(&mut self) {
// SAFETY: `data` was allocated by the Rust global allocator with a
Expand All @@ -185,15 +192,6 @@ impl fmt::Debug for SerializedData {
}
}

impl SettableValue<[u8]> for SerializedData {
fn set_on<'msg>(self, _private: Private, mut mutator: Mut<'msg, [u8]>)
where
[u8]: 'msg,
{
mutator.set(self.as_ref())
}
}

/// A type to transfer an owned Rust string across the FFI boundary:
/// * This struct is ABI-compatible with the equivalent C struct.
/// * It owns its data but does not drop it. Immediately turn it into a
Expand Down
5 changes: 4 additions & 1 deletion rust/map.rs
Expand Up @@ -6,7 +6,7 @@
// https://developers.google.com/open-source/licenses/bsd

use crate::{
Mut, MutProxy, Proxied, SettableValue, View, ViewProxy,
Mut, MutProxied, MutProxy, Proxied, SettableValue, View, ViewProxy,
__internal::Private,
__runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter},
};
Expand Down Expand Up @@ -95,6 +95,9 @@ where

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

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

Expand Down
7 changes: 6 additions & 1 deletion rust/optional.rs
Expand Up @@ -10,7 +10,9 @@
#![allow(unused)]

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::fmt::{self, Debug};
use std::panic;
Expand Down Expand Up @@ -495,6 +497,9 @@ mod tests {

impl Proxied for VtableProxied {
type View<'msg> = VtableProxiedView;
}

impl MutProxied for VtableProxied {
type Mut<'msg> = VtableProxiedMut<'msg>;
}

Expand Down
7 changes: 6 additions & 1 deletion rust/primitive.rs
Expand Up @@ -10,7 +10,9 @@ use std::fmt::Debug;
use crate::__internal::Private;
use crate::__runtime::InnerPrimitiveMut;
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`.
///
Expand Down Expand Up @@ -98,6 +100,9 @@ macro_rules! impl_singular_primitives {
$(
impl Proxied for $t {
type View<'msg> = $t;
}

impl MutProxied for $t {
type Mut<'msg> = PrimitiveMut<'msg, $t>;
}

Expand Down
8 changes: 4 additions & 4 deletions rust/proto_macro.rs
Expand Up @@ -53,13 +53,13 @@ macro_rules! proto_internal {
// nested message
(@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)*);
}
};
(@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)*);
}
};
Expand All @@ -77,12 +77,12 @@ macro_rules! proto_internal {
// empty nested message
(@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)::+ { }) => {
{
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>]());
}
};

Expand Down
48 changes: 37 additions & 11 deletions rust/proxied.rs
Expand Up @@ -51,18 +51,25 @@ 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` and mutably via `Proxied::Mut`.
/// An instance of a `Proxied` can be accessed immutably via `Proxied::View`.
///
/// All Protobuf field types implement `Proxied`.
pub trait Proxied {
/// 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 + SettableValue<Self>
type View<'msg>: ViewProxy<'msg, Proxied = Self> + Copy + Send
where
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
/// `&'msg mut T`.
///
Expand All @@ -83,7 +90,7 @@ pub type View<'msg, T> = <T as Proxied>::View<'msg>;
///
/// This is more concise than fully spelling the associated type.
#[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.
///
Expand Down Expand Up @@ -127,7 +134,7 @@ pub trait ViewProxy<'msg>: 'msg + Sync + Unpin + Sized + Debug {
/// y: View<'b, T>,
/// ) -> [View<'b, T>; 2]
/// where
/// T: Proxied,
/// T: MutProxied,
/// 'a: 'b,
/// {
/// // `[x, y]` fails to compile because `'a` is not the same as `'b` and the `View`
Expand All @@ -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
/// 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`.
///
/// This provides a shorter lifetime than `into_view` but can also be called
Expand Down Expand Up @@ -211,7 +221,7 @@ pub trait MutProxy<'msg>: ViewProxy<'msg> {
///
/// All scalar and message types implement `ProxiedWithPresence`, while repeated
/// types don't.
pub trait ProxiedWithPresence: Proxied {
pub trait ProxiedWithPresence: MutProxied {
/// The data necessary to store a present field mutator proxying `Self`.
/// This is the contents of `PresentField<'msg, Self>`.
type PresentMutData<'msg>: MutProxy<'msg, Proxied = Self>;
Expand All @@ -233,7 +243,7 @@ pub trait ProxiedWithPresence: Proxied {
/// Values that can be used to set a field of `T`.
pub trait SettableValue<T>: Sized
where
T: Proxied + ?Sized,
T: MutProxied + ?Sized,
{
/// Consumes `self` to set the given mutator to the value of `self`.
#[doc(hidden)]
Expand Down Expand Up @@ -285,6 +295,19 @@ where
}
}

/// A value to `Proxied`-value conversion that consumes the input value.
///
/// All setter functions accept types that implement `IntoProxied`. The purpose
/// of `IntoProxied` is to allow setting arbitrary values on Protobuf fields
/// with the minimal number of copies.
///
/// This trait must not be implemented on types outside the Protobuf codegen and
/// runtime. We expect it to change in backwards incompatible ways in the
/// future.
pub trait IntoProxied<T: Proxied> {
fn into(self, _private: Private) -> T;
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -308,6 +331,9 @@ mod tests {

impl Proxied for MyProxied {
type View<'msg> = MyProxiedView<'msg>;
}

impl MutProxied for MyProxied {
type Mut<'msg> = MyProxiedMut<'msg>;
}

Expand Down Expand Up @@ -460,7 +486,7 @@ mod tests {
y: &'b View<'a, T>,
) -> [View<'b, T>; 2]
where
T: Proxied,
T: MutProxied,
'a: 'b,
{
// `[x, y]` fails to compile because `'a` is not the same as `'b` and the `View`
Expand Down Expand Up @@ -509,7 +535,7 @@ mod tests {

fn reborrow_generic_mut_into_view<'a, 'b, T>(x: Mut<'a, T>, y: View<'b, T>) -> [View<'b, T>; 2]
where
T: Proxied,
T: MutProxied,
'a: 'b,
{
[x.into_view(), y]
Expand All @@ -529,7 +555,7 @@ mod tests {

fn reborrow_generic_mut_into_mut<'a, 'b, T>(x: Mut<'a, T>, y: Mut<'b, T>) -> [Mut<'b, T>; 2]
where
T: Proxied,
T: MutProxied,
'a: 'b,
{
// `[x, y]` fails to compile because `'a` is not the same as `'b` and the `Mut`
Expand Down
8 changes: 7 additions & 1 deletion rust/repeated.rs
Expand Up @@ -15,7 +15,7 @@ use std::iter::FusedIterator;
use std::marker::PhantomData;

use crate::{
Mut, MutProxy, Proxied, SettableValue, View, ViewProxy,
Mut, MutProxied, MutProxy, Proxied, SettableValue, View, ViewProxy,
__internal::Private,
__runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField},
};
Expand Down Expand Up @@ -314,6 +314,12 @@ where
T: ProxiedInRepeated + ?Sized,
{
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;
}

Expand Down
3 changes: 2 additions & 1 deletion rust/shared.rs
Expand Up @@ -28,7 +28,8 @@ pub mod __public {
pub use crate::primitive::PrimitiveMut;
pub use crate::proto;
pub use crate::proxied::{
Mut, MutProxy, Proxied, ProxiedWithPresence, SettableValue, View, ViewProxy,
IntoProxied, Mut, MutProxied, MutProxy, Proxied, ProxiedWithPresence, SettableValue, View,
ViewProxy,
};
pub use crate::repeated::{
ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView,
Expand Down

0 comments on commit 5a5c91d

Please sign in to comment.