Skip to content

Commit 503516d

Browse files
committed
Delegate <SocketAddr as Debug> to ByteStr
This allows UTF-8 characters to be printed without escapes, rather than just ASCII.
1 parent 5af801b commit 503516d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

library/std/src/os/unix/net/addr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::bstr::ByteStr;
12
use crate::ffi::OsStr;
23
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
34
use crate::os::net::linux_ext;
@@ -61,7 +62,7 @@ pub(super) fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::s
6162
enum AddressKind<'a> {
6263
Unnamed,
6364
Pathname(&'a Path),
64-
Abstract(&'a [u8]),
65+
Abstract(&'a ByteStr),
6566
}
6667

6768
/// An address associated with a Unix socket.
@@ -245,7 +246,7 @@ impl SocketAddr {
245246
{
246247
AddressKind::Unnamed
247248
} else if self.addr.sun_path[0] == 0 {
248-
AddressKind::Abstract(&path[1..len])
249+
AddressKind::Abstract(ByteStr::from_bytes(&path[1..len]))
249250
} else {
250251
AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
251252
}
@@ -260,7 +261,7 @@ impl Sealed for SocketAddr {}
260261
#[stable(feature = "unix_socket_abstract", since = "1.70.0")]
261262
impl linux_ext::addr::SocketAddrExt for SocketAddr {
262263
fn as_abstract_name(&self) -> Option<&[u8]> {
263-
if let AddressKind::Abstract(name) = self.address() { Some(name) } else { None }
264+
if let AddressKind::Abstract(name) = self.address() { Some(name.as_bytes()) } else { None }
264265
}
265266

266267
fn from_abstract_name<N>(name: N) -> crate::io::Result<Self>
@@ -295,7 +296,7 @@ impl fmt::Debug for SocketAddr {
295296
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
296297
match self.address() {
297298
AddressKind::Unnamed => write!(fmt, "(unnamed)"),
298-
AddressKind::Abstract(name) => write!(fmt, "\"{}\" (abstract)", name.escape_ascii()),
299+
AddressKind::Abstract(name) => write!(fmt, "{name:?} (abstract)"),
299300
AddressKind::Pathname(path) => write!(fmt, "{path:?} (pathname)"),
300301
}
301302
}

0 commit comments

Comments
 (0)