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

Conversation

nham
Copy link
Contributor

@nham nham commented May 3, 2015

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. UdpSockets 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.

@rust-highfive
Copy link
Contributor

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())
Copy link
Member

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())

@alexcrichton
Copy link
Member

Awesome, thanks! Could you also add some tests that exercise this functionality?

@alexcrichton alexcrichton assigned alexcrichton and unassigned pcwalton May 3, 2015
@sfackler
Copy link
Member

sfackler commented May 3, 2015

I use a little adapter type in rust-unix-socket to make Result-wrapped addresses a bit nicer to display: https://github.com/sfackler/rust-unix-socket/blob/master/src/lib.rs#L199-L208. It avoids the Ok(...)/Err(...) wrappers and uses Debug in the Ok case and Display in the Err case.

Nick Hamann added 3 commits May 3, 2015 17:13
…ner/UdpSocket.

This now omits address fields in Debug implementations when a proper address value
cannot be unwrapped.
@nham
Copy link
Contributor Author

nham commented May 4, 2015

@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?

@sfackler
Copy link
Member

sfackler commented May 4, 2015

Seems fine as is.

@alexcrichton
Copy link
Member

@bors: r+ 987eb28

Thanks!

@bors
Copy link
Collaborator

bors commented May 4, 2015

⌛ Testing commit 987eb28 with merge 70db766...

bors added a commit that referenced this pull request May 4, 2015
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.
@bors
Copy link
Collaborator

bors commented May 4, 2015

@bors bors merged commit 987eb28 into rust-lang:master May 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TcpStream (and probably more std::net types) missing Debug impl
6 participants