From e6304eb3a27428370bbcfe6b217caf1f340aa7c5 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 24 Jul 2024 12:28:32 -0700 Subject: [PATCH] Add AsView + AsMut as supertraits of Proxied and MutProxied. PiperOrigin-RevId: 655660782 --- rust/codegen_traits.rs | 22 ++++++++++------------ rust/cord.rs | 9 ++++++++- rust/internal.rs | 1 + rust/map.rs | 16 ++++++++++++++++ rust/proxied.rs | 18 ++++++++++++++++-- rust/repeated.rs | 22 ++++++++++++++++++++++ rust/string.rs | 16 ++++++++++++++++ 7 files changed, 89 insertions(+), 15 deletions(-) diff --git a/rust/codegen_traits.rs b/rust/codegen_traits.rs index d1a07dea25..ddfe3cfd17 100644 --- a/rust/codegen_traits.rs +++ b/rust/codegen_traits.rs @@ -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; @@ -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: @@ -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 @@ -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; } diff --git a/rust/cord.rs b/rust/cord.rs index 4715d287a9..14c9a60991 100644 --- a/rust/cord.rs +++ b/rust/cord.rs @@ -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> { @@ -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> {} diff --git a/rust/internal.rs b/rust/internal.rs index f8118c88d6..c2bdd007c8 100644 --- a/rust/internal.rs +++ b/rust/internal.rs @@ -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; diff --git a/rust/map.rs b/rust/map.rs index 7013226ec7..f136614830 100644 --- a/rust/map.rs +++ b/rust/map.rs @@ -109,10 +109,26 @@ impl> Proxied for Map { type View<'msg> = MapView<'msg, K, V> where K: 'msg, V: 'msg; } +impl> AsView for Map { + type Proxied = Self; + + fn as_view(&self) -> MapView<'_, K, V> { + self.as_view() + } +} + impl> MutProxied for Map { type Mut<'msg> = MapMut<'msg, K, V> where K: 'msg, V: 'msg; } +impl> AsMut for Map { + type MutProxied = Self; + + fn as_mut(&mut self) -> MapMut<'_, K, V> { + self.as_mut() + } +} + impl<'msg, K: Proxied, V: ProxiedInMapValue> Proxy<'msg> for MapView<'msg, K, V> {} impl<'msg, K: Proxied, V: ProxiedInMapValue> AsView for MapView<'msg, K, V> { diff --git a/rust/proxied.rs b/rust/proxied.rs index b8d327fd3c..be941fa3cd 100644 --- a/rust/proxied.rs +++ b/rust/proxied.rs @@ -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 + Sized { /// The proxy type that provides shared access to a `T`, like a `&'msg T`. /// /// Most code should use the type alias [`View`]. @@ -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 { /// The proxy type that provides exclusive mutable access to a `T`, like a /// `&'msg mut T`. /// @@ -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, diff --git a/rust/repeated.rs b/rust/repeated.rs index 0db3f64a96..972209bec2 100644 --- a/rust/repeated.rs +++ b/rust/repeated.rs @@ -391,6 +391,17 @@ where type View<'msg> = RepeatedView<'msg, T> where Repeated: 'msg; } +impl AsView for Repeated +where + T: ProxiedInRepeated, +{ + type Proxied = Self; + + fn as_view(&self) -> RepeatedView<'_, T> { + self.as_view() + } +} + impl MutProxied for Repeated where T: ProxiedInRepeated, @@ -398,6 +409,17 @@ where type Mut<'msg> = RepeatedMut<'msg, T> where Repeated: 'msg; } +impl AsMut for Repeated +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> diff --git a/rust/string.rs b/rust/string.rs index 89e6f367aa..748fe9b71e 100644 --- a/rust/string.rs +++ b/rust/string.rs @@ -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 for ProtoBytes { fn into_proxied(self, _private: Private) -> ProtoBytes { self @@ -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 {