Skip to content

Commit c4fe6d7

Browse files
committed
Constify remaining impls
1 parent f86b89c commit c4fe6d7

File tree

6 files changed

+388
-163
lines changed

6 files changed

+388
-163
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/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
}

0 commit comments

Comments
 (0)