use crate::__internal::{Private, SealedInternal};
pub trait ProtoObject: SealedInternal + AsView<Proxied = Self> + Sized + 'static {
type View<'msg>: AsView<Proxied = Self> + IntoView<'msg>;
}
#[deprecated(note = "Use `ProtoObject` instead.")]
pub use self::ProtoObject as Proxied;
pub trait MutProtoObject:
SealedInternal + ProtoObject + AsMut<MutProxied = Self> + 'static
{
type Mut<'msg>: AsMut<MutProxied = Self> + IntoMut<'msg> + IntoView<'msg>;
}
#[deprecated(note = "Use `MutProtoObject` instead.")]
pub use self::MutProtoObject as MutProxied;
#[allow(dead_code)]
pub type View<'msg, T> = <T as ProtoObject>::View<'msg>;
#[allow(dead_code)]
pub type Mut<'msg, T> = <T as MutProtoObject>::Mut<'msg>;
#[diagnostic::on_unimplemented(
message = "the trait `AsView` is not implemented for `{Self}`",
note = "consider calling `.as_view()`, or changing the fn to accept \
`impl AsView<Proxied = T>`"
)]
pub trait AsView: SealedInternal {
type Proxied: ProtoObject;
fn as_view(&self) -> View<'_, Self::Proxied>;
}
impl<T: ProtoObject> AsView for &T {
type Proxied = T::Proxied;
fn as_view(&self) -> View<'_, Self::Proxied> {
(**self).as_view()
}
}
impl<T: ProtoObject> AsView for &mut T {
type Proxied = T::Proxied;
fn as_view(&self) -> View<'_, Self::Proxied> {
(**self).as_view()
}
}
#[diagnostic::on_unimplemented(
message = "the trait `IntoView` is not implemented for `{Self}` (consider calling `.as_view()` \
or borrowing)",
note = "consider calling `.as_view()` or `.into_view()`"
)]
pub trait IntoView<'msg>: SealedInternal + AsView {
fn into_view<'shorter>(self) -> View<'shorter, Self::Proxied>
where
'msg: 'shorter;
}
impl<'msg, T: ProtoObject> IntoView<'msg> for &'msg T {
fn into_view<'shorter>(self) -> View<'shorter, T>
where
'msg: 'shorter,
{
(*self).as_view()
}
}
impl<'msg, T: ProtoObject> IntoView<'msg> for &'msg mut T {
fn into_view<'shorter>(self) -> View<'shorter, T>
where
'msg: 'shorter,
{
(*self).as_view()
}
}
#[diagnostic::on_unimplemented(
message = "the trait `AsMut` is not implemented for `{Self}`",
note = "consider calling `.as_mut()`, or changing the fn to accept \
`impl AsMut<MutProxied = T>`"
)]
pub trait AsMut: SealedInternal + AsView<Proxied = Self::MutProxied> {
type MutProxied: MutProtoObject;
fn as_mut(&mut self) -> Mut<'_, Self::MutProxied>;
}
impl<T: MutProtoObject> AsMut for &mut T {
type MutProxied = T::MutProxied;
fn as_mut(&mut self) -> Mut<'_, Self::MutProxied> {
(*self).as_mut()
}
}
#[diagnostic::on_unimplemented(
message = "the trait `IntoMut` is not implemented for `{Self}` (consider calling `.as_mut()` \
or mutably borrowing)",
note = "consider calling `.as_mut()` or `.into_mut()`"
)]
pub trait IntoMut<'msg>: SealedInternal + AsMut {
fn into_mut<'shorter>(self) -> Mut<'shorter, Self::MutProxied>
where
'msg: 'shorter;
}
impl<'msg, T: MutProtoObject> IntoMut<'msg> for &'msg mut T {
fn into_mut<'shorter>(self) -> Mut<'shorter, T>
where
'msg: 'shorter,
{
(*self).as_mut()
}
}
pub trait IntoProxied<T: ProtoObject> {
#[doc(hidden)]
fn into_proxied(self, _private: Private) -> T;
}
impl<T: ProtoObject> IntoProxied<T> for T {
fn into_proxied(self, _private: Private) -> T {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use googletest::prelude::*;
#[derive(Debug, Default, PartialEq)]
struct MyProxied {
val: String,
}
impl MyProxied {
fn as_view(&self) -> View<'_, Self> {
MyProxiedView { my_proxied_ref: self }
}
fn as_mut(&mut self) -> Mut<'_, Self> {
MyProxiedMut { my_proxied_ref: self }
}
}
impl SealedInternal for MyProxied {}
impl ProtoObject for MyProxied {
type View<'msg> = MyProxiedView<'msg>;
}
#[gtest]
#[allow(deprecated)]
fn test_deprecated_proxied_alias() {
fn check_proxied<T: Proxied>() {}
check_proxied::<MyProxied>();
fn check_mut_proxied<T: MutProxied>() {}
check_mut_proxied::<MyProxied>();
}
impl AsView for MyProxied {
type Proxied = Self;
fn as_view(&self) -> MyProxiedView<'_> {
self.as_view()
}
}
impl MutProtoObject 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,
}
impl<'msg> SealedInternal for MyProxiedView<'msg> {}
impl MyProxiedView<'_> {
fn val(&self) -> &str {
&self.my_proxied_ref.val
}
}
impl<'msg> AsView for MyProxiedView<'msg> {
type Proxied = MyProxied;
fn as_view(&self) -> MyProxiedView<'msg> {
*self
}
}
impl<'msg> IntoView<'msg> for MyProxiedView<'msg> {
fn into_view<'shorter>(self) -> MyProxiedView<'shorter>
where
'msg: 'shorter,
{
self
}
}
#[derive(Debug)]
struct MyProxiedMut<'msg> {
my_proxied_ref: &'msg mut MyProxied,
}
impl<'msg> SealedInternal for MyProxiedMut<'msg> {}
impl<'msg> AsView for MyProxiedMut<'msg> {
type Proxied = MyProxied;
fn as_view(&self) -> MyProxiedView<'_> {
MyProxiedView { my_proxied_ref: self.my_proxied_ref }
}
}
impl<'msg> IntoView<'msg> for MyProxiedMut<'msg> {
fn into_view<'shorter>(self) -> View<'shorter, MyProxied>
where
'msg: 'shorter,
{
MyProxiedView { my_proxied_ref: self.my_proxied_ref }
}
}
impl<'msg> AsMut for MyProxiedMut<'msg> {
type MutProxied = MyProxied;
fn as_mut(&mut self) -> MyProxiedMut<'_> {
MyProxiedMut { my_proxied_ref: self.my_proxied_ref }
}
}
impl<'msg> IntoMut<'msg> for MyProxiedMut<'msg> {
fn into_mut<'shorter>(self) -> MyProxiedMut<'shorter>
where
'msg: 'shorter,
{
self
}
}
#[gtest]
fn test_as_view() {
let my_proxied = MyProxied { val: "Hello World".to_string() };
let my_view = my_proxied.as_view();
assert_that!(my_view.val(), eq(&my_proxied.val));
}
fn reborrow_mut_into_view<'msg>(x: Mut<'msg, MyProxied>) -> View<'msg, MyProxied> {
x.into_view() }
#[gtest]
fn test_mut_into_view() {
let mut my_proxied = MyProxied { val: "Hello World".to_string() };
reborrow_mut_into_view(my_proxied.as_mut());
}
fn require_unified_lifetimes<'msg>(_x: Mut<'msg, MyProxied>, _y: View<'msg, MyProxied>) {}
#[gtest]
fn test_require_unified_lifetimes() {
let mut my_proxied = MyProxied { val: "Hello1".to_string() };
let my_mut = my_proxied.as_mut();
{
let other_proxied = MyProxied { val: "Hello2".to_string() };
let other_view = other_proxied.as_view();
require_unified_lifetimes(my_mut, other_view);
}
}
fn reborrow_generic_as_view<'a, 'b, T>(
x: &'b mut Mut<'a, T>,
y: &'b View<'a, T>,
) -> [View<'b, T>; 2]
where
T: MutProtoObject,
'a: 'b,
{
[x.as_view(), y.as_view()]
}
#[gtest]
fn test_reborrow_generic_as_view() {
let mut my_proxied = MyProxied { val: "Hello1".to_string() };
let mut my_mut = my_proxied.as_mut();
let my_ref = &mut my_mut;
{
let other_proxied = MyProxied { val: "Hello2".to_string() };
let other_view = other_proxied.as_view();
reborrow_generic_as_view::<MyProxied>(my_ref, &other_view);
}
}
fn reborrow_generic_view_into_view<'a, 'b, T>(
x: View<'a, T>,
y: View<'b, T>,
) -> [View<'b, T>; 2]
where
T: ProtoObject,
'a: 'b,
{
[x.into_view(), y]
}
#[gtest]
fn test_reborrow_generic_into_view() {
let my_proxied = MyProxied { val: "Hello1".to_string() };
let my_view = my_proxied.as_view();
{
let other_proxied = MyProxied { val: "Hello2".to_string() };
let other_view = other_proxied.as_view();
reborrow_generic_view_into_view::<MyProxied>(my_view, other_view);
}
}
fn reborrow_generic_mut_into_view<'a, 'b, T>(x: Mut<'a, T>, y: View<'b, T>) -> [View<'b, T>; 2]
where
T: MutProtoObject,
'a: 'b,
{
[x.into_view(), y]
}
#[gtest]
fn test_reborrow_generic_mut_into_view() {
let mut my_proxied = MyProxied { val: "Hello1".to_string() };
let my_mut = my_proxied.as_mut();
{
let other_proxied = MyProxied { val: "Hello2".to_string() };
let other_view = other_proxied.as_view();
reborrow_generic_mut_into_view::<MyProxied>(my_mut, other_view);
}
}
fn reborrow_generic_mut_into_mut<'a, 'b, T>(x: Mut<'a, T>, y: Mut<'b, T>) -> [Mut<'b, T>; 2]
where
T: MutProtoObject,
'a: 'b,
{
let tmp: Mut<'b, T> = x.into_mut();
[tmp, y]
}
#[gtest]
fn test_reborrow_generic_mut_into_mut() {
let mut my_proxied = MyProxied { val: "Hello1".to_string() };
let my_mut = my_proxied.as_mut();
{
let mut other_proxied = MyProxied { val: "Hello2".to_string() };
let other_mut = other_proxied.as_mut();
reborrow_generic_mut_into_mut::<MyProxied>(my_mut, other_mut);
}
}
fn accepts_as_view(_: impl AsView<Proxied = MyProxied>) {}
fn accepts_as_mut(_: impl AsMut<MutProxied = MyProxied>) {}
fn accepts_into_view<'a>(_: impl IntoView<'a, Proxied = MyProxied>) {}
fn accepts_into_mut<'a>(_: impl IntoMut<'a, MutProxied = MyProxied>) {}
#[gtest]
fn test_suggested_conversions_satisfy_proxy_bounds() {
let mut my_proxied = MyProxied { val: "Hello".to_string() };
accepts_as_view(&my_proxied);
accepts_as_mut(&mut my_proxied);
accepts_as_view(my_proxied.as_view());
accepts_as_mut(my_proxied.as_mut());
let mut my_mut = my_proxied.as_mut();
accepts_as_view(my_mut.as_view());
accepts_as_mut(my_mut.as_mut());
accepts_into_view(&my_proxied);
accepts_into_mut(&mut my_proxied);
accepts_into_view(my_proxied.as_view());
accepts_into_mut(my_proxied.as_mut());
}
}