Avoiding clone in map_to_enum

This commit is contained in:
Dessalines 2025-01-08 18:19:51 -05:00
parent 15f1671107
commit 43179177a0
5 changed files with 23 additions and 11 deletions

View file

@ -284,7 +284,7 @@ pub trait InternalToCombinedView {
type CombinedView; type CombinedView;
/// Maps the combined DB row to an enum /// Maps the combined DB row to an enum
fn map_to_enum(&self) -> Option<Self::CombinedView>; fn map_to_enum(self) -> Option<Self::CombinedView>;
} }
/// Wrapper for assert_eq! macro. Checks that vec matches the given length, and prints the /// Wrapper for assert_eq! macro. Checks that vec matches the given length, and prints the

View file

@ -238,7 +238,10 @@ impl PersonContentCombinedQuery {
let res = query.load::<PersonContentViewInternal>(conn).await?; let res = query.load::<PersonContentViewInternal>(conn).await?;
// Map the query results to the enum // 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) Ok(out)
} }
@ -247,9 +250,9 @@ impl PersonContentCombinedQuery {
impl InternalToCombinedView for PersonContentViewInternal { impl InternalToCombinedView for PersonContentViewInternal {
type CombinedView = PersonContentCombinedView; type CombinedView = PersonContentCombinedView;
fn map_to_enum(&self) -> Option<Self::CombinedView> { fn map_to_enum(self) -> Option<Self::CombinedView> {
// Use for a short alias // Use for a short alias
let v = self.clone(); let v = self;
if let (Some(comment), Some(counts)) = (v.comment, v.comment_counts) { if let (Some(comment), Some(counts)) = (v.comment, v.comment_counts) {
Some(PersonContentCombinedView::Comment(CommentView { Some(PersonContentCombinedView::Comment(CommentView {

View file

@ -239,7 +239,10 @@ impl PersonSavedCombinedQuery {
let res = query.load::<PersonContentViewInternal>(conn).await?; let res = query.load::<PersonContentViewInternal>(conn).await?;
// Map the query results to the enum // 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) Ok(out)
} }

View file

@ -329,7 +329,10 @@ impl ReportCombinedQuery {
let res = query.load::<ReportCombinedViewInternal>(conn).await?; let res = query.load::<ReportCombinedViewInternal>(conn).await?;
// Map the query results to the enum // 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) Ok(out)
} }
@ -338,9 +341,9 @@ impl ReportCombinedQuery {
impl InternalToCombinedView for ReportCombinedViewInternal { impl InternalToCombinedView for ReportCombinedViewInternal {
type CombinedView = ReportCombinedView; type CombinedView = ReportCombinedView;
fn map_to_enum(&self) -> Option<Self::CombinedView> { fn map_to_enum(self) -> Option<Self::CombinedView> {
// Use for a short alias // 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)) = ( if let (Some(post_report), Some(post), Some(community), Some(unread_comments), Some(counts)) = (
v.post_report, v.post_report,

View file

@ -378,7 +378,10 @@ impl ModlogCombinedQuery {
let res = query.load::<ModlogCombinedViewInternal>(conn).await?; let res = query.load::<ModlogCombinedViewInternal>(conn).await?;
// Map the query results to the enum // 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) Ok(out)
} }
@ -387,9 +390,9 @@ impl ModlogCombinedQuery {
impl InternalToCombinedView for ModlogCombinedViewInternal { impl InternalToCombinedView for ModlogCombinedViewInternal {
type CombinedView = ModlogCombinedView; type CombinedView = ModlogCombinedView;
fn map_to_enum(&self) -> Option<Self::CombinedView> { fn map_to_enum(self) -> Option<Self::CombinedView> {
// Use for a short alias // Use for a short alias
let v = self.clone(); let v = self;
if let (Some(admin_allow_instance), Some(instance)) = if let (Some(admin_allow_instance), Some(instance)) =
(v.admin_allow_instance, v.instance.clone()) (v.admin_allow_instance, v.instance.clone())