@@ -96,6 +96,80 @@ pub trait Drop {
96
96
fn drop ( & mut self ) ;
97
97
}
98
98
99
+ // implements the unary operator "op &T"
100
+ // based on "op T" where T is expected to be `Copy`able
101
+ macro_rules! forward_ref_unop {
102
+ ( impl $imp: ident, $method: ident for $t: ty) => {
103
+ #[ unstable]
104
+ impl <' a> $imp for & ' a $t {
105
+ type Output = <$t as $imp>:: Output ;
106
+
107
+ #[ inline]
108
+ fn $method( self ) -> <$t as $imp>:: Output {
109
+ $imp:: $method( * self )
110
+ }
111
+ }
112
+ }
113
+ }
114
+
115
+ // implements the binary operator "&T op U"
116
+ // based on "T + U" where T and U are expected `Copy`able
117
+ macro_rules! forward_ref_val_binop {
118
+ ( impl $imp: ident, $method: ident for $t: ty, $u: ty) => {
119
+ #[ unstable]
120
+ impl <' a> $imp<$u> for & ' a $t {
121
+ type Output = <$t as $imp<$u>>:: Output ;
122
+
123
+ #[ inline]
124
+ fn $method( self , other: $u) -> <$t as $imp<$u>>:: Output {
125
+ $imp:: $method( * self , other)
126
+ }
127
+ }
128
+ }
129
+ }
130
+
131
+ // implements the binary operator "T op &U"
132
+ // based on "T + U" where T and U are expected `Copy`able
133
+ macro_rules! forward_val_ref_binop {
134
+ ( impl $imp: ident, $method: ident for $t: ty, $u: ty) => {
135
+ #[ unstable]
136
+ impl <' a> $imp<& ' a $u> for $t {
137
+ type Output = <$t as $imp<$u>>:: Output ;
138
+
139
+ #[ inline]
140
+ fn $method( self , other: & ' a $u) -> <$t as $imp<$u>>:: Output {
141
+ $imp:: $method( self , * other)
142
+ }
143
+ }
144
+ }
145
+ }
146
+
147
+ // implements the binary operator "&T op &U"
148
+ // based on "T + U" where T and U are expected `Copy`able
149
+ macro_rules! forward_ref_ref_binop {
150
+ ( impl $imp: ident, $method: ident for $t: ty, $u: ty) => {
151
+ #[ unstable]
152
+ impl <' a, ' b> $imp<& ' a $u> for & ' b $t {
153
+ type Output = <$t as $imp<$u>>:: Output ;
154
+
155
+ #[ inline]
156
+ fn $method( self , other: & ' a $u) -> <$t as $imp<$u>>:: Output {
157
+ $imp:: $method( * self , * other)
158
+ }
159
+ }
160
+ }
161
+ }
162
+
163
+ // implements binary operators "&T op U", "T op &U", "&T op &U"
164
+ // based on "T + U" where T and U are expected `Copy`able
165
+ macro_rules! forward_ref_binop {
166
+ ( impl $imp: ident, $method: ident for $t: ty, $u: ty) => {
167
+ forward_ref_val_binop! { impl $imp, $method for $t, $u }
168
+ forward_val_ref_binop! { impl $imp, $method for $t, $u }
169
+ forward_ref_ref_binop! { impl $imp, $method for $t, $u }
170
+ }
171
+ }
172
+
99
173
/// The `Add` trait is used to specify the functionality of `+`.
100
174
///
101
175
/// # Example
@@ -144,6 +218,8 @@ macro_rules! add_impl {
144
218
#[ inline]
145
219
fn add( self , other: $t) -> $t { self + other }
146
220
}
221
+
222
+ forward_ref_binop! { impl Add , add for $t, $t }
147
223
) * )
148
224
}
149
225
@@ -197,6 +273,8 @@ macro_rules! sub_impl {
197
273
#[ inline]
198
274
fn sub( self , other: $t) -> $t { self - other }
199
275
}
276
+
277
+ forward_ref_binop! { impl Sub , sub for $t, $t }
200
278
) * )
201
279
}
202
280
@@ -250,6 +328,8 @@ macro_rules! mul_impl {
250
328
#[ inline]
251
329
fn mul( self , other: $t) -> $t { self * other }
252
330
}
331
+
332
+ forward_ref_binop! { impl Mul , mul for $t, $t }
253
333
) * )
254
334
}
255
335
@@ -303,6 +383,8 @@ macro_rules! div_impl {
303
383
#[ inline]
304
384
fn div( self , other: $t) -> $t { self / other }
305
385
}
386
+
387
+ forward_ref_binop! { impl Div , div for $t, $t }
306
388
) * )
307
389
}
308
390
@@ -356,6 +438,8 @@ macro_rules! rem_impl {
356
438
#[ inline]
357
439
fn rem( self , other: $t) -> $t { self % other }
358
440
}
441
+
442
+ forward_ref_binop! { impl Rem , rem for $t, $t }
359
443
) * )
360
444
}
361
445
@@ -371,6 +455,8 @@ macro_rules! rem_float_impl {
371
455
unsafe { $fmod( self , other) }
372
456
}
373
457
}
458
+
459
+ forward_ref_binop! { impl Rem , rem for $t, $t }
374
460
}
375
461
}
376
462
@@ -429,6 +515,8 @@ macro_rules! neg_impl {
429
515
#[ stable]
430
516
fn neg( self ) -> $t { -self }
431
517
}
518
+
519
+ forward_ref_unop! { impl Neg , neg for $t }
432
520
) * )
433
521
}
434
522
@@ -441,6 +529,8 @@ macro_rules! neg_uint_impl {
441
529
#[ inline]
442
530
fn neg( self ) -> $t { -( self as $t_signed) as $t }
443
531
}
532
+
533
+ forward_ref_unop! { impl Neg , neg for $t }
444
534
}
445
535
}
446
536
@@ -502,6 +592,8 @@ macro_rules! not_impl {
502
592
#[ inline]
503
593
fn not( self ) -> $t { !self }
504
594
}
595
+
596
+ forward_ref_unop! { impl Not , not for $t }
505
597
) * )
506
598
}
507
599
@@ -555,6 +647,8 @@ macro_rules! bitand_impl {
555
647
#[ inline]
556
648
fn bitand( self , rhs: $t) -> $t { self & rhs }
557
649
}
650
+
651
+ forward_ref_binop! { impl BitAnd , bitand for $t, $t }
558
652
) * )
559
653
}
560
654
@@ -608,6 +702,8 @@ macro_rules! bitor_impl {
608
702
#[ inline]
609
703
fn bitor( self , rhs: $t) -> $t { self | rhs }
610
704
}
705
+
706
+ forward_ref_binop! { impl BitOr , bitor for $t, $t }
611
707
) * )
612
708
}
613
709
@@ -661,6 +757,8 @@ macro_rules! bitxor_impl {
661
757
#[ inline]
662
758
fn bitxor( self , other: $t) -> $t { self ^ other }
663
759
}
760
+
761
+ forward_ref_binop! { impl BitXor , bitxor for $t, $t }
664
762
) * )
665
763
}
666
764
@@ -716,6 +814,8 @@ macro_rules! shl_impl {
716
814
self << other
717
815
}
718
816
}
817
+
818
+ forward_ref_binop! { impl Shl , shl for $t, $f }
719
819
)
720
820
}
721
821
@@ -795,6 +895,8 @@ macro_rules! shr_impl {
795
895
self >> other
796
896
}
797
897
}
898
+
899
+ forward_ref_binop! { impl Shr , shr for $t, $f }
798
900
)
799
901
}
800
902
0 commit comments