Skip to content

Commit

Permalink
Add AsView + AsMut as supertraits of Proxied and MutProxied.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 655660782
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jul 24, 2024
1 parent e3fa6aa commit e6304eb
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 15 deletions.
22 changes: 10 additions & 12 deletions rust/codegen_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

//! Traits that are implemeted by codegen types.
use crate::{AsMut, AsView, MutProxied, MutProxy, ViewProxy};
use crate::{MutProxied, MutProxy, ViewProxy};
use create::Parse;
use read::Serialize;
use std::fmt::Debug;
Expand All @@ -16,12 +16,12 @@ use write::ClearAndParse;
/// A trait that all generated owned message types implement.
pub trait Message: MutProxied
// Create traits:
+ create::Parse + Default
+ Parse + Default
// Read traits:
+ Debug + Serialize + AsView
+ Debug + Serialize
// Write traits:
// TODO: Msg should impl Clear.
+ ClearAndParse + AsMut
+ ClearAndParse
// Thread safety:
+ Send + Sync
// Copy/Clone:
Expand All @@ -31,23 +31,22 @@ pub trait Message: MutProxied
/// A trait that all generated message views implement.
pub trait MessageView<'msg>: ViewProxy<'msg, Proxied = Self::Message>
// Read traits:
+ Debug + Serialize + AsView
+ Debug + Serialize
// Thread safety:
+ Send + Sync
// Copy/Clone:
+ Copy + Clone
{
#[doc(hidden)]
type Message: Message;
}
{
#[doc(hidden)]
type Message: Message;
}

/// A trait that all generated message muts implement.
pub trait MessageMut<'msg>:
MutProxy<'msg, MutProxied = Self::Message>
// Read traits:
+ Debug + Serialize + AsView
+ Debug + Serialize
// Write traits:
+ AsMut
// TODO: MsgMut should impl Clear and ClearAndParse.
// Thread safety:
+ Sync
Expand All @@ -60,7 +59,6 @@ pub trait MessageMut<'msg>:
/// Operations related to constructing a message. Only owned messages implement
/// these traits.
pub(crate) mod create {

pub trait Parse: Sized {
fn parse(serialized: &[u8]) -> Result<Self, crate::ParseError>;
}
Expand Down
9 changes: 8 additions & 1 deletion rust/cord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ macro_rules! impl_cord_types {
($($t:ty, $vt:ty);*) => {
paste! { $(
#[derive(Debug)]
pub struct [< $t Cord>];
pub struct [< $t Cord>](Private);

#[derive(Debug)]
pub enum [< $t Cow>]<'a> {
Expand All @@ -30,6 +30,13 @@ macro_rules! impl_cord_types {
type View<'msg> = [< $t Cow>]<'msg>;
}

impl AsView for [< $t Cord>] {
type Proxied = Self;
fn as_view(&self) -> [< $t Cow>]<'_> {
unimplemented!("Proto Cord should never be constructed");
}
}

impl<'msg> Proxy<'msg> for [< $t Cow>]<'msg> {}

impl<'msg> ViewProxy<'msg> for [< $t Cow>]<'msg> {}
Expand Down
1 change: 1 addition & 0 deletions rust/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ pub use crate::ProtoStr;
pub use crate::__runtime::{PtrAndLen, RawMap, RawMessage, RawRepeatedField};

/// Used to protect internal-only items from being used accidentally.
#[derive(Debug)]
pub struct Private;
16 changes: 16 additions & 0 deletions rust/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,26 @@ impl<K: Proxied, V: ProxiedInMapValue<K>> Proxied for Map<K, V> {
type View<'msg> = MapView<'msg, K, V> where K: 'msg, V: 'msg;
}

impl<K: Proxied, V: ProxiedInMapValue<K>> AsView for Map<K, V> {
type Proxied = Self;

fn as_view(&self) -> MapView<'_, K, V> {
self.as_view()
}
}

impl<K: Proxied, V: ProxiedInMapValue<K>> MutProxied for Map<K, V> {
type Mut<'msg> = MapMut<'msg, K, V> where K: 'msg, V: 'msg;
}

impl<K: Proxied, V: ProxiedInMapValue<K>> AsMut for Map<K, V> {
type MutProxied = Self;

fn as_mut(&mut self) -> MapMut<'_, K, V> {
self.as_mut()
}
}

impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> Proxy<'msg> for MapView<'msg, K, V> {}

impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> AsView for MapView<'msg, K, V> {
Expand Down
18 changes: 16 additions & 2 deletions rust/proxied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use std::fmt::Debug;
/// An instance of a `Proxied` can be accessed immutably via `Proxied::View`.
///
/// All Protobuf field types implement `Proxied`.
pub trait Proxied: Sized {
pub trait Proxied: AsView<Proxied = Self> + Sized {
/// The proxy type that provides shared access to a `T`, like a `&'msg T`.
///
/// Most code should use the type alias [`View`].
Expand All @@ -67,7 +67,7 @@ pub trait Proxied: Sized {
/// and immutably via `MutProxied::View`.
///
/// `MutProxied` is implemented by message, map and repeated field types.
pub trait MutProxied: Proxied {
pub trait MutProxied: Proxied + AsMut<MutProxied = Self> {
/// The proxy type that provides exclusive mutable access to a `T`, like a
/// `&'msg mut T`.
///
Expand Down Expand Up @@ -263,10 +263,24 @@ mod tests {
type View<'msg> = MyProxiedView<'msg>;
}

impl AsView for MyProxied {
type Proxied = Self;
fn as_view(&self) -> MyProxiedView<'_> {
self.as_view()
}
}

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

impl AsMut for MyProxied {
type MutProxied = Self;
fn as_mut(&mut self) -> MyProxiedMut<'_> {
self.as_mut()
}
}

#[derive(Debug, Clone, Copy)]
struct MyProxiedView<'msg> {
my_proxied_ref: &'msg MyProxied,
Expand Down
22 changes: 22 additions & 0 deletions rust/repeated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,35 @@ where
type View<'msg> = RepeatedView<'msg, T> where Repeated<T>: 'msg;
}

impl<T> AsView for Repeated<T>
where
T: ProxiedInRepeated,
{
type Proxied = Self;

fn as_view(&self) -> RepeatedView<'_, T> {
self.as_view()
}
}

impl<T> MutProxied for Repeated<T>
where
T: ProxiedInRepeated,
{
type Mut<'msg> = RepeatedMut<'msg, T> where Repeated<T>: 'msg;
}

impl<T> AsMut for Repeated<T>
where
T: ProxiedInRepeated,
{
type MutProxied = Self;

fn as_mut(&mut self) -> RepeatedMut<'_, T> {
self.as_mut()
}
}

impl<'msg, T> Proxy<'msg> for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg {}

impl<'msg, T> AsView for RepeatedView<'msg, T>
Expand Down
16 changes: 16 additions & 0 deletions rust/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ impl Proxied for ProtoBytes {
type View<'msg> = &'msg [u8];
}

impl AsView for ProtoBytes {
type Proxied = Self;

fn as_view(&self) -> &[u8] {
self.as_view()
}
}

impl IntoProxied<ProtoBytes> for ProtoBytes {
fn into_proxied(self, _private: Private) -> ProtoBytes {
self
Expand Down Expand Up @@ -512,6 +520,14 @@ impl Proxied for ProtoString {
type View<'msg> = &'msg ProtoStr;
}

impl AsView for ProtoString {
type Proxied = Self;

fn as_view(&self) -> &ProtoStr {
self.as_view()
}
}

impl<'msg> Proxy<'msg> for &'msg ProtoStr {}

impl AsView for &ProtoStr {
Expand Down

0 comments on commit e6304eb

Please sign in to comment.