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
use core::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Rem, Shl, Shr, Sub};

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

macro_rules! def {
    ($Op:ident, $RefOp:ident, $op:ident, $ref_op:ident, $assert:expr) => {
        doc!(
            "An escape hatch for implimenting",
            concat!("`", stringify!($Op), "`"),
            "for references to newtypes.",
            "",
            "As of Rust 1.52.1, the following code does not compile:",
            "```compile_fail",
            concat!("use core::ops::", stringify!($Op), ";"),
            "",
            "#[derive(PartialEq)]",
            "struct A<T>(T);",
            "",
            concat!("impl<'a, 'b, T, U> ", stringify!($Op), "<&'b A<U>> for &'a A<T>"),
            "where",
            concat!("    T: ", stringify!($Op), "<&'b U>,"),
            concat!("    &'a T: ", stringify!($Op), "<&'b U, Output = T::Output>,"),
            "{",
            concat!("    type Output = A<T::Output>;"),
            "",
            concat!("    fn ", stringify!($op), "(self, other: &'b A<U>) -> Self::Output {"),
            concat!("        A(self.0.", stringify!($op), "(&other.0))"),
            "    }",
            "}",
            "",
            "pub fn f<T, U>(a: T, b: U)",
            "where",
            concat!("    for<'a, 'b> &'a T: ", stringify!($Op), "<&'b U>,"),
            "{",
            concat!("    let a_b = (&a).", stringify!($op), "(&b);"),
            "",
            concat!("    // to do something with `a`, `b`, and `a_b`"),
            "    todo!();",
            "}",
            "",
            "pub fn g<T, U>(a: T, b: U)",
            "where",
            concat!("    for<'a, 'b> &'a T: ", stringify!($Op), "<&'b U>,"),
            "{",
            "    f(a, b);",
            "}",
            "",
            concat!("assert!(", stringify!($assert), ");"),
            "```",
            "but the following code does:",
            "```",
            concat!("use core::ops::", stringify!($Op), ";"),
            concat!("use ref_ops::", stringify!($RefOp),";"),
            "",
            "#[derive(PartialEq)]",
            "struct A<T>(T);",
            "",
            concat!("impl<'a, T, U> ", stringify!($Op), "<&'a A<U>> for &A<T>"),
            "where",
            concat!("    T: ", stringify!($RefOp), "<&'a U>,"),
            "{",
            "    type Output = A<T::Output>;",
            "",
            concat!("    fn ", stringify!($op), "(self, other: &'a A<U>) -> Self::Output {"),
            concat!("        A(self.0.", stringify!($ref_op), "(&other.0))"),
            "    }",
            "}",
            "",
            "pub fn f<T, U>(a: T, b: U)",
            "where",
            concat!("    for<'a, 'b> &'a T: ", stringify!($Op), "<&'b U>,"),
            "{",
            concat!("    let a_b = (&a).", stringify!($op), "(&b);"),
            "",
            concat!("    // to do something with `a`, `b`, and `a_b`"),
            "    todo!();",
            "}",
            "",
            "pub fn g<T, U>(a: T, b: U)",
            "where",
            concat!("    for<'a, 'b> &'a T: ", stringify!($Op), "<&'b U>,"),
            "{",
            "    f(a, b);",
            "}",
            "",
            concat!("assert!(", stringify!($assert), ");"),
            "```",
            @pub trait $RefOp<Rhs>: $Op<Rhs> {
                fn $ref_op(&self, other: Rhs) -> Self::Output;
            }
        );

        impl<T, Rhs> $RefOp<Rhs> for T
        where
            Self: $Op<Rhs>,
            for<'a> &'a Self: $Op<Rhs, Output = Self::Output>,
        {
            fn $ref_op(&self, other: Rhs) -> Self::Output {
                self.$op(other)
            }
        }
    };
}

def!(Add, RefAdd, add, ref_add, &A(1.0) + &A(2.0) == A(3.0));
def!(BitAnd, RefBitAnd, bitand, ref_bitand, &A(6) & &A(5) == A(4));
def!(BitOr, RefBitOr, bitor, ref_bitor, &A(3) | &A(5) == A(7));
def!(BitXor, RefBitXor, bitxor, ref_bitxor, &A(3) ^ &A(5) == A(6));
def!(Div, RefDiv, div, ref_div, &A(6.0) / &A(2.0) == A(3.0));
def!(Mul, RefMul, mul, ref_mul, &A(2.0) * &A(3.0) == A(6.0));
def!(Rem, RefRem, rem, ref_rem, &A(6.0) % &A(4.0) == A(2.0));
def!(Shl, RefShl, shl, ref_shl, &A(3) << &A(2) == A(12));
def!(Shr, RefShr, shr, ref_shr, &A(12) >> &A(2) == A(3));
def!(Sub, RefSub, sub, ref_sub, &A(3.0) - &A(1.0) == A(2.0));