Skip to content

Commit 5b61230

Browse files
authored
Rollup merge of #74409 - LukasKalbertodt:improve-debug-impl-of-socketaddr-ipaddr, r=Amanieu
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 so when pretty printing: V4( 127.0.0.1 ) From the `Display`, one can easily and unambiguously see if it's V4 or V6. Two examples: ``` 127.0.0.1:443 [2001:db8:85a3::8a2e:370:7334]:443 ``` Luckily the docs explicitly state that `Debug` output is not stable and that it may be changed at any time. 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 one starting the server.
2 parents dae020d + 6293dca commit 5b61230

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
@@ -40,7 +40,7 @@ use crate::sys_common::{AsInner, FromInner};
4040
/// assert_eq!(localhost_v4.is_ipv4(), true);
4141
/// ```
4242
#[stable(feature = "ip_addr", since = "1.7.0")]
43-
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, PartialOrd, Ord)]
43+
#[derive(Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
4444
pub enum IpAddr {
4545
/// An IPv4 address.
4646
#[stable(feature = "ip_addr", since = "1.7.0")]
@@ -802,6 +802,13 @@ impl fmt::Display for IpAddr {
802802
}
803803
}
804804

805+
#[stable(feature = "ip_addr", since = "1.7.0")]
806+
impl fmt::Debug for IpAddr {
807+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
808+
fmt::Display::fmt(self, fmt)
809+
}
810+
}
811+
805812
#[stable(feature = "ip_from_ip", since = "1.16.0")]
806813
impl From<Ipv4Addr> for IpAddr {
807814
/// Copies this address to a new `IpAddr::V4`.

0 commit comments

Comments
 (0)