Auto merge of #25078 - nham:std_net_impl_debug, r=alexcrichton

I'm uncertain whether the 3 implementations in `net2` should unwrap the socket address values. Without unwrapping it looks like this:

```
UdpSocket { addr: Ok(V4(127.0.0.1:34354)), inner: 3 }
TcpListener { addr: Ok(V4(127.0.0.1:9123)), inner: 4 }
TcpStream { addr: Ok(V4(127.0.0.1:9123)), peer: Ok(V4(127.0.0.1:58360)), inner: 5 }
```

One issue is that you can create, e.g. `UdpSocket`s with bad addresses, which means you can't just unwrap in the implementation:

```
#![feature(from_raw_os)]
use std::net::UdpSocket;
use std::os::unix::io::FromRawFd;

let sock: UdpSocket = unsafe { FromRawFd::from_raw_fd(-1) };
println!("{:?}", sock); // prints "UdpSocket { addr: Err(Error { repr: Os(9) }), inner: -1 }"

```

Fixes #23134.
This commit is contained in:
bors 2015-05-04 17:12:19 +00:00
commit 70db76602e
4 changed files with 105 additions and 1 deletions

View file

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

View file

@ -14,6 +14,7 @@
use prelude::v1::*;
use io::prelude::*;
use fmt;
use io;
use net::{ToSocketAddrs, SocketAddr, Shutdown};
use sys_common::net2 as net_imp;
@ -167,6 +168,12 @@ impl FromInner<net_imp::TcpStream> for TcpStream {
fn from_inner(inner: net_imp::TcpStream) -> TcpStream { TcpStream(inner) }
}
impl fmt::Debug for TcpStream {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl TcpListener {
/// Creates a new `TcpListener` which will be bound to the specified
/// address.
@ -239,6 +246,12 @@ impl FromInner<net_imp::TcpListener> for TcpListener {
}
}
impl fmt::Debug for TcpListener {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[cfg(test)]
mod tests {
use prelude::v1::*;
@ -248,6 +261,7 @@ mod tests {
use net::*;
use net::test::{next_test_ip4, next_test_ip6};
use sync::mpsc::channel;
use sys_common::AsInner;
use thread;
fn each_ip(f: &mut FnMut(SocketAddr)) {
@ -818,4 +832,27 @@ mod tests {
rx.recv().unwrap();
})
}
#[test]
fn debug() {
let name = if cfg!(windows) {"socket"} else {"fd"};
let socket_addr = next_test_ip4();
let listener = t!(TcpListener::bind(&socket_addr));
let listener_inner = listener.0.socket().as_inner();
let compare = format!("TcpListener {{ addr: {:?}, {}: {:?} }}",
socket_addr, name, listener_inner);
assert_eq!(format!("{:?}", listener), compare);
let mut stream = t!(TcpStream::connect(&("localhost",
socket_addr.port())));
let stream_inner = stream.0.socket().as_inner();
let compare = format!("TcpStream {{ addr: {:?}, \
peer: {:?}, {}: {:?} }}",
stream.local_addr().unwrap(),
stream.peer_addr().unwrap(),
name,
stream_inner);
assert_eq!(format!("{:?}", stream), compare);
}
}

View file

@ -13,6 +13,7 @@
use prelude::v1::*;
use fmt;
use io::{self, Error, ErrorKind};
use net::{ToSocketAddrs, SocketAddr, IpAddr};
use sys_common::net2 as net_imp;
@ -136,6 +137,12 @@ impl FromInner<net_imp::UdpSocket> for UdpSocket {
fn from_inner(inner: net_imp::UdpSocket) -> UdpSocket { UdpSocket(inner) }
}
impl fmt::Debug for UdpSocket {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[cfg(test)]
mod tests {
use prelude::v1::*;
@ -144,6 +151,7 @@ mod tests {
use net::*;
use net::test::{next_test_ip4, next_test_ip6};
use sync::mpsc::channel;
use sys_common::AsInner;
use thread;
fn each_ip(f: &mut FnMut(SocketAddr, SocketAddr)) {
@ -301,4 +309,16 @@ mod tests {
serv_rx.recv().unwrap();
})
}
#[test]
fn debug() {
let name = if cfg!(windows) {"socket"} else {"fd"};
let socket_addr = next_test_ip4();
let udpsock = t!(UdpSocket::bind(&socket_addr));
let udpsock_inner = udpsock.0.socket().as_inner();
let compare = format!("UdpSocket {{ addr: {:?}, {}: {:?} }}",
socket_addr, name, udpsock_inner);
assert_eq!(format!("{:?}", udpsock), compare);
}
}

View file

@ -11,6 +11,7 @@
use prelude::v1::*;
use ffi::{CStr, CString};
use fmt;
use io::{self, Error, ErrorKind};
use libc::{self, c_int, c_char, c_void, socklen_t};
use mem;
@ -268,6 +269,24 @@ impl FromInner<Socket> for TcpStream {
}
}
impl fmt::Debug for TcpStream {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut res = f.debug_struct("TcpStream");
if let Ok(addr) = self.socket_addr() {
res = res.field("addr", &addr);
}
if let Ok(peer) = self.peer_addr() {
res = res.field("peer", &peer);
}
let name = if cfg!(windows) {"socket"} else {"fd"};
res = res.field(name, &self.inner.as_inner());
res.finish()
}
}
////////////////////////////////////////////////////////////////////////////////
// TCP listeners
////////////////////////////////////////////////////////////////////////////////
@ -327,6 +346,20 @@ impl FromInner<Socket> for TcpListener {
}
}
impl fmt::Debug for TcpListener {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut res = f.debug_struct("TcpListener");
if let Ok(addr) = self.socket_addr() {
res = res.field("addr", &addr);
}
let name = if cfg!(windows) {"socket"} else {"fd"};
res = res.field(name, &self.inner.as_inner());
res.finish()
}
}
////////////////////////////////////////////////////////////////////////////////
// UDP
////////////////////////////////////////////////////////////////////////////////
@ -445,3 +478,17 @@ impl FromInner<Socket> for UdpSocket {
UdpSocket { inner: socket }
}
}
impl fmt::Debug for UdpSocket {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut res = f.debug_struct("UdpSocket");
if let Ok(addr) = self.socket_addr() {
res = res.field("addr", &addr);
}
let name = if cfg!(windows) {"socket"} else {"fd"};
res = res.field(name, &self.inner.as_inner());
res.finish()
}
}