Skip to content

Commit 6293dca

Browse files
Change Debug impl of SocketAddr and IpAddr to match their Display output
This has already been done for `SocketAddrV4`, `SocketAddrV6`, `IpAddrV4` and `IpAddrV6`. I don't see a point to keep the rather bad to read derived impl, especially when pretty printing: V4( 127.0.0.1 ) From the `Display`, one can easily and unambiguously see if it's V4 or V6. Using `Display` as `Debug` is very convenient for configuration structs (e.g. for webservers) that often just have a `derive(Debug)` and are printed that way to the user.
1 parent 1f5d69d commit 6293dca

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

library/std/src/net/addr.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::vec;
3737
/// assert_eq!(socket.port(), 8080);
3838
/// assert_eq!(socket.is_ipv4(), true);
3939
/// ```
40-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
40+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
4141
#[stable(feature = "rust1", since = "1.0.0")]
4242
pub enum SocketAddr {
4343
/// An IPv4 socket address.
@@ -597,6 +597,13 @@ impl fmt::Display for SocketAddr {
597597
}
598598
}
599599

600+
#[stable(feature = "rust1", since = "1.0.0")]
601+
impl fmt::Debug for SocketAddr {
602+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
603+
fmt::Display::fmt(self, fmt)
604+
}
605+
}
606+
600607
#[stable(feature = "rust1", since = "1.0.0")]
601608
impl fmt::Display for SocketAddrV4 {
602609
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

library/std/src/net/ip.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::sys_common::{AsInner, FromInner};
3939
/// assert_eq!(localhost_v4.is_ipv4(), true);
4040
/// ```
4141
#[stable(feature = "ip_addr", since = "1.7.0")]
42-
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, PartialOrd, Ord)]
42+
#[derive(Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
4343
pub enum IpAddr {
4444
/// An IPv4 address.
4545
#[stable(feature = "ip_addr", since = "1.7.0")]
@@ -811,6 +811,13 @@ impl fmt::Display for IpAddr {
811811
}
812812
}
813813

814+
#[stable(feature = "ip_addr", since = "1.7.0")]
815+
impl fmt::Debug for IpAddr {
816+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
817+
fmt::Display::fmt(self, fmt)
818+
}
819+
}
820+
814821
#[stable(feature = "ip_from_ip", since = "1.16.0")]
815822
impl From<Ipv4Addr> for IpAddr {
816823
/// Copies this address to a new `IpAddr::V4`.

0 commit comments

Comments
 (0)