1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use proc_macro2::TokenStream;
use syn::DeriveInput;

use util::wrap_in_dummy_mod;

pub fn derive(mut item: DeriveInput) -> TokenStream {
    let struct_name = &item.ident;

    {
        let where_clause = item
            .generics
            .where_clause
            .get_or_insert(parse_quote!(where));
        where_clause.predicates.push(parse_quote!(Self: Expression));
        where_clause.predicates.push_punct(Default::default());
    }

    let (_, ty_generics, where_clause) = item.generics.split_for_impl();
    let mut impl_generics = item.generics.clone();
    impl_generics.params.push(parse_quote!(__Rhs));
    let (impl_generics, _, _) = impl_generics.split_for_impl();

    wrap_in_dummy_mod(quote! {
        use diesel::internal::derives::numeric_ops as ops;
        use diesel::expression::{Expression, AsExpression};
        use diesel::sql_types::ops::{Add, Sub, Mul, Div};
        use diesel::sql_types::{SqlType, SingleValue};

        impl #impl_generics ::std::ops::Add<__Rhs> for #struct_name #ty_generics
        #where_clause
            Self: Expression,
            <Self as Expression>::SqlType: Add,
            <<Self as Expression>::SqlType as Add>::Rhs: SqlType + SingleValue,
            __Rhs: AsExpression<<<Self as Expression>::SqlType as Add>::Rhs>,
        {
            type Output = ops::Add<Self, __Rhs::Expression>;

            fn add(self, rhs: __Rhs) -> Self::Output {
                ops::Add::new(self, rhs.as_expression())
            }
        }

        impl #impl_generics ::std::ops::Sub<__Rhs> for #struct_name #ty_generics
        #where_clause
            Self: Expression,
            <Self as Expression>::SqlType: Sub,
            <<Self as Expression>::SqlType as Sub>::Rhs: SqlType + SingleValue,
            __Rhs: AsExpression<<<Self as Expression>::SqlType as Sub>::Rhs>,
        {
            type Output = ops::Sub<Self, __Rhs::Expression>;

            fn sub(self, rhs: __Rhs) -> Self::Output {
                ops::Sub::new(self, rhs.as_expression())
            }
        }

        impl #impl_generics ::std::ops::Mul<__Rhs> for #struct_name #ty_generics
        #where_clause
            Self: Expression,
            <Self as Expression>::SqlType: Mul,
            <<Self as Expression>::SqlType as Mul>::Rhs: SqlType + SingleValue,
            __Rhs: AsExpression<<<Self as Expression>::SqlType as Mul>::Rhs>,
        {
            type Output = ops::Mul<Self, __Rhs::Expression>;

            fn mul(self, rhs: __Rhs) -> Self::Output {
                ops::Mul::new(self, rhs.as_expression())
            }
        }

        impl #impl_generics ::std::ops::Div<__Rhs> for #struct_name #ty_generics
        #where_clause
            Self: Expression,
            <Self as Expression>::SqlType: Div,
            <<Self as Expression>::SqlType as Div>::Rhs: SqlType + SingleValue,
            __Rhs: AsExpression<<<Self as Expression>::SqlType as Div>::Rhs>,
        {
            type Output = ops::Div<Self, __Rhs::Expression>;

            fn div(self, rhs: __Rhs) -> Self::Output {
                ops::Div::new(self, rhs.as_expression())
            }
        }
    })
}