Skip to content

Implement Debug for std::net::{UdpSocket,TcpStream,TcpListener,Shutdown} #25078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libstd/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/libstd/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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::*;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions src/libstd/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::*;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
}
}
47 changes: 47 additions & 0 deletions src/libstd/sys/common/net2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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()
}
}