Add AsView + AsMut as supertraits of Proxied and MutProxied.

PiperOrigin-RevId: 655660782
pull/17593/head
Protobuf Team Bot 8 months ago committed by Copybara-Service
parent e3fa6aac29
commit e6304eb3a2
  1. 22
      rust/codegen_traits.rs
  2. 9
      rust/cord.rs
  3. 1
      rust/internal.rs
  4. 16
      rust/map.rs
  5. 18
      rust/proxied.rs
  6. 22
      rust/repeated.rs
  7. 16
      rust/string.rs

@ -7,7 +7,7 @@
//! Traits that are implemeted by codegen types. //! Traits that are implemeted by codegen types.
use crate::{AsMut, AsView, MutProxied, MutProxy, ViewProxy}; use crate::{MutProxied, MutProxy, ViewProxy};
use create::Parse; use create::Parse;
use read::Serialize; use read::Serialize;
use std::fmt::Debug; use std::fmt::Debug;
@ -16,12 +16,12 @@ use write::ClearAndParse;
/// A trait that all generated owned message types implement. /// A trait that all generated owned message types implement.
pub trait Message: MutProxied pub trait Message: MutProxied
// Create traits: // Create traits:
+ create::Parse + Default + Parse + Default
// Read traits: // Read traits:
+ Debug + Serialize + AsView + Debug + Serialize
// Write traits: // Write traits:
// TODO: Msg should impl Clear. // TODO: Msg should impl Clear.
+ ClearAndParse + AsMut + ClearAndParse
// Thread safety: // Thread safety:
+ Send + Sync + Send + Sync
// Copy/Clone: // Copy/Clone:
@ -31,23 +31,22 @@ pub trait Message: MutProxied
/// A trait that all generated message views implement. /// A trait that all generated message views implement.
pub trait MessageView<'msg>: ViewProxy<'msg, Proxied = Self::Message> pub trait MessageView<'msg>: ViewProxy<'msg, Proxied = Self::Message>
// Read traits: // Read traits:
+ Debug + Serialize + AsView + Debug + Serialize
// Thread safety: // Thread safety:
+ Send + Sync + Send + Sync
// Copy/Clone: // Copy/Clone:
+ Copy + Clone + Copy + Clone
{ {
#[doc(hidden)] #[doc(hidden)]
type Message: Message; type Message: Message;
} }
/// A trait that all generated message muts implement. /// A trait that all generated message muts implement.
pub trait MessageMut<'msg>: pub trait MessageMut<'msg>:
MutProxy<'msg, MutProxied = Self::Message> MutProxy<'msg, MutProxied = Self::Message>
// Read traits: // Read traits:
+ Debug + Serialize + AsView + Debug + Serialize
// Write traits: // Write traits:
+ AsMut
// TODO: MsgMut should impl Clear and ClearAndParse. // TODO: MsgMut should impl Clear and ClearAndParse.
// Thread safety: // Thread safety:
+ Sync + Sync
@ -60,7 +59,6 @@ pub trait MessageMut<'msg>:
/// Operations related to constructing a message. Only owned messages implement /// Operations related to constructing a message. Only owned messages implement
/// these traits. /// these traits.
pub(crate) mod create { pub(crate) mod create {
pub trait Parse: Sized { pub trait Parse: Sized {
fn parse(serialized: &[u8]) -> Result<Self, crate::ParseError>; fn parse(serialized: &[u8]) -> Result<Self, crate::ParseError>;
} }

@ -18,7 +18,7 @@ macro_rules! impl_cord_types {
($($t:ty, $vt:ty);*) => { ($($t:ty, $vt:ty);*) => {
paste! { $( paste! { $(
#[derive(Debug)] #[derive(Debug)]
pub struct [< $t Cord>]; pub struct [< $t Cord>](Private);
#[derive(Debug)] #[derive(Debug)]
pub enum [< $t Cow>]<'a> { pub enum [< $t Cow>]<'a> {
@ -30,6 +30,13 @@ macro_rules! impl_cord_types {
type View<'msg> = [< $t Cow>]<'msg>; 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> Proxy<'msg> for [< $t Cow>]<'msg> {}
impl<'msg> ViewProxy<'msg> for [< $t Cow>]<'msg> {} impl<'msg> ViewProxy<'msg> for [< $t Cow>]<'msg> {}

@ -21,4 +21,5 @@ pub use crate::ProtoStr;
pub use crate::__runtime::{PtrAndLen, RawMap, RawMessage, RawRepeatedField}; pub use crate::__runtime::{PtrAndLen, RawMap, RawMessage, RawRepeatedField};
/// Used to protect internal-only items from being used accidentally. /// Used to protect internal-only items from being used accidentally.
#[derive(Debug)]
pub struct Private; pub struct Private;

@ -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; 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> { impl<K: Proxied, V: ProxiedInMapValue<K>> 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;
} }
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>> Proxy<'msg> for MapView<'msg, K, V> {}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> AsView for MapView<'msg, K, V> { impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> AsView for MapView<'msg, K, V> {

@ -52,7 +52,7 @@ use std::fmt::Debug;
/// An instance of a `Proxied` can be accessed immutably via `Proxied::View`. /// An instance of a `Proxied` can be accessed immutably via `Proxied::View`.
/// ///
/// All Protobuf field types implement `Proxied`. /// 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`. /// 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`].
@ -67,7 +67,7 @@ pub trait Proxied: Sized {
/// and immutably via `MutProxied::View`. /// and immutably via `MutProxied::View`.
/// ///
/// `MutProxied` is implemented by message, map and repeated field types. /// `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 /// The proxy type that provides exclusive mutable access to a `T`, like a
/// `&'msg mut T`. /// `&'msg mut T`.
/// ///
@ -263,10 +263,24 @@ mod tests {
type View<'msg> = MyProxiedView<'msg>; type View<'msg> = MyProxiedView<'msg>;
} }
impl AsView for MyProxied {
type Proxied = Self;
fn as_view(&self) -> MyProxiedView<'_> {
self.as_view()
}
}
impl MutProxied for MyProxied { impl MutProxied for MyProxied {
type Mut<'msg> = MyProxiedMut<'msg>; 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)] #[derive(Debug, Clone, Copy)]
struct MyProxiedView<'msg> { struct MyProxiedView<'msg> {
my_proxied_ref: &'msg MyProxied, my_proxied_ref: &'msg MyProxied,

@ -391,6 +391,17 @@ where
type View<'msg> = RepeatedView<'msg, T> where Repeated<T>: 'msg; 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> impl<T> MutProxied for Repeated<T>
where where
T: ProxiedInRepeated, T: ProxiedInRepeated,
@ -398,6 +409,17 @@ where
type Mut<'msg> = RepeatedMut<'msg, T> where Repeated<T>: 'msg; 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> Proxy<'msg> for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg {}
impl<'msg, T> AsView for RepeatedView<'msg, T> impl<'msg, T> AsView for RepeatedView<'msg, T>

@ -72,6 +72,14 @@ impl Proxied for ProtoBytes {
type View<'msg> = &'msg [u8]; 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 { impl IntoProxied<ProtoBytes> for ProtoBytes {
fn into_proxied(self, _private: Private) -> ProtoBytes { fn into_proxied(self, _private: Private) -> ProtoBytes {
self self
@ -512,6 +520,14 @@ impl Proxied for ProtoString {
type View<'msg> = &'msg ProtoStr; 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<'msg> Proxy<'msg> for &'msg ProtoStr {}
impl AsView for &ProtoStr { impl AsView for &ProtoStr {

Loading…
Cancel
Save