Skip to content

Commit 70db766

Browse files
committed
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.
2 parents a979efc + 987eb28 commit 70db766

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-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: 37 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::*;
@@ -248,6 +261,7 @@ mod tests {
248261
use net::*;
249262
use net::test::{next_test_ip4, next_test_ip6};
250263
use sync::mpsc::channel;
264+
use sys_common::AsInner;
251265
use thread;
252266

253267
fn each_ip(f: &mut FnMut(SocketAddr)) {
@@ -818,4 +832,27 @@ mod tests {
818832
rx.recv().unwrap();
819833
})
820834
}
835+
836+
#[test]
837+
fn debug() {
838+
let name = if cfg!(windows) {"socket"} else {"fd"};
839+
let socket_addr = next_test_ip4();
840+
841+
let listener = t!(TcpListener::bind(&socket_addr));
842+
let listener_inner = listener.0.socket().as_inner();
843+
let compare = format!("TcpListener {{ addr: {:?}, {}: {:?} }}",
844+
socket_addr, name, listener_inner);
845+
assert_eq!(format!("{:?}", listener), compare);
846+
847+
let mut stream = t!(TcpStream::connect(&("localhost",
848+
socket_addr.port())));
849+
let stream_inner = stream.0.socket().as_inner();
850+
let compare = format!("TcpStream {{ addr: {:?}, \
851+
peer: {:?}, {}: {:?} }}",
852+
stream.local_addr().unwrap(),
853+
stream.peer_addr().unwrap(),
854+
name,
855+
stream_inner);
856+
assert_eq!(format!("{:?}", stream), compare);
857+
}
821858
}

src/libstd/net/udp.rs

Lines changed: 20 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::*;
@@ -144,6 +151,7 @@ mod tests {
144151
use net::*;
145152
use net::test::{next_test_ip4, next_test_ip6};
146153
use sync::mpsc::channel;
154+
use sys_common::AsInner;
147155
use thread;
148156

149157
fn each_ip(f: &mut FnMut(SocketAddr, SocketAddr)) {
@@ -301,4 +309,16 @@ mod tests {
301309
serv_rx.recv().unwrap();
302310
})
303311
}
312+
313+
#[test]
314+
fn debug() {
315+
let name = if cfg!(windows) {"socket"} else {"fd"};
316+
let socket_addr = next_test_ip4();
317+
318+
let udpsock = t!(UdpSocket::bind(&socket_addr));
319+
let udpsock_inner = udpsock.0.socket().as_inner();
320+
let compare = format!("UdpSocket {{ addr: {:?}, {}: {:?} }}",
321+
socket_addr, name, udpsock_inner);
322+
assert_eq!(format!("{:?}", udpsock), compare);
323+
}
304324
}

src/libstd/sys/common/net2.rs

Lines changed: 47 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,24 @@ 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+
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+
271290
////////////////////////////////////////////////////////////////////////////////
272291
// TCP listeners
273292
////////////////////////////////////////////////////////////////////////////////
@@ -327,6 +346,20 @@ impl FromInner<Socket> for TcpListener {
327346
}
328347
}
329348

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+
330363
////////////////////////////////////////////////////////////////////////////////
331364
// UDP
332365
////////////////////////////////////////////////////////////////////////////////
@@ -445,3 +478,17 @@ impl FromInner<Socket> for UdpSocket {
445478
UdpSocket { inner: socket }
446479
}
447480
}
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

Comments
 (0)