Skip to content

Commit b2edcdb

Browse files
committed
Allow operators to be used in more places
This implements `std::ops::{Add, Sub, Mul, Div}` for every type I could think of. I've done this by introducing a new derive that we can use for any type with a `where` clause on its `Expression` impl. I've left it undocumented for now, because I think we may just want to bake this into a different derive once RFC 2056 is stable. I've also cleaned up the SQL type implementations, and added new ones for `Interval`. Fixes diesel-rs#1639.
1 parent c44b2db commit b2edcdb

13 files changed

Lines changed: 180 additions & 89 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
2323
* Added `sqlite-bundled` feature to `diesel_cli` to make installing on
2424
some platforms easier.
2525

26+
* All functions and operators provided by Diesel can now be used with numeric
27+
operators if the SQL type supports it.
28+
29+
* `PgInterval` can now be used with `-`, `*`, and `/`.
30+
2631
### Changed
2732

2833
* `sql_function!` has been redesigned. The syntax is now `sql_function!(fn

diesel/src/expression/bound.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use backend::Backend;
44
use query_builder::*;
55
use result::QueryResult;
66
use serialize::ToSql;
7-
use super::*;
87
use sql_types::HasSqlType;
8+
use super::*;
99

10-
#[derive(Debug, Clone, Copy)]
10+
#[derive(Debug, Clone, Copy, DieselNumericOps)]
1111
pub struct Bound<T, U> {
1212
item: U,
1313
_marker: PhantomData<T>,

diesel/src/expression/coerce.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use expression::*;
55
use query_builder::*;
66
use result::QueryResult;
77

8-
#[derive(Debug, Copy, Clone, QueryId)]
8+
#[derive(Debug, Copy, Clone, QueryId, DieselNumericOps)]
99
#[doc(hidden)]
1010
/// Coerces an expression to be another type. No checks are performed to ensure
1111
/// that the new type is valid in all positions that the previous type was.

diesel/src/expression/count.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn count_star() -> CountStar {
5555
CountStar
5656
}
5757

58-
#[derive(Debug, Clone, Copy, QueryId)]
58+
#[derive(Debug, Clone, Copy, QueryId, DieselNumericOps)]
5959
#[doc(hidden)]
6060
pub struct CountStar;
6161

diesel/src/expression/functions/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ macro_rules! __diesel_sql_function_body {
192192
use super::*;
193193
use $crate::sql_types::*;
194194

195-
#[derive(Debug, Clone, Copy, QueryId)]
195+
#[derive(Debug, Clone, Copy, QueryId, DieselNumericOps)]
196196
pub struct $fn_name<$($type_args,)* $($arg_name),*> {
197197
$(pub(in super) $arg_name: $arg_name,)*
198198
$(pub(in super) $type_args: ::std::marker::PhantomData<$type_args>,)*
@@ -209,7 +209,7 @@ macro_rules! __diesel_sql_function_body {
209209
$crate::expression::Expression
210210
for $fn_name<$($type_args,)* $($arg_name),*>
211211
where
212-
for <'a> ($(&'a $arg_name),*): $crate::expression::Expression,
212+
($($arg_name),*): $crate::expression::Expression,
213213
{
214214
type SqlType = $return_type;
215215
}

diesel/src/expression/grouped.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use expression::{Expression, NonAggregate};
33
use query_builder::*;
44
use result::QueryResult;
55

6-
#[derive(Debug, Copy, Clone, QueryId, Default)]
6+
#[derive(Debug, Copy, Clone, QueryId, Default, DieselNumericOps)]
77
pub struct Grouped<T>(pub T);
88

99
impl<T: Expression> Expression for Grouped<T> {

diesel/src/expression/nullable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use query_builder::*;
44
use result::QueryResult;
55
use sql_types::IntoNullable;
66

7-
#[derive(Debug, Copy, Clone)]
7+
#[derive(Debug, Copy, Clone, DieselNumericOps)]
88
pub struct Nullable<T>(T);
99

1010
impl<T> Nullable<T> {

diesel/src/expression/operators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ macro_rules! __diesel_operator_body {
6161
expression_ty_params = ($($expression_ty_params:ident,)*),
6262
expression_bounds = ($($expression_bounds:tt)*),
6363
) => {
64-
#[derive(Debug, Clone, Copy, QueryId)]
64+
#[derive(Debug, Clone, Copy, QueryId, DieselNumericOps)]
6565
#[doc(hidden)]
6666
pub struct $name<$($ty_param,)+> {
6767
$(pub(crate) $field_name: $ty_param,)+

diesel/src/expression/sql_literal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use query_builder::*;
66
use query_dsl::RunQueryDsl;
77
use result::QueryResult;
88

9-
#[derive(Debug, Clone)]
9+
#[derive(Debug, Clone, DieselNumericOps)]
1010
#[must_use = "Queries are only executed when calling `load`, `get_result`, or similar."]
1111
/// Returned by the [`sql()`] function.
1212
///

diesel/src/pg/types/mod.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -492,25 +492,15 @@ pub mod sql_types {
492492
mod ops {
493493
use super::sql_types::*;
494494
use sql_types::ops::*;
495-
use sql_types::{Interval, Nullable};
495+
use sql_types::Interval;
496496

497497
impl Add for Timestamptz {
498498
type Rhs = Interval;
499499
type Output = Timestamptz;
500500
}
501501

502-
impl Add for Nullable<Timestamptz> {
503-
type Rhs = Nullable<Interval>;
504-
type Output = Nullable<Timestamptz>;
505-
}
506-
507502
impl Sub for Timestamptz {
508503
type Rhs = Interval;
509504
type Output = Timestamptz;
510505
}
511-
512-
impl Sub for Nullable<Timestamptz> {
513-
type Rhs = Nullable<Interval>;
514-
type Output = Nullable<Timestamptz>;
515-
}
516506
}

0 commit comments

Comments
 (0)