Skip to content

ToSocketAddr trait and unification of network structures constructor methods #18462

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 7 commits into from
Nov 5, 2014

Conversation

netvl
Copy link
Contributor

@netvl netvl commented Oct 30, 2014

This is a follow-up to RFC PR #173. I was told there that changes like this don't need to go through the RFC process, so I'm submitting this directly.

This PR introduces ToSocketAddr trait as defined in said RFC. This trait defines a conversion from different types like &str, (&str, u16) or even SocketAddr to SocketAddr. Then this trait is used in all constructor methods for TcpStream, TcpListener and UdpSocket.

This unifies these constructor methods - previously they were using different types of input parameters (TCP ones used (&str, u16) pair while UDP ones used SocketAddr), which is not consistent by itself and sometimes inconvenient - for example, when the address initially is available as SocketAddr, you still need to convert it to string to pass it to e.g. TcpStream. This is very prominently demonstrated by the unit tests for TCP functionality. This PR makes working with network objects much like with Path, which also uses similar trait to be able to be constructed from &[u8], Vec<u8> and other Paths.

This is a breaking change. If constant literals were used before, like this:

TcpStream::connect("localhost", 12345)

then the nicest fix is to change it to this:

TcpStream::connect("localhost:12345")

If variables were used before, like this:

TcpStream::connect(some_address, some_port)

then the arguments should be wrapped in another set of parentheses:

TcpStream::connect((some_address, some_port))

UdpSocket usages won't break because its constructor method accepted SocketAddr which implements ToSocketAddr, so bind() calls:

UdpSocket::bind(some_socket_addr)

will continue working as before.

I haven't changed UdpStream constructor because it is deprecated anyway.

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aturon (or someone else) soon.

@huonw
Copy link
Member

huonw commented Oct 30, 2014

cc @alexcrichton

@seanmonstar
Copy link
Contributor

Excellent! It really bothered me that in hyper I was allocating a String from a SocketAddr just to have the connect method create a new one.

let port_str = try_opt!(parts_iter.next(), "invalid socket address");
let host = try_opt!(parts_iter.next(), "invalid socket address");
let port: u16 = try_opt!(FromStr::from_str(port_str), "invalid port value");
resolve_socket_addr(host, port)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's actually a FromStr implementation for SocketAddr which this should probably use instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm nevermind, I see what's going on here

@alexcrichton
Copy link
Member

This looks great to me, thanks @netvl! Just wanna run it by @aturon as well to make sure I didn't miss anything conventions-wise.

@netvl
Copy link
Contributor Author

netvl commented Nov 2, 2014

I also want to add a little documentation on ToSocketAddr trait first, and probably tweak it on methods which use ToSocketAddr, if you don't mind.

@netvl
Copy link
Contributor Author

netvl commented Nov 4, 2014

I've added some docs on ToSocketAddr, so I think it is ready. Let me know if I there is something else I need to fix :)

@aturon
Copy link
Member

aturon commented Nov 4, 2014

This looks fantastic! I'm happy for this to land (needs a rebase, though.)

netvl added 6 commits November 5, 2014 12:01
This commit adds ToSocketAddr trait to std::io::net::ip module. This
trait is used for generic conversion from different types (strings,
(string, u16) tuples, etc.) into a SocketAddr instance. It supports
multiple output SocketAddresses when it is appropriate (e.g. DNS name
resolution).

This trait is going to be used by TcpStream, TcpListener and UdpSocket
structures.
TcpListener and TcpStream are converted to use ToSocketAddr trait in
their constructor methods.

[breaking-change]
UdpSocket constructor methods now use ToSocketAddr trait instead of
SocketAddr.

[breaking-change]
@netvl
Copy link
Contributor Author

netvl commented Nov 5, 2014

Rebased but still need to run tests to check that rebase was correct.

@alexcrichton
Copy link
Member

Thanks again for this @netvl!

bors added a commit that referenced this pull request Nov 5, 2014
This is a follow-up to [RFC PR #173](rust-lang/rfcs#173). I was told there that changes like this don't need to go through the RFC process, so I'm submitting this directly.

This PR introduces `ToSocketAddr` trait as defined in said RFC. This trait defines a conversion from different types like `&str`, `(&str, u16)` or even `SocketAddr` to `SocketAddr`. Then this trait is used in all constructor methods for `TcpStream`, `TcpListener` and `UdpSocket`.

This unifies these constructor methods - previously they were using different types of input parameters (TCP ones used `(&str, u16)` pair while UDP ones used `SocketAddr`), which is not consistent by itself and sometimes inconvenient - for example, when the address initially is available as `SocketAddr`, you still need to convert it to string to pass it to e.g. `TcpStream`. This is very prominently demonstrated by the unit tests for TCP functionality. This PR makes working with network objects much like with `Path`, which also uses similar trait to be able to be constructed from `&[u8]`, `Vec<u8>` and other `Path`s.

This is a breaking change. If constant literals were used before, like this:
```rust
TcpStream::connect("localhost", 12345)
```
then the nicest fix is to change it to this:
```rust
TcpStream::connect("localhost:12345")
```

If variables were used before, like this:
```rust
TcpStream::connect(some_address, some_port)
```
then the arguments should be wrapped in another set of parentheses:
```rust
TcpStream::connect((some_address, some_port))
```

`UdpSocket` usages won't break because its constructor method accepted `SocketAddr` which implements `ToSocketAddr`, so `bind()` calls:
```rust
UdpSocket::bind(some_socket_addr)
```
will continue working as before.

I haven't changed `UdpStream` constructor because it is deprecated anyway.
@bors bors closed this Nov 5, 2014
@bors bors merged commit 0f610f3 into rust-lang:master Nov 5, 2014
@netvl netvl deleted the to-socket-addr branch November 8, 2014 19:40
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.

7 participants