|
11 | 11 | use prelude::v1::*; |
12 | 12 |
|
13 | 13 | use ffi::{CStr, CString}; |
| 14 | +use fmt; |
14 | 15 | use io::{self, Error, ErrorKind}; |
15 | 16 | use libc::{self, c_int, c_char, c_void, socklen_t}; |
16 | 17 | use mem; |
@@ -268,6 +269,24 @@ impl FromInner<Socket> for TcpStream { |
268 | 269 | } |
269 | 270 | } |
270 | 271 |
|
| 272 | +impl fmt::Debug for TcpStream { |
| 273 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 274 | + let mut res = f.debug_struct("TcpStream"); |
| 275 | + |
| 276 | + if let Ok(addr) = self.socket_addr() { |
| 277 | + res = res.field("addr", &addr); |
| 278 | + } |
| 279 | + |
| 280 | + if let Ok(peer) = self.peer_addr() { |
| 281 | + res = res.field("peer", &peer); |
| 282 | + } |
| 283 | + |
| 284 | + let name = if cfg!(windows) {"socket"} else {"fd"}; |
| 285 | + res = res.field(name, &self.inner.as_inner()); |
| 286 | + res.finish() |
| 287 | + } |
| 288 | +} |
| 289 | + |
271 | 290 | //////////////////////////////////////////////////////////////////////////////// |
272 | 291 | // TCP listeners |
273 | 292 | //////////////////////////////////////////////////////////////////////////////// |
@@ -327,6 +346,20 @@ impl FromInner<Socket> for TcpListener { |
327 | 346 | } |
328 | 347 | } |
329 | 348 |
|
| 349 | +impl fmt::Debug for TcpListener { |
| 350 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 351 | + let mut res = f.debug_struct("TcpListener"); |
| 352 | + |
| 353 | + if let Ok(addr) = self.socket_addr() { |
| 354 | + res = res.field("addr", &addr); |
| 355 | + } |
| 356 | + |
| 357 | + let name = if cfg!(windows) {"socket"} else {"fd"}; |
| 358 | + res = res.field(name, &self.inner.as_inner()); |
| 359 | + res.finish() |
| 360 | + } |
| 361 | +} |
| 362 | + |
330 | 363 | //////////////////////////////////////////////////////////////////////////////// |
331 | 364 | // UDP |
332 | 365 | //////////////////////////////////////////////////////////////////////////////// |
@@ -445,3 +478,17 @@ impl FromInner<Socket> for UdpSocket { |
445 | 478 | UdpSocket { inner: socket } |
446 | 479 | } |
447 | 480 | } |
| 481 | + |
| 482 | +impl fmt::Debug for UdpSocket { |
| 483 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 484 | + let mut res = f.debug_struct("UdpSocket"); |
| 485 | + |
| 486 | + if let Ok(addr) = self.socket_addr() { |
| 487 | + res = res.field("addr", &addr); |
| 488 | + } |
| 489 | + |
| 490 | + let name = if cfg!(windows) {"socket"} else {"fd"}; |
| 491 | + res = res.field(name, &self.inner.as_inner()); |
| 492 | + res.finish() |
| 493 | + } |
| 494 | +} |
0 commit comments