Skip to content

Commit 5592a50

Browse files
authored
Merge pull request diesel-rs#2246 from diesel-rs/sg-use-free-constant
Use unnamed free constants to wrap derive impls
2 parents 6404b7e + ca5b325 commit 5592a50

16 files changed

+200
-252
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ matrix:
5454
env: RUSTFLAGS="--cap-lints=warn"
5555
script:
5656
- (cd diesel_compile_tests && cargo test)
57-
- rust: 1.36.0
57+
- rust: 1.37.0
5858
name: "Rustfmt && Clippy"
5959
script:
6060
- rustup component add rustfmt clippy
@@ -66,8 +66,8 @@ matrix:
6666
- SQLITE_DATABASE_URL=/tmp/test.db
6767
script:
6868
- (cd diesel_cli && cargo test --no-default-features --features "sqlite-bundled")
69-
- rust: 1.36.0
70-
name: "Minimal supported rust version == 1.36.0"
69+
- rust: 1.37.0
70+
name: "Minimal supported rust version == 1.37.0"
7171
script:
7272
- cargo check --all
7373

diesel_derives/src/as_changeset.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,32 @@ pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagno
5858
.emit();
5959
}
6060

61-
Ok(wrap_in_dummy_mod(
62-
model.dummy_mod_name("as_changeset"),
63-
quote!(
64-
use diesel::query_builder::AsChangeset;
65-
use diesel::prelude::*;
61+
Ok(wrap_in_dummy_mod(quote!(
62+
use diesel::query_builder::AsChangeset;
63+
use diesel::prelude::*;
6664

67-
impl #impl_generics AsChangeset for &'update #struct_name #ty_generics
68-
#where_clause
69-
{
70-
type Target = #table_name::table;
71-
type Changeset = <(#(#ref_changeset_ty,)*) as AsChangeset>::Changeset;
65+
impl #impl_generics AsChangeset for &'update #struct_name #ty_generics
66+
#where_clause
67+
{
68+
type Target = #table_name::table;
69+
type Changeset = <(#(#ref_changeset_ty,)*) as AsChangeset>::Changeset;
7270

73-
fn as_changeset(self) -> Self::Changeset {
74-
(#(#ref_changeset_expr,)*).as_changeset()
75-
}
71+
fn as_changeset(self) -> Self::Changeset {
72+
(#(#ref_changeset_expr,)*).as_changeset()
7673
}
74+
}
7775

78-
impl #impl_generics AsChangeset for #struct_name #ty_generics
79-
#where_clause
80-
{
81-
type Target = #table_name::table;
82-
type Changeset = <(#(#direct_changeset_ty,)*) as AsChangeset>::Changeset;
76+
impl #impl_generics AsChangeset for #struct_name #ty_generics
77+
#where_clause
78+
{
79+
type Target = #table_name::table;
80+
type Changeset = <(#(#direct_changeset_ty,)*) as AsChangeset>::Changeset;
8381

84-
fn as_changeset(self) -> Self::Changeset {
85-
(#(#direct_changeset_expr,)*).as_changeset()
86-
}
82+
fn as_changeset(self) -> Self::Changeset {
83+
(#(#direct_changeset_expr,)*).as_changeset()
8784
}
88-
),
89-
))
85+
}
86+
)))
9087
}
9188

9289
fn field_changeset_ty(

diesel_derives/src/as_expression.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use proc_macro2::{self, Ident, Span};
1+
use proc_macro2;
22
use syn;
33

44
use meta::*;
55
use util::*;
66

77
pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
8-
let dummy_mod = format!("_impl_as_expression_for_{}", item.ident,).to_lowercase();
98
let flags =
109
MetaItem::with_name(&item.attrs, "diesel").unwrap_or_else(|| MetaItem::empty("diesel"));
1110
let is_sized = !flags.has_flag("not_sized");
@@ -102,17 +101,14 @@ pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagno
102101
});
103102

104103
if any_sql_types {
105-
Ok(wrap_in_dummy_mod(
106-
Ident::new(&dummy_mod, Span::call_site()),
107-
quote! {
108-
use diesel::expression::AsExpression;
109-
use diesel::expression::bound::Bound;
110-
use diesel::sql_types::Nullable;
111-
use diesel::serialize::{self, ToSql, Output};
112-
113-
#(#tokens)*
114-
},
115-
))
104+
Ok(wrap_in_dummy_mod(quote! {
105+
use diesel::expression::AsExpression;
106+
use diesel::expression::bound::Bound;
107+
use diesel::sql_types::Nullable;
108+
use diesel::serialize::{self, ToSql, Output};
109+
110+
#(#tokens)*
111+
}))
116112
} else {
117113
Ok(quote!())
118114
}

diesel_derives/src/associations.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagno
2323
},
2424
);
2525

26-
Ok(wrap_in_dummy_mod(
27-
model.dummy_mod_name("associations"),
28-
quote!(#(#tokens)*),
29-
))
26+
Ok(wrap_in_dummy_mod(quote!(#(#tokens)*)))
3027
}
3128

3229
fn derive_belongs_to(
Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use proc_macro2::{self, Ident, Span};
1+
use proc_macro2;
22
use syn;
33

44
use util::*;
@@ -19,61 +19,56 @@ pub fn derive(mut item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Di
1919
impl_generics.params.push(parse_quote!(__Rhs));
2020
let (impl_generics, _, _) = impl_generics.split_for_impl();
2121

22-
let dummy_name = format!("_impl_diesel_numeric_ops_for_{}", item.ident);
22+
Ok(wrap_in_dummy_mod(quote! {
23+
use diesel::expression::{ops, Expression, AsExpression};
24+
use diesel::sql_types::ops::{Add, Sub, Mul, Div};
2325

24-
Ok(wrap_in_dummy_mod(
25-
Ident::new(&dummy_name.to_lowercase(), Span::call_site()),
26-
quote! {
27-
use diesel::expression::{ops, Expression, AsExpression};
28-
use diesel::sql_types::ops::{Add, Sub, Mul, Div};
26+
impl #impl_generics ::std::ops::Add<__Rhs> for #struct_name #ty_generics
27+
#where_clause
28+
<Self as Expression>::SqlType: Add,
29+
__Rhs: AsExpression<<<Self as Expression>::SqlType as Add>::Rhs>,
30+
{
31+
type Output = ops::Add<Self, __Rhs::Expression>;
2932

30-
impl #impl_generics ::std::ops::Add<__Rhs> for #struct_name #ty_generics
31-
#where_clause
32-
<Self as Expression>::SqlType: Add,
33-
__Rhs: AsExpression<<<Self as Expression>::SqlType as Add>::Rhs>,
34-
{
35-
type Output = ops::Add<Self, __Rhs::Expression>;
36-
37-
fn add(self, rhs: __Rhs) -> Self::Output {
38-
ops::Add::new(self, rhs.as_expression())
39-
}
33+
fn add(self, rhs: __Rhs) -> Self::Output {
34+
ops::Add::new(self, rhs.as_expression())
4035
}
36+
}
4137

42-
impl #impl_generics ::std::ops::Sub<__Rhs> for #struct_name #ty_generics
43-
#where_clause
44-
<Self as Expression>::SqlType: Sub,
45-
__Rhs: AsExpression<<<Self as Expression>::SqlType as Sub>::Rhs>,
46-
{
47-
type Output = ops::Sub<Self, __Rhs::Expression>;
38+
impl #impl_generics ::std::ops::Sub<__Rhs> for #struct_name #ty_generics
39+
#where_clause
40+
<Self as Expression>::SqlType: Sub,
41+
__Rhs: AsExpression<<<Self as Expression>::SqlType as Sub>::Rhs>,
42+
{
43+
type Output = ops::Sub<Self, __Rhs::Expression>;
4844

49-
fn sub(self, rhs: __Rhs) -> Self::Output {
50-
ops::Sub::new(self, rhs.as_expression())
51-
}
45+
fn sub(self, rhs: __Rhs) -> Self::Output {
46+
ops::Sub::new(self, rhs.as_expression())
5247
}
48+
}
5349

54-
impl #impl_generics ::std::ops::Mul<__Rhs> for #struct_name #ty_generics
55-
#where_clause
56-
<Self as Expression>::SqlType: Mul,
57-
__Rhs: AsExpression<<<Self as Expression>::SqlType as Mul>::Rhs>,
58-
{
59-
type Output = ops::Mul<Self, __Rhs::Expression>;
50+
impl #impl_generics ::std::ops::Mul<__Rhs> for #struct_name #ty_generics
51+
#where_clause
52+
<Self as Expression>::SqlType: Mul,
53+
__Rhs: AsExpression<<<Self as Expression>::SqlType as Mul>::Rhs>,
54+
{
55+
type Output = ops::Mul<Self, __Rhs::Expression>;
6056

61-
fn mul(self, rhs: __Rhs) -> Self::Output {
62-
ops::Mul::new(self, rhs.as_expression())
63-
}
57+
fn mul(self, rhs: __Rhs) -> Self::Output {
58+
ops::Mul::new(self, rhs.as_expression())
6459
}
60+
}
6561

66-
impl #impl_generics ::std::ops::Div<__Rhs> for #struct_name #ty_generics
67-
#where_clause
68-
<Self as Expression>::SqlType: Div,
69-
__Rhs: AsExpression<<<Self as Expression>::SqlType as Div>::Rhs>,
70-
{
71-
type Output = ops::Div<Self, __Rhs::Expression>;
62+
impl #impl_generics ::std::ops::Div<__Rhs> for #struct_name #ty_generics
63+
#where_clause
64+
<Self as Expression>::SqlType: Div,
65+
__Rhs: AsExpression<<<Self as Expression>::SqlType as Div>::Rhs>,
66+
{
67+
type Output = ops::Div<Self, __Rhs::Expression>;
7268

73-
fn div(self, rhs: __Rhs) -> Self::Output {
74-
ops::Div::new(self, rhs.as_expression())
75-
}
69+
fn div(self, rhs: __Rhs) -> Self::Output {
70+
ops::Div::new(self, rhs.as_expression())
7671
}
77-
},
78-
))
72+
}
73+
}))
7974
}

diesel_derives/src/from_sql_row.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,27 @@ pub fn derive(mut item: syn::DeriveInput) -> Result<TokenStream, Diagnostic> {
2525
}
2626
let (impl_generics, _, where_clause) = item.generics.split_for_impl();
2727

28-
let dummy_mod = format!("_impl_from_sql_row_for_{}", item.ident,).to_lowercase();
29-
Ok(wrap_in_dummy_mod(
30-
Ident::new(&dummy_mod, Span::call_site()),
31-
quote! {
32-
use diesel::deserialize::{self, FromSql, FromSqlRow, Queryable};
28+
Ok(wrap_in_dummy_mod(quote! {
29+
use diesel::deserialize::{self, FromSql, FromSqlRow, Queryable};
3330

34-
impl #impl_generics FromSqlRow<__ST, __DB> for #struct_ty
35-
#where_clause
31+
impl #impl_generics FromSqlRow<__ST, __DB> for #struct_ty
32+
#where_clause
33+
{
34+
fn build_from_row<R: diesel::row::Row<__DB>>(row: &mut R)
35+
-> deserialize::Result<Self>
3636
{
37-
fn build_from_row<R: diesel::row::Row<__DB>>(row: &mut R)
38-
-> deserialize::Result<Self>
39-
{
40-
FromSql::<__ST, __DB>::from_sql(row.take())
41-
}
37+
FromSql::<__ST, __DB>::from_sql(row.take())
4238
}
39+
}
4340

44-
impl #impl_generics Queryable<__ST, __DB> for #struct_ty
45-
#where_clause
46-
{
47-
type Row = Self;
41+
impl #impl_generics Queryable<__ST, __DB> for #struct_ty
42+
#where_clause
43+
{
44+
type Row = Self;
4845

49-
fn build(row: Self::Row) -> Self {
50-
row
51-
}
46+
fn build(row: Self::Row) -> Self {
47+
row
5248
}
53-
},
54-
))
49+
}
50+
}))
5551
}

diesel_derives/src/identifiable.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,27 @@ pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagno
2121
.map(|f| (&f.ty, f.name.access()))
2222
.unzip();
2323

24-
Ok(wrap_in_dummy_mod(
25-
model.dummy_mod_name("identifiable"),
26-
quote! {
27-
use diesel::associations::{HasTable, Identifiable};
28-
29-
impl #impl_generics HasTable for #struct_name #ty_generics
30-
#where_clause
31-
{
32-
type Table = #table_name::table;
33-
34-
fn table() -> Self::Table {
35-
#table_name::table
36-
}
24+
Ok(wrap_in_dummy_mod(quote! {
25+
use diesel::associations::{HasTable, Identifiable};
26+
27+
impl #impl_generics HasTable for #struct_name #ty_generics
28+
#where_clause
29+
{
30+
type Table = #table_name::table;
31+
32+
fn table() -> Self::Table {
33+
#table_name::table
3734
}
35+
}
3836

39-
impl #ref_generics Identifiable for &'ident #struct_name #ty_generics
40-
#where_clause
41-
{
42-
type Id = (#(&'ident #field_ty),*);
37+
impl #ref_generics Identifiable for &'ident #struct_name #ty_generics
38+
#where_clause
39+
{
40+
type Id = (#(&'ident #field_ty),*);
4341

44-
fn id(self) -> Self::Id {
45-
(#(&self#field_access),*)
46-
}
42+
fn id(self) -> Self::Id {
43+
(#(&self#field_access),*)
4744
}
48-
},
49-
))
45+
}
46+
}))
5047
}

0 commit comments

Comments
 (0)