From cf6602ea2e33234cd96904796b243898d407bedc Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 20 May 2025 10:32:04 +0200 Subject: [PATCH] Revert "Remove some uses of async_trait" This reverts commit 51bf4b332e89d081f626798f510cf311085e34b4. --- docs/10_fetching_objects_with_unknown_type.md | 2 +- examples/live_federation/objects/person.rs | 1 + examples/live_federation/objects/post.rs | 1 + examples/local_federation/objects/person.rs | 1 + examples/local_federation/objects/post.rs | 1 + src/fetch/mod.rs | 1 - src/fetch/object_id.rs | 2 +- src/traits/either.rs | 1 + src/traits/mod.rs | 46 ++++++++----------- 9 files changed, 27 insertions(+), 29 deletions(-) diff --git a/docs/10_fetching_objects_with_unknown_type.md b/docs/10_fetching_objects_with_unknown_type.md index 8a15953..96392d4 100644 --- a/docs/10_fetching_objects_with_unknown_type.md +++ b/docs/10_fetching_objects_with_unknown_type.md @@ -26,7 +26,7 @@ pub enum SearchableObjects { Note(Note) } - +#[async_trait::async_trait] impl Object for SearchableDbObjects { type DataType = DbConnection; type Kind = SearchableObjects; diff --git a/examples/live_federation/objects/person.rs b/examples/live_federation/objects/person.rs index e7e334b..d9439ea 100644 --- a/examples/live_federation/objects/person.rs +++ b/examples/live_federation/objects/person.rs @@ -63,6 +63,7 @@ pub struct Person { public_key: PublicKey, } +#[async_trait::async_trait] impl Object for DbUser { type DataType = DatabaseHandle; type Kind = Person; diff --git a/examples/live_federation/objects/post.rs b/examples/live_federation/objects/post.rs index 31edeb1..1b19fac 100644 --- a/examples/live_federation/objects/post.rs +++ b/examples/live_federation/objects/post.rs @@ -44,6 +44,7 @@ pub struct Mention { pub kind: MentionType, } +#[async_trait::async_trait] impl Object for DbPost { type DataType = DatabaseHandle; type Kind = Note; diff --git a/examples/local_federation/objects/person.rs b/examples/local_federation/objects/person.rs index 57b4ec2..0ae402f 100644 --- a/examples/local_federation/objects/person.rs +++ b/examples/local_federation/objects/person.rs @@ -128,6 +128,7 @@ impl DbUser { } } +#[async_trait::async_trait] impl Object for DbUser { type DataType = DatabaseHandle; type Kind = Person; diff --git a/examples/local_federation/objects/post.rs b/examples/local_federation/objects/post.rs index 7868e07..cbdf8e8 100644 --- a/examples/local_federation/objects/post.rs +++ b/examples/local_federation/objects/post.rs @@ -41,6 +41,7 @@ pub struct Note { content: String, } +#[async_trait::async_trait] impl Object for DbPost { type DataType = DatabaseHandle; type Kind = Note; diff --git a/src/fetch/mod.rs b/src/fetch/mod.rs index 0365b3e..424a115 100644 --- a/src/fetch/mod.rs +++ b/src/fetch/mod.rs @@ -109,7 +109,6 @@ async fn fetch_object_http_with_accept( let mut counter = data.request_counter.fetch_add(1, Ordering::SeqCst); // fetch_add returns old value so we need to increment manually here counter += 1; - url.to_string(); if counter > config.http_fetch_limit { return Err(Error::RequestLimit); } diff --git a/src/fetch/object_id.rs b/src/fetch/object_id.rs index 4792cd9..e702972 100644 --- a/src/fetch/object_id.rs +++ b/src/fetch/object_id.rs @@ -144,7 +144,7 @@ where data: &Data<::DataType>, ) -> Result, ::Error> { let id = self.0.clone(); - ::read_from_id(*id, data).await + Object::read_from_id(*id, data).await } /// Fetch object from origin instance over HTTP, then verify and parse it. diff --git a/src/traits/either.rs b/src/traits/either.rs index 9c3d238..95c2411 100644 --- a/src/traits/either.rs +++ b/src/traits/either.rs @@ -15,6 +15,7 @@ pub enum UntaggedEither { Right(R), } +#[async_trait] impl Object for Either where T: Object + Object + Send, diff --git a/src/traits/mod.rs b/src/traits/mod.rs index 58fadcc..d99d812 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -46,7 +46,7 @@ pub mod either; /// content: String, /// } /// -/// +/// #[async_trait::async_trait] /// impl Object for DbPost { /// type DataType = DbConnection; /// type Kind = Note; @@ -96,6 +96,7 @@ pub mod either; /// } /// /// } +#[async_trait] pub trait Object: Sized + Debug { /// App data type passed to handlers. Must be identical to /// [crate::config::FederationConfigBuilder::app_data] type. @@ -121,29 +122,23 @@ pub trait Object: Sized + Debug { /// Try to read the object with given `id` from local database. /// /// Should return `Ok(None)` if not found. - fn read_from_id( + async fn read_from_id( object_id: Url, data: &Data, - ) -> impl std::future::Future, Self::Error>> + Send; + ) -> Result, Self::Error>; /// Mark remote object as deleted in local database. /// /// Called when a `Delete` activity is received, or if fetch returns a `Tombstone` object. - fn delete( - self, - _data: &Data, - ) -> impl std::future::Future> + Send { - async { Ok(()) } + async fn delete(self, _data: &Data) -> Result<(), Self::Error> { + Ok(()) } /// Convert database type to Activitypub type. /// /// Called when a local object gets fetched by another instance over HTTP, or when an object /// gets sent in an activity. - fn into_json( - self, - data: &Data, - ) -> impl std::future::Future> + Send; + async fn into_json(self, data: &Data) -> Result; /// Verifies that the received object is valid. /// @@ -152,21 +147,18 @@ pub trait Object: Sized + Debug { /// /// It is necessary to use a separate method for this, because it might be used for activities /// like `Delete/Note`, which shouldn't perform any database write for the inner `Note`. - fn verify( + async fn verify( json: &Self::Kind, expected_domain: &Url, data: &Data, - ) -> impl std::future::Future> + Send; + ) -> Result<(), Self::Error>; /// Convert object from ActivityPub type to database type. /// /// Called when an object is received from HTTP fetch or as part of an activity. This method /// should write the received object to database. Note that there is no distinction between /// create and update, so an `upsert` operation should be used. - fn from_json( - json: Self::Kind, - data: &Data, - ) -> impl std::future::Future> + Send; + async fn from_json(json: Self::Kind, data: &Data) -> Result; } /// Handler for receiving incoming activities. @@ -303,6 +295,7 @@ where } /// Trait for federating collections +#[async_trait] pub trait Collection: Sized { /// Actor or object that this collection belongs to type Owner; @@ -315,31 +308,31 @@ pub trait Collection: Sized { type Error; /// Reads local collection from database and returns it as Activitypub JSON. - fn read_local( + async fn read_local( owner: &Self::Owner, data: &Data, - ) -> impl std::future::Future> + Send; + ) -> Result; /// Verifies that the received object is valid. /// /// You should check here that the domain of id matches `expected_domain`. Additionally you /// should perform any application specific checks. - fn verify( + async fn verify( json: &Self::Kind, expected_domain: &Url, data: &Data, - ) -> impl std::future::Future> + Send; + ) -> Result<(), Self::Error>; /// Convert object from ActivityPub type to database type. /// /// Called when an object is received from HTTP fetch or as part of an activity. This method /// should also write the received object to database. Note that there is no distinction /// between create and update, so an `upsert` operation should be used. - fn from_json( + async fn from_json( json: Self::Kind, owner: &Self::Owner, data: &Data, - ) -> impl std::future::Future> + Send; + ) -> Result; } /// Some impls of these traits for use in tests. Dont use this from external crates. @@ -348,7 +341,7 @@ pub trait Collection: Sized { #[doc(hidden)] #[allow(clippy::unwrap_used)] pub mod tests { - use super::{ActivityHandler, Actor, Data, Debug, Object, PublicKey, Url}; + use super::{async_trait, ActivityHandler, Actor, Data, Debug, Object, PublicKey, Url}; use crate::{ error::Error, fetch::object_id::ObjectId, @@ -356,7 +349,6 @@ pub mod tests { protocol::verification::verify_domains_match, }; use activitystreams_kinds::{activity::FollowType, actor::PersonType}; - use async_trait::async_trait; use serde::{Deserialize, Serialize}; use std::sync::LazyLock; @@ -413,6 +405,7 @@ pub mod tests { local: false, }); + #[async_trait] impl Object for DbUser { type DataType = DbConnection; type Kind = Person; @@ -516,6 +509,7 @@ pub mod tests { #[derive(Debug, Clone)] pub struct DbPost {} + #[async_trait] impl Object for DbPost { type DataType = DbConnection; type Kind = Note;