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
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 mutable 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 mut A<T>"),
"where",
concat!(" &'a mut 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>(mut a: T)",
"where",
concat!(" for<'a> &'a mut T: ", stringify!($Op), ","),
"{",
concat!(" let _op_a = (&mut a).", stringify!($op), "();"),
"",
concat!(" // to do something with `a` and `_op_a`"),
"}",
"",
"fn _g<T>(a: T)",
"where",
concat!(" for<'a> &'a mut 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 &mut 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>(mut a: T)",
"where",
concat!(" for<'a> &'a mut T: ", stringify!($Op), ","),
"{",
concat!(" let _op_a = (&mut a).", stringify!($op), "();"),
"",
concat!(" // to do something with `a` and `_op_a`"),
"}",
"",
"fn _g<T>(a: T)",
"where",
concat!(" for<'a> &'a mut 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(&mut self) -> Self::Output;
);
}
);
impl<T, O> $op::Sealed for T
where
T: ?Sized,
for<'a> &'a mut T: $Op<Output = O>,
{
}
impl<T, O> $RefOp for T
where
T: ?Sized,
for<'a> &'a mut T: $Op<Output = O>,
{
type Output = O;
fn $ref_op(&mut self) -> O {
self.$op()
}
}
};
}
def_unary!(Neg, neg, RefMutNeg, ref_mut_neg);
def_unary!(Not, not, RefMutNot, ref_mut_not);
#[cfg(test)]
mod tests {
use super::*;
macro_rules! test_unary {
($fn:ident, $Op:ident, $op:ident, $RefOp:ident, $ref_op:ident) => {
#[test]
fn $fn() {
#[derive(PartialEq)]
struct A<T: ?Sized>(T);
impl<T> $Op for &mut A<T>
where
T: ?Sized + $RefOp,
{
type Output = A<T::Output>;
fn $op(self) -> Self::Output {
A(self.0.$ref_op())
}
}
fn _f<T>(mut a: T)
where
for<'a> &'a mut T: $Op,
{
let _op_a = (&mut a).$op();
}
fn _g<T>(a: T)
where
for<'a> &'a mut T: $Op,
{
_f(a);
}
}
};
}
test_unary!(test_neg, Neg, neg, RefMutNeg, ref_mut_neg);
test_unary!(test_not, Not, not, RefMutNot, ref_mut_not);
}