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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use core::ops::{Neg, Not};

macro_rules! doc {
    ($( $x:expr, )* @$item:item) => {
        $( #[doc = $x] )*
        $item
    };
}

macro_rules! def_unary {
    ($Op:ident, $op:ident, $RefOp:ident, $ref_op:ident) => {
        mod $op {
            pub trait Sealed {}
        }

        doc!(
            concat!("`", stringify!($op), "` operation through references."),
            "",
            "As of Rust 1.65.0, the following code does not compile:",
            "```compile_fail",
            concat!("use core::ops::", stringify!($Op), ";"),
            "",
            "struct A<T>(T);",
            "",
            concat!("impl<'a, T, O> ", stringify!($Op), " for &'a A<T>"),
            "where",
            concat!("    &'a T: ", stringify!($Op), "<Output = O>,"),
            "{",
            concat!("    type Output = A<O>;"),
            "",
            concat!("    fn ", stringify!($op), "(self) -> Self::Output {"),
            concat!("        A(self.0.", stringify!($op), "())"),
            "    }",
            "}",
            "",
            "fn _f<T>(a: T)",
            "where",
            concat!("    for<'a> &'a T: ", stringify!($Op), ","),
            "{",
            concat!("    let _op_a = (&a).", stringify!($op), "();"),
            "",
            concat!("    // to do something with `a` and `_op_a`"),
            "}",
            "",
            "fn _g<T>(a: T)",
            "where",
            concat!("    for<'a> &'a T: ", stringify!($Op), ","),
            "{",
            "    _f(a);",
            "}",
            "```",
            "but the following code does:",
            "```",
            concat!("use core::ops::", stringify!($Op), ";"),
            concat!("use ref_ops::", stringify!($RefOp),";"),
            "",
            "struct A<T>(T);",
            "",            "",
            concat!("impl<T> ", stringify!($Op), " for &A<T>"),
            "where",
            concat!("    T: ", stringify!($RefOp), ","),
            "{",
            "    type Output = A<T::Output>;",
            "",
            concat!("    fn ", stringify!($op), "(self) -> Self::Output {"),
            concat!("        A(self.0.", stringify!($ref_op), "())"),
            "    }",
            "}",
            "",
            "fn _f<T>(a: T)",
            "where",
            concat!("    for<'a> &'a T: ", stringify!($Op), ","),
            "{",
            concat!("    let _op_a = (&a).", stringify!($op), "();"),
            "",
            concat!("    // to do something with `a` and `_op_a`"),
            "}",
            "",
            "fn _g<T>(a: T)",
            "where",
            concat!("    for<'a> &'a T: ", stringify!($Op), ","),
            "{",
            "    _f(a);",
            "}",
            "```",
            @pub trait $RefOp: $op::Sealed {
                doc!(
                    concat!("The resulting type after applying `", stringify!($op), "` operation."),
                    @type Output;
                );

                doc!(
                    concat!("Performs `", stringify!($op), "` operation."),
                    @fn $ref_op(&self) -> Self::Output;
                );
            }
        );

        impl<T, O> $op::Sealed for T
        where
            T: ?Sized,
            for<'a> &'a T: $Op<Output = O>,
        {
        }

        impl<T, O> $RefOp for T
        where
            T: ?Sized,
            for<'a> &'a T: $Op<Output = O>,
        {
            type Output = O;

            fn $ref_op(&self) -> O {
                self.$op()
            }
        }
    };
}

def_unary!(Neg, neg, RefNeg, ref_neg);
def_unary!(Not, not, RefNot, ref_not);

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! test_unary {
        ($fn:ident, $Op:ident, $op:ident, $RefOp:ident, $ref_op:ident, $assert:expr, $dummy:literal) => {
            #[test]
            fn $fn() {
                #[derive(PartialEq)]
                struct A<T: ?Sized>(T);

                impl<T> $Op for &A<T>
                where
                    T: ?Sized + $RefOp,
                {
                    type Output = A<T::Output>;

                    fn $op(self) -> Self::Output {
                        A(self.0.$ref_op())
                    }
                }

                fn f<T>(a: T)
                where
                    for<'a> &'a T: $Op,
                {
                    let _op_a = (&a).$op();

                    // to do something with `a` and `_op_a`
                }

                fn g<T>(a: T)
                where
                    for<'a> &'a T: $Op,
                {
                    f(a);
                }

                g($dummy);

                assert!($assert);
            }
        };
    }

    test_unary!(
        test_neg,
        Neg,
        neg,
        RefNeg,
        ref_neg,
        -&A(1.0) == A(-1.0),
        1.0
    );
    test_unary!(
        test_not,
        Not,
        not,
        RefNot,
        ref_not,
        !&A(true) == A(false),
        true
    );
}