Skip to content

Commit 2b544c1

Browse files
authored
Use #[diesel::dsl::auto_type] in some places (#8970)
This commit replaces some of the manual written type aliases by `#[diesel::dsl::auto_type]` which just generates the correct return type based on the function content. This hopefully removes some complexity from the database code.
1 parent 21d7f0d commit 2b544c1

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/models/category.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub struct Category {
1616
}
1717

1818
type WithSlug<'a> = diesel::dsl::Eq<categories::slug, crate::sql::lower<&'a str>>;
19-
type BySlug<'a> = diesel::dsl::Filter<categories::table, WithSlug<'a>>;
2019

2120
#[derive(Associations, Insertable, Identifiable, Debug, Clone, Copy)]
2221
#[diesel(
@@ -36,8 +35,10 @@ impl Category {
3635
categories::slug.eq(crate::sql::lower(slug))
3736
}
3837

39-
pub fn by_slug(slug: &str) -> BySlug<'_> {
40-
categories::table.filter(Self::with_slug(slug))
38+
#[dsl::auto_type(no_type_alias)]
39+
pub fn by_slug<'a>(slug: &'a str) -> _ {
40+
let filter: WithSlug<'a> = Self::with_slug(slug);
41+
categories::table.filter(filter)
4142
}
4243

4344
pub fn update_crate(

src/models/krate.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::BTreeMap;
22

33
use chrono::NaiveDateTime;
44
use diesel::associations::Identifiable;
5+
use diesel::dsl;
56
use diesel::pg::Pg;
67
use diesel::prelude::*;
78
use diesel::sql_types::{Bool, Text};
@@ -80,8 +81,6 @@ pub const MAX_NAME_LENGTH: usize = 64;
8081

8182
type All = diesel::dsl::Select<crates::table, diesel::dsl::AsSelect<Crate, diesel::pg::Pg>>;
8283
type WithName<'a> = diesel::dsl::Eq<canon_crate_name<crates::name>, canon_crate_name<&'a str>>;
83-
type ByName<'a> = diesel::dsl::Filter<All, WithName<'a>>;
84-
type ByExactName<'a> = diesel::dsl::Filter<All, diesel::dsl::Eq<crates::name, &'a str>>;
8584

8685
#[derive(Insertable, AsChangeset, Default, Debug)]
8786
#[diesel(
@@ -170,12 +169,17 @@ impl Crate {
170169
canon_crate_name(crates::name).eq(canon_crate_name(name))
171170
}
172171

173-
pub fn by_name(name: &str) -> ByName<'_> {
174-
Crate::all().filter(Self::with_name(name))
172+
#[dsl::auto_type(no_type_alias)]
173+
pub fn by_name<'a>(name: &'a str) -> _ {
174+
let all: All = Crate::all();
175+
let filter: WithName<'a> = Self::with_name(name);
176+
all.filter(filter)
175177
}
176178

177-
pub fn by_exact_name(name: &str) -> ByExactName<'_> {
178-
Crate::all().filter(crates::name.eq(name))
179+
#[dsl::auto_type(no_type_alias)]
180+
pub fn by_exact_name<'a>(name: &'a str) -> _ {
181+
let all: All = Crate::all();
182+
all.filter(crates::name.eq(name))
179183
}
180184

181185
pub fn all() -> All {

0 commit comments

Comments
 (0)