Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions rust/codegen_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

//! Traits that are implemented by codegen types.

use crate::MutProxied;
use crate::__internal::SealedInternal;
use crate::{MutProxied, MutProxy, ViewProxy};
use create::Parse;
use interop::{MessageMutInterop, MessageViewInterop, OwnedMessageInterop};
use read::Serialize;
Expand All @@ -35,7 +35,6 @@ pub trait Message: SealedInternal

/// A trait that all generated message views implement.
pub trait MessageView<'msg>: SealedInternal
+ ViewProxy<'msg, Proxied = Self::Message>
// Read traits:
+ Debug + Serialize + Default
// Thread safety:
Expand All @@ -51,7 +50,6 @@ pub trait MessageView<'msg>: SealedInternal

/// A trait that all generated message muts implement.
pub trait MessageMut<'msg>: SealedInternal
+ MutProxy<'msg, MutProxied = Self::Message>
// Read traits:
+ Debug + Serialize
// Write traits:
Expand Down
9 changes: 1 addition & 8 deletions rust/cord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
// https://developers.google.com/open-source/licenses/bsd

use crate::__internal::{Private, SealedInternal};
use crate::{
AsView, IntoProxied, IntoView, ProtoBytes, ProtoStr, ProtoString, Proxied, Proxy, View,
ViewProxy,
};
use crate::{AsView, IntoProxied, IntoView, ProtoBytes, ProtoStr, ProtoString, Proxied, View};
use paste::paste;
use std::cmp::PartialEq;
use std::ops::Deref;
Expand Down Expand Up @@ -41,10 +38,6 @@ macro_rules! impl_cord_types {
}
}

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

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

impl<'msg> AsView for [< $t Cow>]<'msg> {
type Proxied = [< $t Cord>];

Expand Down
11 changes: 1 addition & 10 deletions rust/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
// https://developers.google.com/open-source/licenses/bsd

use crate::{
AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, MutProxy, Proxied,
Proxy, View, ViewProxy,
AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, Proxied, View,
__internal::runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter},
__internal::{Private, SealedInternal},
};
Expand Down Expand Up @@ -141,8 +140,6 @@ impl<K: Proxied, V: ProxiedInMapValue<K>> AsMut for Map<K, V> {

impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> SealedInternal 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> {
type Proxied = Map<K, V>;

Expand All @@ -160,12 +157,8 @@ impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> IntoView<'msg> for MapView<'msg,
}
}

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

impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> SealedInternal for MapMut<'msg, K, V> {}

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

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

Expand Down Expand Up @@ -200,8 +193,6 @@ impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> IntoMut<'msg> for MapMut<'msg, K
}
}

impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> MutProxy<'msg> for MapMut<'msg, K, V> {}

impl<K, V> Map<K, V>
where
K: Proxied,
Expand Down
14 changes: 11 additions & 3 deletions rust/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#![allow(unused)]

use crate::__internal::Private;
use crate::{Mut, MutProxied, MutProxy, Proxied, View, ViewProxy};
use crate::{Mut, MutProxied, Proxied, View};
use std::convert::{AsMut, AsRef};
use std::fmt::{self, Debug};
use std::panic;
Expand Down Expand Up @@ -51,12 +51,20 @@ impl<T> Optional<T> {

/// Constructs an `Optional<T>` with a `T` value and presence bit.
pub fn new(val: T, is_set: bool) -> Self {
if is_set { Optional::Set(val) } else { Optional::Unset(val) }
if is_set {
Optional::Set(val)
} else {
Optional::Unset(val)
}
}

/// Converts into an `Option` of the set value, ignoring any unset value.
pub fn into_option(self) -> Option<T> {
if let Optional::Set(x) = self { Some(x) } else { None }
if let Optional::Set(x) = self {
Some(x)
} else {
None
}
}

/// Returns if the field is set.
Expand Down
7 changes: 1 addition & 6 deletions rust/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
use crate::__internal::SealedInternal;
use crate::{AsView, IntoView, Proxied, Proxy, ViewProxy};
use crate::{AsView, IntoView, Proxied};

macro_rules! impl_singular_primitives {
($($t:ty),*) => {
Expand All @@ -16,9 +16,6 @@ macro_rules! impl_singular_primitives {
type View<'msg> = $t;
}

impl<'msg> Proxy<'msg> for $t {
}

impl AsView for $t {
type Proxied = $t;

Expand All @@ -35,8 +32,6 @@ macro_rules! impl_singular_primitives {
}
}

impl<'msg> ViewProxy<'msg> for $t {}

// ProxiedInRepeated is implemented in {cpp,upb}.rs
)*
}
Expand Down
67 changes: 13 additions & 54 deletions rust/proxied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,17 @@
//! indirection between the user and the internal memory representation.

use crate::__internal::{Private, SealedInternal};
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`.
///
/// All Protobuf field types implement `Proxied`.
pub trait Proxied: SealedInternal + AsView<Proxied = Self> + Sized {
pub trait Proxied: SealedInternal + AsView<Proxied = Self> + Sized + 'static {
/// 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>
where
Self: 'msg;
type View<'msg>: AsView<Proxied = Self> + IntoView<'msg>;
}

/// A type that can be be accessed through a reference-like proxy.
Expand All @@ -67,14 +64,12 @@ pub trait Proxied: SealedInternal + AsView<Proxied = Self> + Sized {
/// and immutably via `MutProxied::View`.
///
/// `MutProxied` is implemented by message, map and repeated field types.
pub trait MutProxied: SealedInternal + Proxied + AsMut<MutProxied = Self> {
pub trait MutProxied: SealedInternal + Proxied + AsMut<MutProxied = Self> + 'static {
/// The proxy type that provides exclusive mutable access to a `T`, like a
/// `&'msg mut T`.
///
/// Most code should use the type alias [`Mut`].
type Mut<'msg>: MutProxy<'msg, MutProxied = Self>
where
Self: 'msg;
type Mut<'msg>: AsMut<MutProxied = Self> + IntoMut<'msg> + IntoView<'msg>;
}

/// A proxy type that provides shared access to a `T`, like a `&'msg T`.
Expand All @@ -91,10 +86,10 @@ pub type View<'msg, T> = <T as Proxied>::View<'msg>;
pub type Mut<'msg, T> = <T as MutProxied>::Mut<'msg>;

/// Used to semantically do a cheap "to-reference" conversion. This is
/// implemented on both owned `Proxied` types as well as ViewProxy and MutProxy
/// implemented on both owned `Proxied` types as well as view and mut proxy
/// types.
///
/// On ViewProxy this will behave as a reborrow into a shorter lifetime.
/// On a view proxy this will behave as a reborrow into a shorter lifetime.
pub trait AsView: SealedInternal {
type Proxied: Proxied;

Expand All @@ -120,12 +115,12 @@ pub trait AsView: SealedInternal {
fn as_view(&self) -> View<'_, Self::Proxied>;
}

/// Used to turn another 'borrow' into a ViewProxy.
/// Used to turn another 'borrow' into a view proxy.
///
/// On a MutProxy this borrows to a View (semantically matching turning a `&mut
/// On a mut proxy this borrows to a View (semantically matching turning a `&mut
/// T` into a `&T`).
///
/// On a ViewProxy this will behave as a reborrow into a shorter lifetime
/// On a view proxy this will behave as a reborrow into a shorter lifetime
/// (semantically matching a `&'a T` into a `&'b T` where `'a: 'b`).
pub trait IntoView<'msg>: SealedInternal + AsView {
/// Converts into a `View` with a potentially shorter lifetime.
Expand Down Expand Up @@ -159,19 +154,19 @@ pub trait IntoView<'msg>: SealedInternal + AsView {
}

/// Used to semantically do a cheap "to-mut-reference" conversion. This is
/// implemented on both owned `Proxied` types as well as MutProxy types.
/// implemented on both owned `Proxied` types as well as mut proxy types.
///
/// On MutProxy this will behave as a reborrow into a shorter lifetime.
/// On a mut proxy this will behave as a reborrow into a shorter lifetime.
pub trait AsMut: SealedInternal + AsView<Proxied = Self::MutProxied> {
type MutProxied: MutProxied;

/// Converts a borrow into a `Mut` with the lifetime of that borrow.
fn as_mut(&mut self) -> Mut<'_, Self::MutProxied>;
}

/// Used to turn another 'borrow' into a MutProxy.
/// Used to turn another 'borrow' into a mut proxy.
///
/// On a MutProxy this will behave as a reborrow into a shorter lifetime
/// On a mut proxy this will behave as a reborrow into a shorter lifetime
/// (semantically matching a `&mut 'a T` into a `&mut 'b T` where `'a: 'b`).
pub trait IntoMut<'msg>: SealedInternal + AsMut {
/// Converts into a `Mut` with a potentially shorter lifetime.
Expand Down Expand Up @@ -201,34 +196,6 @@ pub trait IntoMut<'msg>: SealedInternal + AsMut {
'msg: 'shorter;
}

/// Declares conversion operations common to all proxies (both views and mut
/// proxies).
///
/// This trait is intentionally made non-object-safe to prevent a potential
/// future incompatible change.
pub trait Proxy<'msg>:
SealedInternal + 'msg + IntoView<'msg> + Sync + Unpin + Sized + Debug
{
}

/// Declares conversion operations common to view proxies.
pub trait ViewProxy<'msg>: SealedInternal + Proxy<'msg> + Send {}

/// Declares operations common to all mut proxies.
///
/// This trait is intentionally made non-object-safe to prevent a potential
/// future incompatible change.
pub trait MutProxy<'msg>: SealedInternal + Proxy<'msg> + AsMut + IntoMut<'msg> {
/// 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
/// multiple times - if the result of `get` is not living long enough
/// for your use, use that instead.
fn get(&self) -> View<'_, Self::Proxied> {
self.as_view()
}
}

/// A value to `Proxied`-value conversion that consumes the input value.
///
/// All setter functions accept types that implement `IntoProxied`. The purpose
Expand Down Expand Up @@ -306,10 +273,6 @@ mod tests {
}
}

impl<'msg> Proxy<'msg> for MyProxiedView<'msg> {}

impl<'msg> ViewProxy<'msg> for MyProxiedView<'msg> {}

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

Expand All @@ -334,8 +297,6 @@ mod tests {

impl<'msg> SealedInternal for MyProxiedMut<'msg> {}

impl<'msg> Proxy<'msg> for MyProxiedMut<'msg> {}

impl<'msg> AsView for MyProxiedMut<'msg> {
type Proxied = MyProxied;

Expand Down Expand Up @@ -370,8 +331,6 @@ mod tests {
}
}

impl<'msg> MutProxy<'msg> for MyProxiedMut<'msg> {}

#[gtest]
fn test_as_view() {
let my_proxied = MyProxied { val: "Hello World".to_string() };
Expand Down
11 changes: 1 addition & 10 deletions rust/repeated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use std::iter::FusedIterator;
use std::marker::PhantomData;

use crate::{
AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, MutProxy, Proxied,
Proxy, View, ViewProxy,
AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, Proxied, View,
__internal::runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField},
__internal::{Private, SealedInternal},
};
Expand Down Expand Up @@ -460,8 +459,6 @@ where

impl<'msg, T> SealedInternal 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>
where
T: ProxiedInRepeated + 'msg,
Expand All @@ -487,12 +484,8 @@ where
}
}

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

impl<'msg, T> SealedInternal for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg {}

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

impl<'msg, T> AsView for RepeatedMut<'msg, T>
where
T: ProxiedInRepeated + 'msg,
Expand Down Expand Up @@ -543,8 +536,6 @@ where
}
}

impl<'msg, T> MutProxy<'msg> for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg {}

impl<'msg, T> iter::Iterator for RepeatedIter<'msg, T>
where
T: ProxiedInRepeated + 'msg,
Expand Down
3 changes: 1 addition & 2 deletions rust/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ pub use crate::cord::{ProtoBytesCow, ProtoStringCow};
pub use crate::map::{Map, MapIter, MapMut, MapView, ProxiedInMapValue};
pub use crate::optional::Optional;
pub use crate::proxied::{
AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Proxied, Proxy, View,
ViewProxy,
AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProxied, Proxied, View,
};
pub use crate::r#enum::{Enum, UnknownEnumValue};
pub use crate::repeated::{ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView};
Expand Down
Loading
Loading