Skip to content

Commit b5a8a41

Browse files
committed
Constify remaining operators
1 parent 74f23e2 commit b5a8a41

File tree

9 files changed

+442
-216
lines changed

9 files changed

+442
-216
lines changed

library/core/src/internal_macros.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
macro_rules! forward_ref_unop {
44
(impl $imp:ident, $method:ident for $t:ty, $(#[$attr:meta])+) => {
55
$(#[$attr])+
6-
impl $imp for &$t {
6+
impl const $imp for &$t {
77
type Output = <$t as $imp>::Output;
88

99
#[inline]
@@ -19,7 +19,7 @@ macro_rules! forward_ref_unop {
1919
macro_rules! forward_ref_binop {
2020
(impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+) => {
2121
$(#[$attr])+
22-
impl<'a> $imp<$u> for &'a $t {
22+
impl const $imp<$u> for &$t {
2323
type Output = <$t as $imp<$u>>::Output;
2424

2525
#[inline]
@@ -30,7 +30,7 @@ macro_rules! forward_ref_binop {
3030
}
3131

3232
$(#[$attr])+
33-
impl $imp<&$u> for $t {
33+
impl const $imp<&$u> for $t {
3434
type Output = <$t as $imp<$u>>::Output;
3535

3636
#[inline]
@@ -41,7 +41,7 @@ macro_rules! forward_ref_binop {
4141
}
4242

4343
$(#[$attr])+
44-
impl $imp<&$u> for &$t {
44+
impl const $imp<&$u> for &$t {
4545
type Output = <$t as $imp<$u>>::Output;
4646

4747
#[inline]
@@ -58,7 +58,7 @@ macro_rules! forward_ref_binop {
5858
macro_rules! forward_ref_op_assign {
5959
(impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+) => {
6060
$(#[$attr])+
61-
impl $imp<&$u> for $t {
61+
impl const $imp<&$u> for $t {
6262
#[inline]
6363
#[track_caller]
6464
fn $method(&mut self, other: &$u) {

library/core/src/net/ip_addr.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::display_buffer::DisplayBuffer;
22
use crate::cmp::Ordering;
33
use crate::fmt::{self, Write};
44
use crate::hash::{Hash, Hasher};
5-
use crate::iter;
65
use crate::mem::transmute;
76
use crate::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
87

@@ -2336,20 +2335,24 @@ impl From<[u16; 8]> for IpAddr {
23362335
}
23372336

23382337
#[stable(feature = "ip_bitops", since = "1.75.0")]
2339-
impl Not for Ipv4Addr {
2338+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2339+
impl const Not for Ipv4Addr {
23402340
type Output = Ipv4Addr;
23412341

23422342
#[inline]
23432343
fn not(mut self) -> Ipv4Addr {
2344-
for octet in &mut self.octets {
2345-
*octet = !*octet;
2344+
let mut idx = 0;
2345+
while idx < 4 {
2346+
self.octets[idx] = !self.octets[idx];
2347+
idx += 1;
23462348
}
23472349
self
23482350
}
23492351
}
23502352

23512353
#[stable(feature = "ip_bitops", since = "1.75.0")]
2352-
impl Not for &'_ Ipv4Addr {
2354+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2355+
impl const Not for &'_ Ipv4Addr {
23532356
type Output = Ipv4Addr;
23542357

23552358
#[inline]
@@ -2359,20 +2362,24 @@ impl Not for &'_ Ipv4Addr {
23592362
}
23602363

23612364
#[stable(feature = "ip_bitops", since = "1.75.0")]
2362-
impl Not for Ipv6Addr {
2365+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2366+
impl const Not for Ipv6Addr {
23632367
type Output = Ipv6Addr;
23642368

23652369
#[inline]
23662370
fn not(mut self) -> Ipv6Addr {
2367-
for octet in &mut self.octets {
2368-
*octet = !*octet;
2371+
let mut idx = 0;
2372+
while idx < 16 {
2373+
self.octets[idx] = !self.octets[idx];
2374+
idx += 1;
23692375
}
23702376
self
23712377
}
23722378
}
23732379

23742380
#[stable(feature = "ip_bitops", since = "1.75.0")]
2375-
impl Not for &'_ Ipv6Addr {
2381+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2382+
impl const Not for &'_ Ipv6Addr {
23762383
type Output = Ipv6Addr;
23772384

23782385
#[inline]
@@ -2388,23 +2395,25 @@ macro_rules! bitop_impls {
23882395
)*) => {
23892396
$(
23902397
$(#[$attr])*
2391-
impl $BitOpAssign for $ty {
2398+
impl const $BitOpAssign for $ty {
23922399
fn $bitop_assign(&mut self, rhs: $ty) {
2393-
for (lhs, rhs) in iter::zip(&mut self.octets, rhs.octets) {
2394-
lhs.$bitop_assign(rhs);
2400+
let mut idx = 0;
2401+
while idx < self.octets.len() {
2402+
self.octets[idx].$bitop_assign(rhs.octets[idx]);
2403+
idx += 1;
23952404
}
23962405
}
23972406
}
23982407

23992408
$(#[$attr])*
2400-
impl $BitOpAssign<&'_ $ty> for $ty {
2409+
impl const $BitOpAssign<&'_ $ty> for $ty {
24012410
fn $bitop_assign(&mut self, rhs: &'_ $ty) {
24022411
self.$bitop_assign(*rhs);
24032412
}
24042413
}
24052414

24062415
$(#[$attr])*
2407-
impl $BitOp for $ty {
2416+
impl const $BitOp for $ty {
24082417
type Output = $ty;
24092418

24102419
#[inline]
@@ -2415,7 +2424,7 @@ macro_rules! bitop_impls {
24152424
}
24162425

24172426
$(#[$attr])*
2418-
impl $BitOp<&'_ $ty> for $ty {
2427+
impl const $BitOp<&'_ $ty> for $ty {
24192428
type Output = $ty;
24202429

24212430
#[inline]
@@ -2426,7 +2435,7 @@ macro_rules! bitop_impls {
24262435
}
24272436

24282437
$(#[$attr])*
2429-
impl $BitOp<$ty> for &'_ $ty {
2438+
impl const $BitOp<$ty> for &'_ $ty {
24302439
type Output = $ty;
24312440

24322441
#[inline]
@@ -2438,7 +2447,7 @@ macro_rules! bitop_impls {
24382447
}
24392448

24402449
$(#[$attr])*
2441-
impl $BitOp<&'_ $ty> for &'_ $ty {
2450+
impl const $BitOp<&'_ $ty> for &'_ $ty {
24422451
type Output = $ty;
24432452

24442453
#[inline]
@@ -2454,12 +2463,16 @@ macro_rules! bitop_impls {
24542463

24552464
bitop_impls! {
24562465
#[stable(feature = "ip_bitops", since = "1.75.0")]
2466+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
24572467
impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign);
24582468
#[stable(feature = "ip_bitops", since = "1.75.0")]
2469+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
24592470
impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign);
24602471

24612472
#[stable(feature = "ip_bitops", since = "1.75.0")]
2473+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
24622474
impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign);
24632475
#[stable(feature = "ip_bitops", since = "1.75.0")]
2476+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
24642477
impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign);
24652478
}

library/core/src/num/nonzero.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<T> Copy for NonZero<T> where T: ZeroablePrimitive {}
203203
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
204204
impl<T> const PartialEq for NonZero<T>
205205
where
206-
T: ZeroablePrimitive + ~const PartialEq,
206+
T: ZeroablePrimitive + [const] PartialEq,
207207
{
208208
#[inline]
209209
fn eq(&self, other: &Self) -> bool {
@@ -309,9 +309,10 @@ where
309309
}
310310

311311
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
312-
impl<T> BitOr for NonZero<T>
312+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
313+
impl<T> const BitOr for NonZero<T>
313314
where
314-
T: ZeroablePrimitive + BitOr<Output = T>,
315+
T: ZeroablePrimitive + [const] BitOr<Output = T>,
315316
{
316317
type Output = Self;
317318

@@ -323,9 +324,10 @@ where
323324
}
324325

325326
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
326-
impl<T> BitOr<T> for NonZero<T>
327+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
328+
impl<T> const BitOr<T> for NonZero<T>
327329
where
328-
T: ZeroablePrimitive + BitOr<Output = T>,
330+
T: ZeroablePrimitive + [const] BitOr<Output = T>,
329331
{
330332
type Output = Self;
331333

@@ -337,9 +339,10 @@ where
337339
}
338340

339341
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
340-
impl<T> BitOr<NonZero<T>> for T
342+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
343+
impl<T> const BitOr<NonZero<T>> for T
341344
where
342-
T: ZeroablePrimitive + BitOr<Output = T>,
345+
T: ZeroablePrimitive + [const] BitOr<Output = T>,
343346
{
344347
type Output = NonZero<T>;
345348

@@ -351,10 +354,11 @@ where
351354
}
352355

353356
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
354-
impl<T> BitOrAssign for NonZero<T>
357+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
358+
impl<T> const BitOrAssign for NonZero<T>
355359
where
356360
T: ZeroablePrimitive,
357-
Self: BitOr<Output = Self>,
361+
Self: [const] BitOr<Output = Self>,
358362
{
359363
#[inline]
360364
fn bitor_assign(&mut self, rhs: Self) {
@@ -363,10 +367,11 @@ where
363367
}
364368

365369
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
366-
impl<T> BitOrAssign<T> for NonZero<T>
370+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
371+
impl<T> const BitOrAssign<T> for NonZero<T>
367372
where
368373
T: ZeroablePrimitive,
369-
Self: BitOr<T, Output = Self>,
374+
Self: [const] BitOr<T, Output = Self>,
370375
{
371376
#[inline]
372377
fn bitor_assign(&mut self, rhs: T) {
@@ -1238,7 +1243,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
12381243
// Impls for unsigned nonzero types only.
12391244
(unsigned $Int:ty) => {
12401245
#[stable(feature = "nonzero_div", since = "1.51.0")]
1241-
impl Div<NonZero<$Int>> for $Int {
1246+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1247+
impl const Div<NonZero<$Int>> for $Int {
12421248
type Output = $Int;
12431249

12441250
/// Same as `self / other.get()`, but because `other` is a `NonZero<_>`,
@@ -1256,7 +1262,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
12561262
}
12571263

12581264
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1259-
impl DivAssign<NonZero<$Int>> for $Int {
1265+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1266+
impl const DivAssign<NonZero<$Int>> for $Int {
12601267
/// Same as `self /= other.get()`, but because `other` is a `NonZero<_>`,
12611268
/// there's never a runtime check for division-by-zero.
12621269
///
@@ -1269,7 +1276,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
12691276
}
12701277

12711278
#[stable(feature = "nonzero_div", since = "1.51.0")]
1272-
impl Rem<NonZero<$Int>> for $Int {
1279+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1280+
impl const Rem<NonZero<$Int>> for $Int {
12731281
type Output = $Int;
12741282

12751283
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
@@ -1282,7 +1290,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
12821290
}
12831291

12841292
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1285-
impl RemAssign<NonZero<$Int>> for $Int {
1293+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1294+
impl const RemAssign<NonZero<$Int>> for $Int {
12861295
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
12871296
#[inline]
12881297
fn rem_assign(&mut self, other: NonZero<$Int>) {
@@ -1322,7 +1331,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
13221331
// Impls for signed nonzero types only.
13231332
(signed $Int:ty) => {
13241333
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1325-
impl Neg for NonZero<$Int> {
1334+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1335+
impl const Neg for NonZero<$Int> {
13261336
type Output = Self;
13271337

13281338
#[inline]
@@ -1333,7 +1343,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
13331343
}
13341344

13351345
forward_ref_unop! { impl Neg, neg for NonZero<$Int>,
1336-
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")] }
1346+
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1347+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
13371348
};
13381349
}
13391350

0 commit comments

Comments
 (0)