From 1887e9edfbb583782b06cabbf7c790dec0888138 Mon Sep 17 00:00:00 2001 From: asonix Date: Tue, 6 Dec 2022 17:47:16 -0600 Subject: [PATCH] Use associated types for Ap traits --- activitystreams-ext/src/ext1.rs | 20 ++++---- activitystreams-ext/src/ext2.rs | 20 ++++---- activitystreams-ext/src/ext3.rs | 20 ++++---- activitystreams-ext/src/ext4.rs | 20 ++++---- src/actor.rs | 84 ++++++++++++++++++--------------- src/object.rs | 28 ++++++----- src/unparsed.rs | 10 ++-- 7 files changed, 116 insertions(+), 86 deletions(-) diff --git a/activitystreams-ext/src/ext1.rs b/activitystreams-ext/src/ext1.rs index 79ae5ff..76862cc 100644 --- a/activitystreams-ext/src/ext1.rs +++ b/activitystreams-ext/src/ext1.rs @@ -53,15 +53,17 @@ where } } -impl AsApObject for Ext1 +impl AsApObject for Ext1 where - Inner: AsApObject, + Inner: AsApObject, { - fn ap_object_ref(&self) -> &ApObject { + type Inner = Inner::Inner; + + fn ap_object_ref(&self) -> &ApObject { self.inner.ap_object_ref() } - fn ap_object_mut(&mut self) -> &mut ApObject { + fn ap_object_mut(&mut self) -> &mut ApObject { self.inner.ap_object_mut() } } @@ -96,15 +98,17 @@ where } } -impl AsApActor for Ext1 +impl AsApActor for Ext1 where - Inner: AsApActor, + Inner: AsApActor, { - fn ap_actor_ref(&self) -> &ApActor { + type Inner = Inner::Inner; + + fn ap_actor_ref(&self) -> &ApActor { self.inner.ap_actor_ref() } - fn ap_actor_mut(&mut self) -> &mut ApActor { + fn ap_actor_mut(&mut self) -> &mut ApActor { self.inner.ap_actor_mut() } } diff --git a/activitystreams-ext/src/ext2.rs b/activitystreams-ext/src/ext2.rs index 16abb2b..5dcf650 100644 --- a/activitystreams-ext/src/ext2.rs +++ b/activitystreams-ext/src/ext2.rs @@ -53,15 +53,17 @@ where } } -impl AsApObject for Ext2 +impl AsApObject for Ext2 where - Inner: AsApObject, + Inner: AsApObject, { - fn ap_object_ref(&self) -> &ApObject { + type Inner = Inner::Inner; + + fn ap_object_ref(&self) -> &ApObject { self.inner.ap_object_ref() } - fn ap_object_mut(&mut self) -> &mut ApObject { + fn ap_object_mut(&mut self) -> &mut ApObject { self.inner.ap_object_mut() } } @@ -96,15 +98,17 @@ where } } -impl AsApActor for Ext2 +impl AsApActor for Ext2 where - Inner: AsApActor, + Inner: AsApActor, { - fn ap_actor_ref(&self) -> &ApActor { + type Inner = Inner::Inner; + + fn ap_actor_ref(&self) -> &ApActor { self.inner.ap_actor_ref() } - fn ap_actor_mut(&mut self) -> &mut ApActor { + fn ap_actor_mut(&mut self) -> &mut ApActor { self.inner.ap_actor_mut() } } diff --git a/activitystreams-ext/src/ext3.rs b/activitystreams-ext/src/ext3.rs index dbcfd47..72757cc 100644 --- a/activitystreams-ext/src/ext3.rs +++ b/activitystreams-ext/src/ext3.rs @@ -56,15 +56,17 @@ where } } -impl AsApObject for Ext3 +impl AsApObject for Ext3 where - Inner: AsApObject, + Inner: AsApObject, { - fn ap_object_ref(&self) -> &ApObject { + type Inner = Inner::Inner; + + fn ap_object_ref(&self) -> &ApObject { self.inner.ap_object_ref() } - fn ap_object_mut(&mut self) -> &mut ApObject { + fn ap_object_mut(&mut self) -> &mut ApObject { self.inner.ap_object_mut() } } @@ -99,15 +101,17 @@ where } } -impl AsApActor for Ext3 +impl AsApActor for Ext3 where - Inner: AsApActor, + Inner: AsApActor, { - fn ap_actor_ref(&self) -> &ApActor { + type Inner = Inner::Inner; + + fn ap_actor_ref(&self) -> &ApActor { self.inner.ap_actor_ref() } - fn ap_actor_mut(&mut self) -> &mut ApActor { + fn ap_actor_mut(&mut self) -> &mut ApActor { self.inner.ap_actor_mut() } } diff --git a/activitystreams-ext/src/ext4.rs b/activitystreams-ext/src/ext4.rs index 62fdb36..2c601d7 100644 --- a/activitystreams-ext/src/ext4.rs +++ b/activitystreams-ext/src/ext4.rs @@ -59,15 +59,17 @@ where } } -impl AsApObject for Ext4 +impl AsApObject for Ext4 where - Inner: AsApObject, + Inner: AsApObject, { - fn ap_object_ref(&self) -> &ApObject { + type Inner = Inner::Inner; + + fn ap_object_ref(&self) -> &ApObject { self.inner.ap_object_ref() } - fn ap_object_mut(&mut self) -> &mut ApObject { + fn ap_object_mut(&mut self) -> &mut ApObject { self.inner.ap_object_mut() } } @@ -102,15 +104,17 @@ where } } -impl AsApActor for Ext4 +impl AsApActor for Ext4 where - Inner: AsApActor, + Inner: AsApActor, { - fn ap_actor_ref(&self) -> &ApActor { + type Inner = Inner::Inner; + + fn ap_actor_ref(&self) -> &ApActor { self.inner.ap_actor_ref() } - fn ap_actor_mut(&mut self) -> &mut ApActor { + fn ap_actor_mut(&mut self) -> &mut ApActor { self.inner.ap_actor_mut() } } diff --git a/src/actor.rs b/src/actor.rs index 3ab5382..2d11c36 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -39,12 +39,14 @@ use self::kind::*; /// Implementation trait for deriving ActivityPub Actor methods for a type /// /// Any type implementing AsObject will automatically gain methods provided by ApActorExt -pub trait AsApActor: markers::Actor { +pub trait AsApActor: markers::Actor { + type Inner; + /// Immutable borrow of `ApActor` - fn ap_actor_ref(&self) -> &ApActor; + fn ap_actor_ref(&self) -> &ApActor; /// Mutable borrow of `ApActor` - fn ap_actor_mut(&mut self) -> &mut ApActor; + fn ap_actor_mut(&mut self) -> &mut ApActor; } /// Helper methods for interacting with ActivityPub Actor types @@ -52,7 +54,7 @@ pub trait AsApActor: markers::Actor { /// This trait represents methods valid for any ActivityPub Actor. /// /// Documentation for the fields related to these methods can be found on the `ApActor` struct -pub trait ApActorExt: AsApActor { +pub trait ApActorExt: AsApActor { /// Fetch the inbox for the current actor /// /// ```rust @@ -64,7 +66,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn inbox<'a>(&'a self) -> Result<&'a IriString, CheckError> where - Inner: 'a, + Self::Inner: 'a, Self: BaseExt, { let inbox = self.inbox_unchecked(); @@ -82,7 +84,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn inbox_unchecked<'a>(&'a self) -> &'a IriString where - Inner: 'a, + Self::Inner: 'a, { &self.ap_actor_ref().inbox } @@ -98,7 +100,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn inbox_mut<'a>(&'a mut self) -> &'a mut IriString where - Inner: 'a, + Self::Inner: 'a, { &mut self.ap_actor_mut().inbox } @@ -131,7 +133,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn outbox<'a>(&'a self) -> Result, CheckError> where - Inner: 'a, + Self::Inner: 'a, Self: BaseExt, { self.outbox_unchecked() @@ -150,7 +152,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn outbox_unchecked<'a>(&'a self) -> Option<&'a IriString> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_ref().outbox.as_ref() } @@ -168,7 +170,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn outbox_mut<'a>(&'a mut self) -> Option<&'a mut IriString> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_mut().outbox.as_mut() } @@ -238,7 +240,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn following<'a>(&'a self) -> Result, CheckError> where - Inner: 'a, + Self::Inner: 'a, Self: BaseExt, { self.following_unchecked() @@ -259,7 +261,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn following_unchecked<'a>(&'a self) -> Option<&'a IriString> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_ref().following.as_ref() } @@ -277,7 +279,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn following_mut<'a>(&'a mut self) -> Option<&'a mut IriString> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_mut().following.as_mut() } @@ -347,7 +349,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn followers<'a>(&'a self) -> Result, CheckError> where - Inner: 'a, + Self::Inner: 'a, Self: BaseExt, { self.followers_unchecked() @@ -368,7 +370,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn followers_unchecked<'a>(&'a self) -> Option<&'a IriString> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_ref().followers.as_ref() } @@ -386,7 +388,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn followers_mut<'a>(&'a mut self) -> Option<&'a mut IriString> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_mut().followers.as_mut() } @@ -456,7 +458,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn liked<'a>(&'a self) -> Result, CheckError> where - Inner: 'a, + Self::Inner: 'a, Self: BaseExt, { self.liked_unchecked() @@ -477,7 +479,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn liked_unchecked<'a>(&'a self) -> Option<&'a IriString> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_ref().liked.as_ref() } @@ -495,7 +497,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn liked_mut<'a>(&'a mut self) -> Option<&'a mut IriString> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_mut().liked.as_mut() } @@ -565,7 +567,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn streams<'a>(&'a self) -> Result>, CheckError> where - Inner: 'a, + Self::Inner: 'a, Self: BaseExt, { if let Some(streams) = self.streams_unchecked() { @@ -592,7 +594,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn streams_unchecked<'a>(&'a self) -> Option> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_ref().streams.as_ref().map(|o| o.as_ref()) } @@ -613,7 +615,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn streams_mut<'a>(&'a mut self) -> Option> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_mut().streams.as_mut().map(|o| o.as_mut()) } @@ -734,7 +736,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn preferred_username<'a>(&'a self) -> Option<&'a str> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_ref().preferred_username.as_deref() } @@ -813,7 +815,7 @@ pub trait ApActorExt: AsApActor { fn endpoints<'a>(&'a self) -> Result>, CheckError> where Self: BaseExt, - Inner: 'a, + Self::Inner: 'a, { if let Some(endpoints) = self.endpoints_unchecked() { let authority_opt = self.id_unchecked().and_then(|id| id.authority_components()); @@ -868,7 +870,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn endpoints_unchecked<'a>(&'a self) -> Option> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_ref().endpoints.as_ref().map(|e| e.as_ref()) } @@ -889,7 +891,7 @@ pub trait ApActorExt: AsApActor { /// ``` fn endpoints_mut<'a>(&'a mut self) -> Option> where - Inner: 'a, + Self::Inner: 'a, { self.ap_actor_mut().endpoints.as_mut().map(|e| e.as_mut()) } @@ -1473,28 +1475,32 @@ where } } -impl AsApActor for ApActor +impl AsApActor for ApActor where Inner: markers::Actor, { - fn ap_actor_ref(&self) -> &ApActor { + type Inner = Inner; + + fn ap_actor_ref(&self) -> &ApActor { self } - fn ap_actor_mut(&mut self) -> &mut ApActor { + fn ap_actor_mut(&mut self) -> &mut ApActor { self } } -impl AsApObject for ApActor +impl AsApObject for ApActor where - Inner1: AsApObject, + Inner: AsApObject, { - fn ap_object_ref(&self) -> &ApObject { + type Inner = Inner::Inner; + + fn ap_object_ref(&self) -> &ApObject { self.inner.ap_object_ref() } - fn ap_object_mut(&mut self) -> &mut ApObject { + fn ap_object_mut(&mut self) -> &mut ApObject { self.inner.ap_object_mut() } } @@ -1543,17 +1549,19 @@ impl AsObject for Actor { } } -impl ApActorExt for T where T: AsApActor {} +impl ApActorExt for T where T: AsApActor {} -impl AsApActor for ApObject +impl AsApActor for ApObject where - Inner1: AsApActor, + Inner: AsApActor, { - fn ap_actor_ref(&self) -> &ApActor { + type Inner = Inner::Inner; + + fn ap_actor_ref(&self) -> &ApActor { self.inner().ap_actor_ref() } - fn ap_actor_mut(&mut self) -> &mut ApActor { + fn ap_actor_mut(&mut self) -> &mut ApActor { self.inner_mut().ap_actor_mut() } } diff --git a/src/object.rs b/src/object.rs index 97cd53f..3a77f7a 100644 --- a/src/object.rs +++ b/src/object.rs @@ -49,12 +49,14 @@ pub trait AsObject: markers::Object { /// Implementation trait for deriving ActivityPub Object methods for a type /// /// Any type implementing AsApObject will automatically gain methods provided by ApObjectExt -pub trait AsApObject: markers::Object { +pub trait AsApObject: markers::Object { + type Inner; + /// Immutable borrow of `ApObject` - fn ap_object_ref(&self) -> &ApObject; + fn ap_object_ref(&self) -> &ApObject; /// Mutable borrow of `ApObject` - fn ap_object_mut(&mut self) -> &mut ApObject; + fn ap_object_mut(&mut self) -> &mut ApObject; } /// Implementation trait for deriving Place methods for a type @@ -2786,7 +2788,7 @@ pub trait ObjectExt: AsObject { /// This trait represents methods valid for any ActivityPub Object. /// /// Documentation for the fields related to these methods can be found on the `ApObject` struct -pub trait ApObjectExt: AsApObject { +pub trait ApObjectExt: AsApObject { /// Fetch the shares for the current object /// /// ```rust @@ -2801,7 +2803,7 @@ pub trait ApObjectExt: AsApObject { /// ``` fn shares<'a>(&'a self) -> Option<&'a IriString> where - Inner: 'a, + Self::Inner: 'a, { self.ap_object_ref().shares.as_ref() } @@ -2874,7 +2876,7 @@ pub trait ApObjectExt: AsApObject { /// ``` fn likes<'a>(&'a self) -> Option<&'a IriString> where - Inner: 'a, + Self::Inner: 'a, { self.ap_object_ref().likes.as_ref() } @@ -2947,7 +2949,7 @@ pub trait ApObjectExt: AsApObject { /// ``` fn source<'a>(&'a self) -> Option<&'a AnyBase> where - Inner: 'a, + Self::Inner: 'a, { self.ap_object_ref().source.as_ref() } @@ -3023,7 +3025,7 @@ pub trait ApObjectExt: AsApObject { /// ``` fn upload_media<'a>(&'a self) -> Option> where - Inner: 'a, + Self::Inner: 'a, { self.ap_object_ref() .upload_media @@ -5364,7 +5366,7 @@ impl markers::Base for Tombstone {} impl markers::Object for Tombstone {} impl ObjectExt for T where T: AsObject {} -impl ApObjectExt for T where T: AsApObject {} +impl ApObjectExt for T where T: AsApObject {} impl PlaceExt for T where T: AsPlace {} impl ProfileExt for T where T: AsProfile {} impl RelationshipExt for T where T: AsRelationship {} @@ -5424,15 +5426,17 @@ where } } -impl AsApObject for ApObject +impl AsApObject for ApObject where Inner: markers::Object, { - fn ap_object_ref(&self) -> &ApObject { + type Inner = Inner; + + fn ap_object_ref(&self) -> &ApObject { self } - fn ap_object_mut(&mut self) -> &mut ApObject { + fn ap_object_mut(&mut self) -> &mut ApObject { self } } diff --git a/src/unparsed.rs b/src/unparsed.rs index e2f74fe..a6e79ac 100644 --- a/src/unparsed.rs +++ b/src/unparsed.rs @@ -126,15 +126,17 @@ //! /// //! /// This allows us to access methods related to `inbox`, `outbox`, `following`, `followers`, //! /// `liked`, `streams`, `endpoints`, and more directly from the PublicKey struct -//! impl AsApActor for PublicKey +//! impl AsApActor for PublicKey //! where -//! Inner1: AsApActor, +//! Inner: AsApActor, //! { -//! fn ap_actor_ref(&self) -> &ApActor { +//! type Inner = Inner::Inner; +//! +//! fn ap_actor_ref(&self) -> &ApActor { //! self.inner.ap_actor_ref() //! } //! -//! fn ap_actor_mut(&mut self) -> &mut ApActor { +//! fn ap_actor_mut(&mut self) -> &mut ApActor { //! self.inner.ap_actor_mut() //! } //! }