Skip to content

Commit 5eb254b

Browse files
committed
libcore: implement many operators for references as well
1 parent 8903c21 commit 5eb254b

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

src/libcore/ops.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,80 @@ pub trait Drop {
9696
fn drop(&mut self);
9797
}
9898

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+
99173
/// The `Add` trait is used to specify the functionality of `+`.
100174
///
101175
/// # Example
@@ -144,6 +218,8 @@ macro_rules! add_impl {
144218
#[inline]
145219
fn add(self, other: $t) -> $t { self + other }
146220
}
221+
222+
forward_ref_binop! { impl Add, add for $t, $t }
147223
)*)
148224
}
149225

@@ -197,6 +273,8 @@ macro_rules! sub_impl {
197273
#[inline]
198274
fn sub(self, other: $t) -> $t { self - other }
199275
}
276+
277+
forward_ref_binop! { impl Sub, sub for $t, $t }
200278
)*)
201279
}
202280

@@ -250,6 +328,8 @@ macro_rules! mul_impl {
250328
#[inline]
251329
fn mul(self, other: $t) -> $t { self * other }
252330
}
331+
332+
forward_ref_binop! { impl Mul, mul for $t, $t }
253333
)*)
254334
}
255335

@@ -303,6 +383,8 @@ macro_rules! div_impl {
303383
#[inline]
304384
fn div(self, other: $t) -> $t { self / other }
305385
}
386+
387+
forward_ref_binop! { impl Div, div for $t, $t }
306388
)*)
307389
}
308390

@@ -356,6 +438,8 @@ macro_rules! rem_impl {
356438
#[inline]
357439
fn rem(self, other: $t) -> $t { self % other }
358440
}
441+
442+
forward_ref_binop! { impl Rem, rem for $t, $t }
359443
)*)
360444
}
361445

@@ -371,6 +455,8 @@ macro_rules! rem_float_impl {
371455
unsafe { $fmod(self, other) }
372456
}
373457
}
458+
459+
forward_ref_binop! { impl Rem, rem for $t, $t }
374460
}
375461
}
376462

@@ -429,6 +515,8 @@ macro_rules! neg_impl {
429515
#[stable]
430516
fn neg(self) -> $t { -self }
431517
}
518+
519+
forward_ref_unop! { impl Neg, neg for $t }
432520
)*)
433521
}
434522

@@ -441,6 +529,8 @@ macro_rules! neg_uint_impl {
441529
#[inline]
442530
fn neg(self) -> $t { -(self as $t_signed) as $t }
443531
}
532+
533+
forward_ref_unop! { impl Neg, neg for $t }
444534
}
445535
}
446536

@@ -502,6 +592,8 @@ macro_rules! not_impl {
502592
#[inline]
503593
fn not(self) -> $t { !self }
504594
}
595+
596+
forward_ref_unop! { impl Not, not for $t }
505597
)*)
506598
}
507599

@@ -555,6 +647,8 @@ macro_rules! bitand_impl {
555647
#[inline]
556648
fn bitand(self, rhs: $t) -> $t { self & rhs }
557649
}
650+
651+
forward_ref_binop! { impl BitAnd, bitand for $t, $t }
558652
)*)
559653
}
560654

@@ -608,6 +702,8 @@ macro_rules! bitor_impl {
608702
#[inline]
609703
fn bitor(self, rhs: $t) -> $t { self | rhs }
610704
}
705+
706+
forward_ref_binop! { impl BitOr, bitor for $t, $t }
611707
)*)
612708
}
613709

@@ -661,6 +757,8 @@ macro_rules! bitxor_impl {
661757
#[inline]
662758
fn bitxor(self, other: $t) -> $t { self ^ other }
663759
}
760+
761+
forward_ref_binop! { impl BitXor, bitxor for $t, $t }
664762
)*)
665763
}
666764

@@ -716,6 +814,8 @@ macro_rules! shl_impl {
716814
self << other
717815
}
718816
}
817+
818+
forward_ref_binop! { impl Shl, shl for $t, $f }
719819
)
720820
}
721821

@@ -795,6 +895,8 @@ macro_rules! shr_impl {
795895
self >> other
796896
}
797897
}
898+
899+
forward_ref_binop! { impl Shr, shr for $t, $f }
798900
)
799901
}
800902

0 commit comments

Comments
 (0)