Skip to content

Commit 207d52a

Browse files
committed
Constify remaining operators
1 parent 600a02c commit 207d52a

File tree

9 files changed

+426
-205
lines changed

9 files changed

+426
-205
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

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

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

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

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

23562359
#[inline]
@@ -2360,20 +2363,24 @@ impl Not for &'_ Ipv4Addr {
23602363
}
23612364

23622365
#[stable(feature = "ip_bitops", since = "1.75.0")]
2363-
impl Not for Ipv6Addr {
2366+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2367+
impl const Not for Ipv6Addr {
23642368
type Output = Ipv6Addr;
23652369

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

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

23792386
#[inline]
@@ -2389,23 +2396,25 @@ macro_rules! bitop_impls {
23892396
)*) => {
23902397
$(
23912398
$(#[$attr])*
2392-
impl $BitOpAssign for $ty {
2399+
impl const $BitOpAssign for $ty {
23932400
fn $bitop_assign(&mut self, rhs: $ty) {
2394-
for (lhs, rhs) in iter::zip(&mut self.octets, rhs.octets) {
2395-
lhs.$bitop_assign(rhs);
2401+
let mut idx = 0;
2402+
while idx < self.octets.len() {
2403+
self.octets[idx].$bitop_assign(rhs.octets[idx]);
2404+
idx += 1;
23962405
}
23972406
}
23982407
}
23992408

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

24072416
$(#[$attr])*
2408-
impl $BitOp for $ty {
2417+
impl const $BitOp for $ty {
24092418
type Output = $ty;
24102419

24112420
#[inline]
@@ -2416,7 +2425,7 @@ macro_rules! bitop_impls {
24162425
}
24172426

24182427
$(#[$attr])*
2419-
impl $BitOp<&'_ $ty> for $ty {
2428+
impl const $BitOp<&'_ $ty> for $ty {
24202429
type Output = $ty;
24212430

24222431
#[inline]
@@ -2427,7 +2436,7 @@ macro_rules! bitop_impls {
24272436
}
24282437

24292438
$(#[$attr])*
2430-
impl $BitOp<$ty> for &'_ $ty {
2439+
impl const $BitOp<$ty> for &'_ $ty {
24312440
type Output = $ty;
24322441

24332442
#[inline]
@@ -2439,7 +2448,7 @@ macro_rules! bitop_impls {
24392448
}
24402449

24412450
$(#[$attr])*
2442-
impl $BitOp<&'_ $ty> for &'_ $ty {
2451+
impl const $BitOp<&'_ $ty> for &'_ $ty {
24432452
type Output = $ty;
24442453

24452454
#[inline]
@@ -2455,12 +2464,16 @@ macro_rules! bitop_impls {
24552464

24562465
bitop_impls! {
24572466
#[stable(feature = "ip_bitops", since = "1.75.0")]
2467+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
24582468
impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign);
24592469
#[stable(feature = "ip_bitops", since = "1.75.0")]
2470+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
24602471
impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign);
24612472

24622473
#[stable(feature = "ip_bitops", since = "1.75.0")]
2474+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
24632475
impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign);
24642476
#[stable(feature = "ip_bitops", since = "1.75.0")]
2477+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
24652478
impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign);
24662479
}

library/core/src/num/nonzero.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
12381238
// Impls for unsigned nonzero types only.
12391239
(unsigned $Int:ty) => {
12401240
#[stable(feature = "nonzero_div", since = "1.51.0")]
1241-
impl Div<NonZero<$Int>> for $Int {
1241+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1242+
impl const Div<NonZero<$Int>> for $Int {
12421243
type Output = $Int;
12431244

12441245
/// Same as `self / other.get()`, but because `other` is a `NonZero<_>`,
@@ -1256,7 +1257,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
12561257
}
12571258

12581259
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1259-
impl DivAssign<NonZero<$Int>> for $Int {
1260+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1261+
impl const DivAssign<NonZero<$Int>> for $Int {
12601262
/// Same as `self /= other.get()`, but because `other` is a `NonZero<_>`,
12611263
/// there's never a runtime check for division-by-zero.
12621264
///
@@ -1269,7 +1271,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
12691271
}
12701272

12711273
#[stable(feature = "nonzero_div", since = "1.51.0")]
1272-
impl Rem<NonZero<$Int>> for $Int {
1274+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1275+
impl const Rem<NonZero<$Int>> for $Int {
12731276
type Output = $Int;
12741277

12751278
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
@@ -1282,7 +1285,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
12821285
}
12831286

12841287
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1285-
impl RemAssign<NonZero<$Int>> for $Int {
1288+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1289+
impl const RemAssign<NonZero<$Int>> for $Int {
12861290
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
12871291
#[inline]
12881292
fn rem_assign(&mut self, other: NonZero<$Int>) {
@@ -1322,7 +1326,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
13221326
// Impls for signed nonzero types only.
13231327
(signed $Int:ty) => {
13241328
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1325-
impl Neg for NonZero<$Int> {
1329+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1330+
impl const Neg for NonZero<$Int> {
13261331
type Output = Self;
13271332

13281333
#[inline]
@@ -1333,7 +1338,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
13331338
}
13341339

13351340
forward_ref_unop! { impl Neg, neg for NonZero<$Int>,
1336-
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")] }
1341+
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1342+
#[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
13371343
};
13381344
}
13391345

0 commit comments

Comments
 (0)