Skip to content

Commit 3a4a1e5

Browse files
author
Nick Hamann
committed
Implement Debug for std::net::{UdpSocket,TcpStream,TcpListener,Shutdown}
Fixes #23134.
1 parent 84f8c25 commit 3a4a1e5

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/libstd/net/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod parser;
3232

3333
/// Possible values which can be passed to the `shutdown` method of `TcpStream`
3434
/// and `UdpSocket`.
35-
#[derive(Copy, Clone, PartialEq)]
35+
#[derive(Copy, Clone, PartialEq, Debug)]
3636
#[stable(feature = "rust1", since = "1.0.0")]
3737
pub enum Shutdown {
3838
/// Indicates that the reading portion of this stream/socket should be shut

src/libstd/net/tcp.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use prelude::v1::*;
1515
use io::prelude::*;
1616

17+
use fmt;
1718
use io;
1819
use net::{ToSocketAddrs, SocketAddr, Shutdown};
1920
use sys_common::net2 as net_imp;
@@ -167,6 +168,12 @@ impl FromInner<net_imp::TcpStream> for TcpStream {
167168
fn from_inner(inner: net_imp::TcpStream) -> TcpStream { TcpStream(inner) }
168169
}
169170

171+
impl fmt::Debug for TcpStream {
172+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
173+
self.0.fmt(f)
174+
}
175+
}
176+
170177
impl TcpListener {
171178
/// Creates a new `TcpListener` which will be bound to the specified
172179
/// address.
@@ -239,6 +246,12 @@ impl FromInner<net_imp::TcpListener> for TcpListener {
239246
}
240247
}
241248

249+
impl fmt::Debug for TcpListener {
250+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
251+
self.0.fmt(f)
252+
}
253+
}
254+
242255
#[cfg(test)]
243256
mod tests {
244257
use prelude::v1::*;

src/libstd/net/udp.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use prelude::v1::*;
1515

16+
use fmt;
1617
use io::{self, Error, ErrorKind};
1718
use net::{ToSocketAddrs, SocketAddr, IpAddr};
1819
use sys_common::net2 as net_imp;
@@ -136,6 +137,12 @@ impl FromInner<net_imp::UdpSocket> for UdpSocket {
136137
fn from_inner(inner: net_imp::UdpSocket) -> UdpSocket { UdpSocket(inner) }
137138
}
138139

140+
impl fmt::Debug for UdpSocket {
141+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
142+
self.0.fmt(f)
143+
}
144+
}
145+
139146
#[cfg(test)]
140147
mod tests {
141148
use prelude::v1::*;

src/libstd/sys/common/net2.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use prelude::v1::*;
1212

1313
use ffi::{CStr, CString};
14+
use fmt;
1415
use io::{self, Error, ErrorKind};
1516
use libc::{self, c_int, c_char, c_void, socklen_t};
1617
use mem;
@@ -268,6 +269,16 @@ impl FromInner<Socket> for TcpStream {
268269
}
269270
}
270271

272+
impl fmt::Debug for TcpStream {
273+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
274+
f.debug_struct("TcpStream")
275+
.field("addr", &self.socket_addr())
276+
.field("peer", &self.peer_addr())
277+
.field("inner", &self.inner.as_inner())
278+
.finish()
279+
}
280+
}
281+
271282
////////////////////////////////////////////////////////////////////////////////
272283
// TCP listeners
273284
////////////////////////////////////////////////////////////////////////////////
@@ -327,6 +338,15 @@ impl FromInner<Socket> for TcpListener {
327338
}
328339
}
329340

341+
impl fmt::Debug for TcpListener {
342+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
343+
f.debug_struct("TcpListener")
344+
.field("addr", &self.socket_addr())
345+
.field("inner", &self.inner.as_inner())
346+
.finish()
347+
}
348+
}
349+
330350
////////////////////////////////////////////////////////////////////////////////
331351
// UDP
332352
////////////////////////////////////////////////////////////////////////////////
@@ -445,3 +465,12 @@ impl FromInner<Socket> for UdpSocket {
445465
UdpSocket { inner: socket }
446466
}
447467
}
468+
469+
impl fmt::Debug for UdpSocket {
470+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
471+
f.debug_struct("UdpSocket")
472+
.field("addr", &self.socket_addr())
473+
.field("inner", &self.inner.as_inner())
474+
.finish()
475+
}
476+
}

0 commit comments

Comments
 (0)