Skip to content

Commit a3f84a9

Browse files
committed
constify comparison traits on network addresses
1 parent 0c8360a commit a3f84a9

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

library/core/src/net/ip_addr.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use crate::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
2626
/// ```
2727
#[rustc_diagnostic_item = "IpAddr"]
2828
#[stable(feature = "ip_addr", since = "1.7.0")]
29-
#[derive(Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
29+
#[derive(Copy, Hash)]
30+
#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord)]
3031
pub enum IpAddr {
3132
/// An IPv4 address.
3233
#[stable(feature = "ip_addr", since = "1.7.0")]
@@ -68,7 +69,8 @@ pub enum IpAddr {
6869
/// assert!("0xcb.0x0.0x71.0x00".parse::<Ipv4Addr>().is_err()); // all octets are in hex
6970
/// ```
7071
#[rustc_diagnostic_item = "Ipv4Addr"]
71-
#[derive(Copy, Clone, PartialEq, Eq)]
72+
#[derive(Copy)]
73+
#[derive_const(Clone, PartialEq, Eq)]
7274
#[stable(feature = "rust1", since = "1.0.0")]
7375
pub struct Ipv4Addr {
7476
octets: [u8; 4],
@@ -161,7 +163,8 @@ impl Hash for Ipv4Addr {
161163
/// assert_eq!(localhost.is_loopback(), true);
162164
/// ```
163165
#[rustc_diagnostic_item = "Ipv6Addr"]
164-
#[derive(Copy, Clone, PartialEq, Eq)]
166+
#[derive(Copy)]
167+
#[derive_const(Clone, PartialEq, Eq)]
165168
#[stable(feature = "rust1", since = "1.0.0")]
166169
pub struct Ipv6Addr {
167170
octets: [u8; 16],
@@ -1161,7 +1164,8 @@ impl fmt::Debug for Ipv4Addr {
11611164
}
11621165

11631166
#[stable(feature = "ip_cmp", since = "1.16.0")]
1164-
impl PartialEq<Ipv4Addr> for IpAddr {
1167+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1168+
impl const PartialEq<Ipv4Addr> for IpAddr {
11651169
#[inline]
11661170
fn eq(&self, other: &Ipv4Addr) -> bool {
11671171
match self {
@@ -1172,7 +1176,8 @@ impl PartialEq<Ipv4Addr> for IpAddr {
11721176
}
11731177

11741178
#[stable(feature = "ip_cmp", since = "1.16.0")]
1175-
impl PartialEq<IpAddr> for Ipv4Addr {
1179+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1180+
impl const PartialEq<IpAddr> for Ipv4Addr {
11761181
#[inline]
11771182
fn eq(&self, other: &IpAddr) -> bool {
11781183
match other {
@@ -1183,15 +1188,17 @@ impl PartialEq<IpAddr> for Ipv4Addr {
11831188
}
11841189

11851190
#[stable(feature = "rust1", since = "1.0.0")]
1186-
impl PartialOrd for Ipv4Addr {
1191+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1192+
impl const PartialOrd for Ipv4Addr {
11871193
#[inline]
11881194
fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
11891195
Some(self.cmp(other))
11901196
}
11911197
}
11921198

11931199
#[stable(feature = "ip_cmp", since = "1.16.0")]
1194-
impl PartialOrd<Ipv4Addr> for IpAddr {
1200+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1201+
impl const PartialOrd<Ipv4Addr> for IpAddr {
11951202
#[inline]
11961203
fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
11971204
match self {
@@ -1202,7 +1209,8 @@ impl PartialOrd<Ipv4Addr> for IpAddr {
12021209
}
12031210

12041211
#[stable(feature = "ip_cmp", since = "1.16.0")]
1205-
impl PartialOrd<IpAddr> for Ipv4Addr {
1212+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1213+
impl const PartialOrd<IpAddr> for Ipv4Addr {
12061214
#[inline]
12071215
fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
12081216
match other {
@@ -1213,7 +1221,8 @@ impl PartialOrd<IpAddr> for Ipv4Addr {
12131221
}
12141222

12151223
#[stable(feature = "rust1", since = "1.0.0")]
1216-
impl Ord for Ipv4Addr {
1224+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1225+
impl const Ord for Ipv4Addr {
12171226
#[inline]
12181227
fn cmp(&self, other: &Ipv4Addr) -> Ordering {
12191228
self.octets.cmp(&other.octets)
@@ -2155,7 +2164,8 @@ impl fmt::Debug for Ipv6Addr {
21552164
}
21562165

21572166
#[stable(feature = "ip_cmp", since = "1.16.0")]
2158-
impl PartialEq<IpAddr> for Ipv6Addr {
2167+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2168+
impl const PartialEq<IpAddr> for Ipv6Addr {
21592169
#[inline]
21602170
fn eq(&self, other: &IpAddr) -> bool {
21612171
match other {
@@ -2166,7 +2176,8 @@ impl PartialEq<IpAddr> for Ipv6Addr {
21662176
}
21672177

21682178
#[stable(feature = "ip_cmp", since = "1.16.0")]
2169-
impl PartialEq<Ipv6Addr> for IpAddr {
2179+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2180+
impl const PartialEq<Ipv6Addr> for IpAddr {
21702181
#[inline]
21712182
fn eq(&self, other: &Ipv6Addr) -> bool {
21722183
match self {
@@ -2177,15 +2188,17 @@ impl PartialEq<Ipv6Addr> for IpAddr {
21772188
}
21782189

21792190
#[stable(feature = "rust1", since = "1.0.0")]
2180-
impl PartialOrd for Ipv6Addr {
2191+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2192+
impl const PartialOrd for Ipv6Addr {
21812193
#[inline]
21822194
fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
21832195
Some(self.cmp(other))
21842196
}
21852197
}
21862198

21872199
#[stable(feature = "ip_cmp", since = "1.16.0")]
2188-
impl PartialOrd<Ipv6Addr> for IpAddr {
2200+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2201+
impl const PartialOrd<Ipv6Addr> for IpAddr {
21892202
#[inline]
21902203
fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
21912204
match self {
@@ -2196,7 +2209,8 @@ impl PartialOrd<Ipv6Addr> for IpAddr {
21962209
}
21972210

21982211
#[stable(feature = "ip_cmp", since = "1.16.0")]
2199-
impl PartialOrd<IpAddr> for Ipv6Addr {
2212+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2213+
impl const PartialOrd<IpAddr> for Ipv6Addr {
22002214
#[inline]
22012215
fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
22022216
match other {
@@ -2207,7 +2221,8 @@ impl PartialOrd<IpAddr> for Ipv6Addr {
22072221
}
22082222

22092223
#[stable(feature = "rust1", since = "1.0.0")]
2210-
impl Ord for Ipv6Addr {
2224+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2225+
impl const Ord for Ipv6Addr {
22112226
#[inline]
22122227
fn cmp(&self, other: &Ipv6Addr) -> Ordering {
22132228
self.segments().cmp(&other.segments())
@@ -2223,6 +2238,7 @@ impl const From<Ipv6Addr> for u128 {
22232238
ip.to_bits()
22242239
}
22252240
}
2241+
22262242
#[stable(feature = "i128", since = "1.26.0")]
22272243
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
22282244
impl const From<u128> for Ipv6Addr {

library/core/src/net/socket_addr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
2828
/// assert_eq!(socket.port(), 8080);
2929
/// assert_eq!(socket.is_ipv4(), true);
3030
/// ```
31-
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
31+
#[derive(Copy, Hash)]
32+
#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord)]
3233
#[stable(feature = "rust1", since = "1.0.0")]
3334
pub enum SocketAddr {
3435
/// An IPv4 socket address.
@@ -76,7 +77,8 @@ pub enum SocketAddr {
7677
/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
7778
/// assert_eq!(socket.port(), 8080);
7879
/// ```
79-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
80+
#[derive(Copy, Hash)]
81+
#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord)]
8082
#[stable(feature = "rust1", since = "1.0.0")]
8183
pub struct SocketAddrV4 {
8284
ip: Ipv4Addr,
@@ -142,7 +144,8 @@ pub struct SocketAddrV4 {
142144
/// with_scope.set_scope_id(3);
143145
/// assert_eq!("[2001:db8::1%3]:8080".parse(), Ok(with_scope));
144146
/// ```
145-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
147+
#[derive(Copy, Hash)]
148+
#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord)]
146149
#[stable(feature = "rust1", since = "1.0.0")]
147150
pub struct SocketAddrV6 {
148151
ip: Ipv6Addr,

0 commit comments

Comments
 (0)