Implement Debug for std::net::{UdpSocket,TcpStream,TcpListener,Shutdown}#25078
Implement Debug for std::net::{UdpSocket,TcpStream,TcpListener,Shutdown}#25078bors merged 4 commits intorust-lang:masterfrom
Conversation
|
r? @pcwalton (rust_highfive has picked a reviewer for you, use r? to override) |
src/libstd/sys/common/net2.rs
Outdated
There was a problem hiding this comment.
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
net2should unwrap the socket address values. Without unwrapping it looks like this:One issue is that you can create, e.g.
UdpSockets with bad addresses, which means you can't just unwrap in the implementation:Fixes #23134.