From 43179177a0a128e2518ee11a26b1120a7bf4509e Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 8 Jan 2025 18:19:51 -0500 Subject: [PATCH] Avoiding clone in map_to_enum --- crates/db_schema/src/lib.rs | 2 +- crates/db_views/src/person_content_combined_view.rs | 9 ++++++--- crates/db_views/src/person_saved_combined_view.rs | 5 ++++- crates/db_views/src/report_combined_view.rs | 9 ++++++--- crates/db_views_moderator/src/modlog_combined_view.rs | 9 ++++++--- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index 9a1a47429..4cf0c5030 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -284,7 +284,7 @@ pub trait InternalToCombinedView { type CombinedView; /// Maps the combined DB row to an enum - fn map_to_enum(&self) -> Option; + fn map_to_enum(self) -> Option; } /// Wrapper for assert_eq! macro. Checks that vec matches the given length, and prints the diff --git a/crates/db_views/src/person_content_combined_view.rs b/crates/db_views/src/person_content_combined_view.rs index edbb70093..b20447b98 100644 --- a/crates/db_views/src/person_content_combined_view.rs +++ b/crates/db_views/src/person_content_combined_view.rs @@ -238,7 +238,10 @@ impl PersonContentCombinedQuery { let res = query.load::(conn).await?; // Map the query results to the enum - let out = res.into_iter().filter_map(|u| u.map_to_enum()).collect(); + let out = res + .into_iter() + .filter_map(InternalToCombinedView::map_to_enum) + .collect(); Ok(out) } @@ -247,9 +250,9 @@ impl PersonContentCombinedQuery { impl InternalToCombinedView for PersonContentViewInternal { type CombinedView = PersonContentCombinedView; - fn map_to_enum(&self) -> Option { + fn map_to_enum(self) -> Option { // Use for a short alias - let v = self.clone(); + let v = self; if let (Some(comment), Some(counts)) = (v.comment, v.comment_counts) { Some(PersonContentCombinedView::Comment(CommentView { diff --git a/crates/db_views/src/person_saved_combined_view.rs b/crates/db_views/src/person_saved_combined_view.rs index e2aad01d4..4e1ce8df7 100644 --- a/crates/db_views/src/person_saved_combined_view.rs +++ b/crates/db_views/src/person_saved_combined_view.rs @@ -239,7 +239,10 @@ impl PersonSavedCombinedQuery { let res = query.load::(conn).await?; // Map the query results to the enum - let out = res.into_iter().filter_map(|u| u.map_to_enum()).collect(); + let out = res + .into_iter() + .filter_map(InternalToCombinedView::map_to_enum) + .collect(); Ok(out) } diff --git a/crates/db_views/src/report_combined_view.rs b/crates/db_views/src/report_combined_view.rs index eaf233985..6ba7910f9 100644 --- a/crates/db_views/src/report_combined_view.rs +++ b/crates/db_views/src/report_combined_view.rs @@ -329,7 +329,10 @@ impl ReportCombinedQuery { let res = query.load::(conn).await?; // Map the query results to the enum - let out = res.into_iter().filter_map(|u| u.map_to_enum()).collect(); + let out = res + .into_iter() + .filter_map(InternalToCombinedView::map_to_enum) + .collect(); Ok(out) } @@ -338,9 +341,9 @@ impl ReportCombinedQuery { impl InternalToCombinedView for ReportCombinedViewInternal { type CombinedView = ReportCombinedView; - fn map_to_enum(&self) -> Option { + fn map_to_enum(self) -> Option { // Use for a short alias - let v = self.clone(); + let v = self; if let (Some(post_report), Some(post), Some(community), Some(unread_comments), Some(counts)) = ( v.post_report, diff --git a/crates/db_views_moderator/src/modlog_combined_view.rs b/crates/db_views_moderator/src/modlog_combined_view.rs index 99a1a62dc..69613a664 100644 --- a/crates/db_views_moderator/src/modlog_combined_view.rs +++ b/crates/db_views_moderator/src/modlog_combined_view.rs @@ -378,7 +378,10 @@ impl ModlogCombinedQuery { let res = query.load::(conn).await?; // Map the query results to the enum - let out = res.into_iter().filter_map(|u| u.map_to_enum()).collect(); + let out = res + .into_iter() + .filter_map(InternalToCombinedView::map_to_enum) + .collect(); Ok(out) } @@ -387,9 +390,9 @@ impl ModlogCombinedQuery { impl InternalToCombinedView for ModlogCombinedViewInternal { type CombinedView = ModlogCombinedView; - fn map_to_enum(&self) -> Option { + fn map_to_enum(self) -> Option { // Use for a short alias - let v = self.clone(); + let v = self; if let (Some(admin_allow_instance), Some(instance)) = (v.admin_allow_instance, v.instance.clone())