Skip to content

Commit 90eb42d

Browse files
committed
Added const versions of common numeric operations
1 parent 42983a2 commit 90eb42d

File tree

6 files changed

+232
-106
lines changed

6 files changed

+232
-106
lines changed

library/core/src/internal_macros.rs

+68
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ macro_rules! forward_ref_unop {
55
forward_ref_unop!(impl $imp, $method for $t,
66
#[stable(feature = "rust1", since = "1.0.0")]);
77
};
8+
(impl const $imp:ident, $method:ident for $t:ty) => {
9+
forward_ref_unop!(impl const $imp, $method for $t,
10+
#[stable(feature = "rust1", since = "1.0.0")]);
11+
};
12+
(impl const $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
13+
#[$attr]
14+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
15+
impl const $imp for &$t {
16+
type Output = <$t as $imp>::Output;
17+
18+
#[inline]
19+
fn $method(self) -> <$t as $imp>::Output {
20+
$imp::$method(*self)
21+
}
22+
}
23+
};
824
(impl $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
925
#[$attr]
1026
impl $imp for &$t {
@@ -25,6 +41,44 @@ macro_rules! forward_ref_binop {
2541
forward_ref_binop!(impl $imp, $method for $t, $u,
2642
#[stable(feature = "rust1", since = "1.0.0")]);
2743
};
44+
(impl const $imp:ident, $method:ident for $t:ty, $u:ty) => {
45+
forward_ref_binop!(impl const $imp, $method for $t, $u,
46+
#[stable(feature = "rust1", since = "1.0.0")]);
47+
};
48+
(impl const $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
49+
#[$attr]
50+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
51+
impl<'a> const $imp<$u> for &'a $t {
52+
type Output = <$t as $imp<$u>>::Output;
53+
54+
#[inline]
55+
fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
56+
$imp::$method(*self, other)
57+
}
58+
}
59+
60+
#[$attr]
61+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
62+
impl const $imp<&$u> for $t {
63+
type Output = <$t as $imp<$u>>::Output;
64+
65+
#[inline]
66+
fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
67+
$imp::$method(self, *other)
68+
}
69+
}
70+
71+
#[$attr]
72+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
73+
impl const $imp<&$u> for &$t {
74+
type Output = <$t as $imp<$u>>::Output;
75+
76+
#[inline]
77+
fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
78+
$imp::$method(*self, *other)
79+
}
80+
}
81+
};
2882
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
2983
#[$attr]
3084
impl<'a> $imp<$u> for &'a $t {
@@ -65,6 +119,20 @@ macro_rules! forward_ref_op_assign {
65119
forward_ref_op_assign!(impl $imp, $method for $t, $u,
66120
#[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
67121
};
122+
(impl const $imp:ident, $method:ident for $t:ty, $u:ty) => {
123+
forward_ref_op_assign!(impl const $imp, $method for $t, $u,
124+
#[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
125+
};
126+
(impl const $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
127+
#[$attr]
128+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
129+
impl const $imp<&$u> for $t {
130+
#[inline]
131+
fn $method(&mut self, other: &$u) {
132+
$imp::$method(self, *other);
133+
}
134+
}
135+
};
68136
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
69137
#[$attr]
70138
impl $imp<&$u> for $t {

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
#![feature(const_likely)]
116116
#![feature(const_maybe_uninit_as_ptr)]
117117
#![feature(const_maybe_uninit_assume_init)]
118+
#![feature(const_ops)]
118119
#![feature(const_option)]
119120
#![feature(const_pin)]
120121
#![feature(const_replace)]

library/core/src/num/nonzero.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ macro_rules! nonzero_integers {
9292
}
9393

9494
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
95-
impl BitOr for $Ty {
95+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
96+
impl const BitOr for $Ty {
9697
type Output = Self;
9798
#[inline]
9899
fn bitor(self, rhs: Self) -> Self::Output {
@@ -103,7 +104,8 @@ macro_rules! nonzero_integers {
103104
}
104105

105106
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
106-
impl BitOr<$Int> for $Ty {
107+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
108+
impl const BitOr<$Int> for $Ty {
107109
type Output = Self;
108110
#[inline]
109111
fn bitor(self, rhs: $Int) -> Self::Output {
@@ -115,7 +117,8 @@ macro_rules! nonzero_integers {
115117
}
116118

117119
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
118-
impl BitOr<$Ty> for $Int {
120+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
121+
impl const BitOr<$Ty> for $Int {
119122
type Output = $Ty;
120123
#[inline]
121124
fn bitor(self, rhs: $Ty) -> Self::Output {
@@ -127,15 +130,17 @@ macro_rules! nonzero_integers {
127130
}
128131

129132
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
130-
impl BitOrAssign for $Ty {
133+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
134+
impl const BitOrAssign for $Ty {
131135
#[inline]
132136
fn bitor_assign(&mut self, rhs: Self) {
133137
*self = *self | rhs;
134138
}
135139
}
136140

137141
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
138-
impl BitOrAssign<$Int> for $Ty {
142+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
143+
impl const BitOrAssign<$Int> for $Ty {
139144
#[inline]
140145
fn bitor_assign(&mut self, rhs: $Int) {
141146
*self = *self | rhs;
@@ -257,7 +262,8 @@ macro_rules! nonzero_integers_div {
257262
( $( $Ty: ident($Int: ty); )+ ) => {
258263
$(
259264
#[stable(feature = "nonzero_div", since = "1.51.0")]
260-
impl Div<$Ty> for $Int {
265+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
266+
impl const Div<$Ty> for $Int {
261267
type Output = $Int;
262268
/// This operation rounds towards zero,
263269
/// truncating any fractional part of the exact result, and cannot panic.
@@ -270,7 +276,8 @@ macro_rules! nonzero_integers_div {
270276
}
271277

272278
#[stable(feature = "nonzero_div", since = "1.51.0")]
273-
impl Rem<$Ty> for $Int {
279+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
280+
impl const Rem<$Ty> for $Int {
274281
type Output = $Int;
275282
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
276283
#[inline]

0 commit comments

Comments
 (0)