Skip to content

Commit

Permalink
Remove PrimitiveMut and related vtable types
Browse files Browse the repository at this point in the history
The last callside that used PrimitiveMut was in our enums code. This change makes it so that enums nolonger implement MutProxied and thus no longer need the PrimitiveMut type.

PiperOrigin-RevId: 627632154
  • Loading branch information
buchgr authored and Copybara-Service committed Apr 24, 2024
1 parent 2c16dec commit 0b3e8a8
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 559 deletions.
2 changes: 1 addition & 1 deletion rust/BUILD
Expand Up @@ -49,8 +49,8 @@ PROTOBUF_SHARED = [
"enum.rs",
"internal.rs",
"macros.rs",
"optional.rs",
"primitive.rs",
"optional.rs",
"proxied.rs",
"repeated.rs",
"shared.rs",
Expand Down
17 changes: 7 additions & 10 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 Expand Up @@ -240,7 +238,6 @@ pub type MessageAbsentMutData<'msg, T> = crate::vtable::RawVTableOptionalMutator
pub type BytesPresentMutData<'msg> = crate::vtable::RawVTableOptionalMutatorData<'msg, [u8]>;
pub type BytesAbsentMutData<'msg> = crate::vtable::RawVTableOptionalMutatorData<'msg, [u8]>;
pub type InnerBytesMut<'msg> = crate::vtable::RawVTableMutator<'msg, [u8]>;
pub type InnerPrimitiveMut<'msg, T> = crate::vtable::RawVTableMutator<'msg, T>;
pub type RawMapIter = UntypedMapIterator;

#[derive(Debug)]
Expand Down
5 changes: 2 additions & 3 deletions rust/internal.rs
Expand Up @@ -14,9 +14,8 @@ pub use paste::paste;

pub use crate::r#enum::Enum;
pub use crate::vtable::{
new_vtable_field_entry, BytesMutVTable, BytesOptionalMutVTable, PrimitiveOptionalMutVTable,
PrimitiveVTable, PrimitiveWithRawVTable, ProxiedWithRawOptionalVTable, ProxiedWithRawVTable,
RawVTableMutator, RawVTableOptionalMutatorData,
new_vtable_field_entry, BytesMutVTable, BytesOptionalMutVTable, ProxiedWithRawOptionalVTable,
ProxiedWithRawVTable, RawVTableMutator, RawVTableOptionalMutatorData,
};
pub use crate::ProtoStr;

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
124 changes: 1 addition & 123 deletions rust/primitive.rs
Expand Up @@ -4,101 +4,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

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

/// A mutator for a primitive (numeric or enum) value of `T`.
///
/// This type is `protobuf::Mut<'msg, T>`.
pub struct PrimitiveMut<'msg, T> {
inner: InnerPrimitiveMut<'msg, T>,
}

impl<'msg, T> Debug for PrimitiveMut<'msg, T>
where
T: PrimitiveWithRawVTable,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PrimitiveMut").field("inner", &self.inner).finish()
}
}

impl<'msg, T> PrimitiveMut<'msg, T> {
/// # Safety
/// `inner` must be valid and non-aliased for `T` for `'msg`
#[doc(hidden)]
pub unsafe fn from_inner(_private: Private, inner: InnerPrimitiveMut<'msg, T>) -> Self {
Self { inner }
}
}

// SAFETY: all `T` that can perform mutations don't mutate through a shared
// reference.
unsafe impl<'msg, T> Sync for PrimitiveMut<'msg, T> {}

impl<'msg, T> PrimitiveMut<'msg, T>
where
T: PrimitiveWithRawVTable,
{
/// Gets the current value of the field.
pub fn get(&self) -> View<'_, T> {
T::make_view(Private, self.inner)
}

/// Sets a new value for the field.
pub fn set(&mut self, val: impl SettableValue<T>) {
val.set_on(Private, self.as_mut())
}

#[doc(hidden)]
pub fn set_primitive(&mut self, _private: Private, value: T) {
// SAFETY: the raw mutator is valid for `'msg` as enforced by `Mut`
unsafe { self.inner.set(value) }
}
}

impl<'msg, T> ViewProxy<'msg> for PrimitiveMut<'msg, T>
where
T: PrimitiveWithRawVTable,
{
type Proxied = T;

fn as_view(&self) -> View<'_, Self::Proxied> {
self.get()
}

fn into_view<'shorter>(self) -> View<'shorter, Self::Proxied> {
self.get()
}
}

impl<'msg, T> MutProxy<'msg> for PrimitiveMut<'msg, T>
where
T: PrimitiveWithRawVTable,
{
fn as_mut(&mut self) -> Mut<'_, Self::Proxied> {
PrimitiveMut { inner: self.inner }
}

fn into_mut<'shorter>(self) -> Mut<'shorter, Self::Proxied>
where
'msg: 'shorter,
{
self
}
}
use crate::{Proxied, View, ViewProxy};

macro_rules! impl_singular_primitives {
($($t:ty),*) => {
$(
impl Proxied for $t {
type View<'msg> = $t;
type Mut<'msg> = PrimitiveMut<'msg, $t>;
}

impl<'msg> ViewProxy<'msg> for $t {
Expand All @@ -113,40 +25,6 @@ macro_rules! impl_singular_primitives {
}
}

impl SettableValue<$t> for $t {
fn set_on<'msg>(self, private: Private, mut mutator: Mut<'msg, $t>) where $t: 'msg {
mutator.set_primitive(private, self)
}

fn set_on_absent(
self,
_private: Private,
absent_mutator: <$t as ProxiedWithPresence>::PresentMutData<'_>,
) -> <$t as ProxiedWithPresence>::AbsentMutData<'_>
{
absent_mutator.set(Private, self)
}
}

impl ProxiedWithPresence for $t {
type PresentMutData<'msg> = RawVTableOptionalMutatorData<'msg, $t>;
type AbsentMutData<'msg> = RawVTableOptionalMutatorData<'msg, $t>;

fn clear_present_field(
present_mutator: Self::PresentMutData<'_>,
) -> Self::AbsentMutData<'_> {
present_mutator.clear(Private)
}

fn set_absent_to_default(
absent_mutator: Self::AbsentMutData<'_>,
) -> Self::PresentMutData<'_> {
absent_mutator.set_absent_to_default(Private)
}
}

impl PrimitiveWithRawVTable for $t {}

// ProxiedInRepeated is implemented in {cpp,upb}.rs
)*
}
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

0 comments on commit 0b3e8a8

Please sign in to comment.