-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Conversation
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. |
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) |
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.
There's actually a FromStr
implementation for SocketAddr
which this should probably use instead
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.
Hm nevermind, I see what's going on here
I also want to add a little documentation on |
I've added some docs on |
This looks fantastic! I'm happy for this to land (needs a rebase, though.) |
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]
Rebased but still need to run tests to check that rebase was correct. |
Thanks again for this @netvl! |
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.
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 evenSocketAddr
toSocketAddr
. Then this trait is used in all constructor methods forTcpStream
,TcpListener
andUdpSocket
.This unifies these constructor methods - previously they were using different types of input parameters (TCP ones used
(&str, u16)
pair while UDP ones usedSocketAddr
), which is not consistent by itself and sometimes inconvenient - for example, when the address initially is available asSocketAddr
, 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 withPath
, which also uses similar trait to be able to be constructed from&[u8]
,Vec<u8>
and otherPath
s.This is a breaking change. If constant literals were used before, like this:
then the nicest fix is to change it to this:
If variables were used before, like this:
then the arguments should be wrapped in another set of parentheses:
UdpSocket
usages won't break because its constructor method acceptedSocketAddr
which implementsToSocketAddr
, sobind()
calls:will continue working as before.
I haven't changed
UdpStream
constructor because it is deprecated anyway.