Skip to content

Commit 04b0b5a

Browse files
willmurphyscodesgrif
authored andcommitted
Issue 1135: Associated constants (diesel-rs#1145)
* 1135: Change has_static_query_id() to be associated const This replaces `fn has_static_query_id() -> bool` with `const HAS_STATIC_QUERY_ID: bool` on the trait `QueryId`. * remove allow associated consts, since this feature is now stable * remove extra line above const declaration * change tests to use const HAS_STATIC_QUERY_ID * 1135: Use const NAME instead of fn name() On trait `diesel::query_source::Column`, use an associated constant instead of a method to return the columns name. Also change all references to this trait to use the constant. * 1135: use const FIELDS_NEEDED instead of fields_needed() In `diesel::types::FromSqlRow`, use an associated constant instead of a function to return the number of fields needed by structs implementing the FromSqlRow trait. * Style change Organize traits and their implementers to that they have types, then consts, then functions, in that order, and with blank lines between the groups. * fix missing line and trailing whitespace
1 parent b8076e0 commit 04b0b5a

19 files changed

Lines changed: 41 additions & 90 deletions

File tree

diesel/src/expression/bound.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ impl<T, U, DB> QueryFragment<DB> for Bound<T, U> where
3838
impl<T: QueryId, U> QueryId for Bound<T, U> {
3939
type QueryId = Bound<T::QueryId, ()>;
4040

41-
fn has_static_query_id() -> bool {
42-
T::has_static_query_id()
43-
}
41+
const HAS_STATIC_QUERY_ID: bool = T::HAS_STATIC_QUERY_ID;
4442
}
4543

4644
impl<T, U, QS> SelectableExpression<QS> for Bound<T, U> where

diesel/src/expression/coerce.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ impl<T, ST, DB> QueryFragment<DB> for Coerce<T, ST> where
6060
impl<T: QueryId, ST: 'static> QueryId for Coerce<T, ST> {
6161
type QueryId = Coerce<T::QueryId, ST>;
6262

63-
fn has_static_query_id() -> bool {
64-
true
65-
}
63+
const HAS_STATIC_QUERY_ID: bool = true;
6664
}
6765

6866
impl<T, ST> NonAggregate for Coerce<T, ST> where

diesel/src/expression/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,5 @@ impl<QS, T, DB> BoxableExpression<QS, DB> for T where
229229
impl<QS, ST, DB> QueryId for BoxableExpression<QS, DB, SqlType=ST> {
230230
type QueryId = ();
231231

232-
fn has_static_query_id() -> bool {
233-
false
234-
}
232+
const HAS_STATIC_QUERY_ID: bool = false;
235233
}

diesel/src/expression/nullable.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ impl<T, QS> AppearsOnTable<QS> for Nullable<T> where
4040
impl<T: QueryId> QueryId for Nullable<T> {
4141
type QueryId = T::QueryId;
4242

43-
fn has_static_query_id() -> bool {
44-
T::has_static_query_id()
45-
}
43+
const HAS_STATIC_QUERY_ID: bool = T::HAS_STATIC_QUERY_ID;
4644
}
4745

4846
impl<T> NonAggregate for Nullable<T> where

diesel/src/expression/operators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl<T, U, DB> Changeset<DB> for Eq<T, U> where
329329
}
330330

331331
fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
332-
try!(out.push_identifier(T::name()));
332+
try!(out.push_identifier(T::NAME));
333333
out.push_sql(" = ");
334334
QueryFragment::walk_ast(&self.right, out)
335335
}

diesel/src/expression/unchecked_bind.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ impl<Query, Value, ST> QueryId for UncheckedBind<Query, Value, ST> where
3333
{
3434
type QueryId = UncheckedBind<Query::QueryId, (), ST::QueryId>;
3535

36-
fn has_static_query_id() -> bool {
37-
Query::has_static_query_id() && ST::has_static_query_id()
38-
}
36+
const HAS_STATIC_QUERY_ID: bool = Query::HAS_STATIC_QUERY_ID && ST::HAS_STATIC_QUERY_ID;
3937
}
4038

4139
impl<Query, Value, ST, DB> QueryFragment<DB> for UncheckedBind<Query, Value, ST> where

diesel/src/macros/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ macro_rules! __diesel_column {
6868
impl $crate::query_source::Column for $column_name {
6969
type Table = $($table)::*;
7070

71-
fn name() -> &'static str {
72-
$sql_name
73-
}
71+
const NAME: &'static str = $sql_name;
7472
}
7573

7674
impl<T> $crate::EqAll<T> for $column_name where

diesel/src/macros/query_id.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ macro_rules! impl_query_id {
55
impl $crate::query_builder::QueryId for $name {
66
type QueryId = Self;
77

8-
fn has_static_query_id() -> bool {
9-
true
10-
}
8+
const HAS_STATIC_QUERY_ID: bool = true;
119
}
1210
};
1311

@@ -18,19 +16,15 @@ macro_rules! impl_query_id {
1816
{
1917
type QueryId = $name<$($ty_param::QueryId),*>;
2018

21-
fn has_static_query_id() -> bool {
22-
$($ty_param::has_static_query_id() &&)* true
23-
}
19+
const HAS_STATIC_QUERY_ID: bool = $($ty_param::HAS_STATIC_QUERY_ID &&)* true;
2420
}
2521
};
2622

2723
(noop: $name: ident) => {
2824
impl $crate::query_builder::QueryId for $name {
2925
type QueryId = ();
3026

31-
fn has_static_query_id() -> bool {
32-
false
33-
}
27+
const HAS_STATIC_QUERY_ID: bool = false;
3428
}
3529
};
3630

@@ -39,9 +33,7 @@ macro_rules! impl_query_id {
3933
impl<$($ty_param),*> $crate::query_builder::QueryId for $name<$($ty_param),*> {
4034
type QueryId = ();
4135

42-
fn has_static_query_id() -> bool {
43-
false
44-
}
36+
const HAS_STATIC_QUERY_ID: bool = false;
4537
}
4638
}
4739
}

diesel/src/mysql/types/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ impl HasSqlType<::types::Numeric> for Mysql {
7878
impl QueryId for ::types::Numeric {
7979
type QueryId = Self;
8080

81-
fn has_static_query_id() -> bool {
82-
true
83-
}
81+
const HAS_STATIC_QUERY_ID: bool = true;
8482
}
8583

8684
/// Represents the MySQL datetime type.

diesel/src/pg/upsert/on_conflict_actions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<T> QueryFragment<Pg> for Excluded<T> where
169169
{
170170
fn walk_ast(&self, mut out: AstPass<Pg>) -> QueryResult<()> {
171171
out.push_sql("excluded.");
172-
try!(out.push_identifier(T::name()));
172+
try!(out.push_identifier(T::NAME));
173173
Ok(())
174174
}
175175
}

0 commit comments

Comments
 (0)