Skip to content

Commit 5960968

Browse files
Allow T op= &T for built-in numeric types T
Currently, there are 4 ways to add two i32's together: a + b, a + &b, &a + b and &a + &b. The same is possible for all other binary operators and for all built-in numeric types that support those binary operators. Similarly, unary operators also have both a ref and a non-ref version, e.g., -7 == -&7. However, the assignment versions of these binary operators don't allow a ref right-hand side. This commit fixes that inconsistency by implementing these operators. These changes should be fully backwards compatible.
1 parent 13fd5e9 commit 5960968

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/libcore/internal_macros.rs

+18
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,21 @@ macro_rules! forward_ref_binop {
6868
}
6969
}
7070
}
71+
72+
// implements "T op= &U", based on "T op= U"
73+
// where U is expected to be `Copy`able
74+
macro_rules! forward_ref_op_assign {
75+
(impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
76+
forward_ref_op_assign!(impl $imp, $method for $t, $u,
77+
#[stable(feature = "op_assign_builtins_by_ref", since = "1.18.0")]);
78+
};
79+
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
80+
#[$attr]
81+
impl<'a> $imp<&'a $u> for $t {
82+
#[inline]
83+
fn $method(&mut self, other: &'a $u) {
84+
$imp::$method(self, *other);
85+
}
86+
}
87+
}
88+
}

src/libcore/ops.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,8 @@ macro_rules! add_assign_impl {
13461346
#[rustc_inherit_overflow_checks]
13471347
fn add_assign(&mut self, other: $t) { *self += other }
13481348
}
1349+
1350+
forward_ref_op_assign! { impl AddAssign, add_assign for $t, $t }
13491351
)+)
13501352
}
13511353

@@ -1403,6 +1405,8 @@ macro_rules! sub_assign_impl {
14031405
#[rustc_inherit_overflow_checks]
14041406
fn sub_assign(&mut self, other: $t) { *self -= other }
14051407
}
1408+
1409+
forward_ref_op_assign! { impl SubAssign, sub_assign for $t, $t }
14061410
)+)
14071411
}
14081412

@@ -1449,6 +1453,8 @@ macro_rules! mul_assign_impl {
14491453
#[rustc_inherit_overflow_checks]
14501454
fn mul_assign(&mut self, other: $t) { *self *= other }
14511455
}
1456+
1457+
forward_ref_op_assign! { impl MulAssign, mul_assign for $t, $t }
14521458
)+)
14531459
}
14541460

@@ -1494,6 +1500,8 @@ macro_rules! div_assign_impl {
14941500
#[inline]
14951501
fn div_assign(&mut self, other: $t) { *self /= other }
14961502
}
1503+
1504+
forward_ref_op_assign! { impl DivAssign, div_assign for $t, $t }
14971505
)+)
14981506
}
14991507

@@ -1539,6 +1547,8 @@ macro_rules! rem_assign_impl {
15391547
#[inline]
15401548
fn rem_assign(&mut self, other: $t) { *self %= other }
15411549
}
1550+
1551+
forward_ref_op_assign! { impl RemAssign, rem_assign for $t, $t }
15421552
)+)
15431553
}
15441554

@@ -1626,6 +1636,8 @@ macro_rules! bitand_assign_impl {
16261636
#[inline]
16271637
fn bitand_assign(&mut self, other: $t) { *self &= other }
16281638
}
1639+
1640+
forward_ref_op_assign! { impl BitAndAssign, bitand_assign for $t, $t }
16291641
)+)
16301642
}
16311643

@@ -1671,6 +1683,8 @@ macro_rules! bitor_assign_impl {
16711683
#[inline]
16721684
fn bitor_assign(&mut self, other: $t) { *self |= other }
16731685
}
1686+
1687+
forward_ref_op_assign! { impl BitOrAssign, bitor_assign for $t, $t }
16741688
)+)
16751689
}
16761690

@@ -1716,6 +1730,8 @@ macro_rules! bitxor_assign_impl {
17161730
#[inline]
17171731
fn bitxor_assign(&mut self, other: $t) { *self ^= other }
17181732
}
1733+
1734+
forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for $t, $t }
17191735
)+)
17201736
}
17211737

@@ -1764,6 +1780,8 @@ macro_rules! shl_assign_impl {
17641780
*self <<= other
17651781
}
17661782
}
1783+
1784+
forward_ref_op_assign! { impl ShlAssign, shl_assign for $t, $f }
17671785
)
17681786
}
17691787

@@ -1830,6 +1848,8 @@ macro_rules! shr_assign_impl {
18301848
*self >>= other
18311849
}
18321850
}
1851+
1852+
forward_ref_op_assign! { impl ShrAssign, shr_assign for $t, $f }
18331853
)
18341854
}
18351855

0 commit comments

Comments
 (0)