-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Conversation
r? @pcwalton (rust_highfive has picked a reviewer for you, use r? to override) |
f.debug_struct("TcpStream") | ||
.field("addr", &self.socket_addr()) | ||
.field("peer", &self.peer_addr()) | ||
.field("inner", &self.inner.as_inner()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be worth using using the windows/unix specific naming here to say whether it's a file descriptor or a socket. For example something like:
let name = if cfg!(windows) {"socket"} else {"fd"};
// ...
.field(name, &self.inner.as_inner())
Awesome, thanks! Could you also add some tests that exercise this functionality? |
I use a little adapter type in rust-unix-socket to make |
…ner/UdpSocket. This now omits address fields in Debug implementations when a proper address value cannot be unwrapped.
@alexcrichton I incorporated your suggestions above, including adding some basic tests. @sfackler That's a good idea, but for the moment I've adopted @alexcrichton's suggestion to hide the field if it's an Err. Do you think it's more useful to display the error instead? |
Seems fine as is. |
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.
I'm uncertain whether the 3 implementations in
net2
should unwrap the socket address values. Without unwrapping it looks like this:One issue is that you can create, e.g.
UdpSocket
s with bad addresses, which means you can't just unwrap in the implementation:Fixes #23134.