Skip to content

Commit d7a8770

Browse files
committed
Constify missing reasonable implementations
1 parent 2235f6f commit d7a8770

File tree

5 files changed

+218
-90
lines changed

5 files changed

+218
-90
lines changed

library/core/src/internal_macros.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// implements the unary operator "op &T"
22
// based on "op T" where T is expected to be `Copy`able
33
macro_rules! forward_ref_unop {
4-
(impl $imp:ident, $method:ident for $t:ty, $(#[$attr:meta])+) => {
4+
// FIXME: use macro_named_capture_groups to directly do impl $const(const)? instead
5+
(impl $imp:ident, $method:ident for $t:ty, $(#[$attr:meta])+ $(, $const:tt)?) => {
56
$(#[$attr])+
6-
impl $imp for &$t {
7+
impl $($const)? $imp for &$t {
78
type Output = <$t as $imp>::Output;
89

910
#[inline]
@@ -17,9 +18,10 @@ macro_rules! forward_ref_unop {
1718
// implements binary operators "&T op U", "T op &U", "&T op &U"
1819
// based on "T op U" where T and U are expected to be `Copy`able
1920
macro_rules! forward_ref_binop {
20-
(impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+) => {
21+
// FIXME: use macro_named_capture_groups to directly do impl $const(const)? instead
22+
(impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+ $(, $const:tt)?) => {
2123
$(#[$attr])+
22-
impl<'a> $imp<$u> for &'a $t {
24+
impl<'a> $($const)? $imp<$u> for &'a $t {
2325
type Output = <$t as $imp<$u>>::Output;
2426

2527
#[inline]
@@ -30,7 +32,7 @@ macro_rules! forward_ref_binop {
3032
}
3133

3234
$(#[$attr])+
33-
impl $imp<&$u> for $t {
35+
impl $($const)? $imp<&$u> for $t {
3436
type Output = <$t as $imp<$u>>::Output;
3537

3638
#[inline]
@@ -41,7 +43,7 @@ macro_rules! forward_ref_binop {
4143
}
4244

4345
$(#[$attr])+
44-
impl $imp<&$u> for &$t {
46+
impl $($const)? $imp<&$u> for &$t {
4547
type Output = <$t as $imp<$u>>::Output;
4648

4749
#[inline]
@@ -56,9 +58,10 @@ macro_rules! forward_ref_binop {
5658
// implements "T op= &U", based on "T op= U"
5759
// where U is expected to be `Copy`able
5860
macro_rules! forward_ref_op_assign {
59-
(impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+) => {
61+
// FIXME: use macro_named_capture_groups to directly do impl $const(const)? instead
62+
(impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+ $(, $const:tt)?) => {
6063
$(#[$attr])+
61-
impl $imp<&$u> for $t {
64+
impl $($const)? $imp<&$u> for $t {
6265
#[inline]
6366
#[track_caller]
6467
fn $method(&mut self, other: &$u) {

library/core/src/num/saturating.rs

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ impl<T: fmt::UpperHex> fmt::UpperHex for Saturating<T> {
213213
macro_rules! saturating_impl {
214214
($($t:ty)*) => ($(
215215
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
216-
impl Add for Saturating<$t> {
216+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
217+
impl const Add for Saturating<$t> {
217218
type Output = Saturating<$t>;
218219

219220
#[inline]
@@ -222,30 +223,39 @@ macro_rules! saturating_impl {
222223
}
223224
}
224225
forward_ref_binop! { impl Add, add for Saturating<$t>, Saturating<$t>,
225-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
226+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
227+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
228+
const }
226229

227230
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
228-
impl AddAssign for Saturating<$t> {
231+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
232+
impl const AddAssign for Saturating<$t> {
229233
#[inline]
230234
fn add_assign(&mut self, other: Saturating<$t>) {
231235
*self = *self + other;
232236
}
233237
}
234238
forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, Saturating<$t>,
235-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
239+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
240+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
241+
const }
236242

237243
#[stable(feature = "saturating_int_assign_impl", since = "1.74.0")]
238-
impl AddAssign<$t> for Saturating<$t> {
244+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
245+
impl const AddAssign<$t> for Saturating<$t> {
239246
#[inline]
240247
fn add_assign(&mut self, other: $t) {
241248
*self = *self + Saturating(other);
242249
}
243250
}
244251
forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, $t,
245-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
252+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
253+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
254+
const }
246255

247256
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
248-
impl Sub for Saturating<$t> {
257+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
258+
impl const Sub for Saturating<$t> {
249259
type Output = Saturating<$t>;
250260

251261
#[inline]
@@ -254,30 +264,39 @@ macro_rules! saturating_impl {
254264
}
255265
}
256266
forward_ref_binop! { impl Sub, sub for Saturating<$t>, Saturating<$t>,
257-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
267+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
268+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
269+
const }
258270

259271
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
260-
impl SubAssign for Saturating<$t> {
272+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
273+
impl const SubAssign for Saturating<$t> {
261274
#[inline]
262275
fn sub_assign(&mut self, other: Saturating<$t>) {
263276
*self = *self - other;
264277
}
265278
}
266279
forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, Saturating<$t>,
267-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
280+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
281+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
282+
const }
268283

269284
#[stable(feature = "saturating_int_assign_impl", since = "1.74.0")]
270-
impl SubAssign<$t> for Saturating<$t> {
285+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
286+
impl const SubAssign<$t> for Saturating<$t> {
271287
#[inline]
272288
fn sub_assign(&mut self, other: $t) {
273289
*self = *self - Saturating(other);
274290
}
275291
}
276292
forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, $t,
277-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
293+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
294+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
295+
const }
278296

279297
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
280-
impl Mul for Saturating<$t> {
298+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
299+
impl const Mul for Saturating<$t> {
281300
type Output = Saturating<$t>;
282301

283302
#[inline]
@@ -286,27 +305,35 @@ macro_rules! saturating_impl {
286305
}
287306
}
288307
forward_ref_binop! { impl Mul, mul for Saturating<$t>, Saturating<$t>,
289-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
308+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
309+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
310+
const }
290311

291312
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
292-
impl MulAssign for Saturating<$t> {
313+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
314+
impl const MulAssign for Saturating<$t> {
293315
#[inline]
294316
fn mul_assign(&mut self, other: Saturating<$t>) {
295317
*self = *self * other;
296318
}
297319
}
298320
forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, Saturating<$t>,
299-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
321+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
322+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
323+
const }
300324

301325
#[stable(feature = "saturating_int_assign_impl", since = "1.74.0")]
302-
impl MulAssign<$t> for Saturating<$t> {
326+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
327+
impl const MulAssign<$t> for Saturating<$t> {
303328
#[inline]
304329
fn mul_assign(&mut self, other: $t) {
305330
*self = *self * Saturating(other);
306331
}
307332
}
308333
forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, $t,
309-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
334+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
335+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
336+
const }
310337

311338
/// # Examples
312339
///
@@ -324,7 +351,8 @@ macro_rules! saturating_impl {
324351
#[doc = concat!("let _ = Saturating(0", stringify!($t), ") / Saturating(0);")]
325352
/// ```
326353
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
327-
impl Div for Saturating<$t> {
354+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
355+
impl const Div for Saturating<$t> {
328356
type Output = Saturating<$t>;
329357

330358
#[inline]
@@ -333,30 +361,39 @@ macro_rules! saturating_impl {
333361
}
334362
}
335363
forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>,
336-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
364+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
365+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
366+
const }
337367

338368
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
339-
impl DivAssign for Saturating<$t> {
369+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
370+
impl const DivAssign for Saturating<$t> {
340371
#[inline]
341372
fn div_assign(&mut self, other: Saturating<$t>) {
342373
*self = *self / other;
343374
}
344375
}
345376
forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, Saturating<$t>,
346-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
377+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
378+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
379+
const }
347380

348381
#[stable(feature = "saturating_int_assign_impl", since = "1.74.0")]
349-
impl DivAssign<$t> for Saturating<$t> {
382+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
383+
impl const DivAssign<$t> for Saturating<$t> {
350384
#[inline]
351385
fn div_assign(&mut self, other: $t) {
352386
*self = *self / Saturating(other);
353387
}
354388
}
355389
forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, $t,
356-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
390+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
391+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
392+
const }
357393

358394
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
359-
impl Rem for Saturating<$t> {
395+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
396+
impl const Rem for Saturating<$t> {
360397
type Output = Saturating<$t>;
361398

362399
#[inline]
@@ -365,27 +402,35 @@ macro_rules! saturating_impl {
365402
}
366403
}
367404
forward_ref_binop! { impl Rem, rem for Saturating<$t>, Saturating<$t>,
368-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
405+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
406+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
407+
const }
369408

370409
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
371-
impl RemAssign for Saturating<$t> {
410+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
411+
impl const RemAssign for Saturating<$t> {
372412
#[inline]
373413
fn rem_assign(&mut self, other: Saturating<$t>) {
374414
*self = *self % other;
375415
}
376416
}
377417
forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, Saturating<$t>,
378-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
418+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
419+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
420+
const }
379421

380422
#[stable(feature = "saturating_int_assign_impl", since = "1.74.0")]
381-
impl RemAssign<$t> for Saturating<$t> {
423+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")]
424+
impl const RemAssign<$t> for Saturating<$t> {
382425
#[inline]
383426
fn rem_assign(&mut self, other: $t) {
384427
*self = *self % Saturating(other);
385428
}
386429
}
387430
forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, $t,
388-
#[stable(feature = "saturating_int_impl", since = "1.74.0")] }
431+
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
432+
#[rustc_const_unstable(feature = "const_arith_ops", issue = "143802")],
433+
const }
389434

390435
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
391436
impl Not for Saturating<$t> {

0 commit comments

Comments
 (0)