-
Notifications
You must be signed in to change notification settings - Fork 20
add UnboundUdpSocket to std::net #66
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
Comments
Could anyone in libs team please help review / comment on this proposal? It has been here more than 3 weeks. I don't know if it has been looked at by libs team. @rust-lang/libs-impl @rust-lang/libs |
Is there a reason this doesn't use a similar Builder pattern to what's proposed on IRLO and is common elsewhere in the stdlib? You mention it in the ACP but don't elaborate on that. |
The main reason is that I felt that calling out If we use a Builder pattern and name the new type something like
|
We discussed this in the libs meetup. We came to the conclusion that we do want a mechanism to configure unbound sockets but UnboundUdpSocket is too narrow since there are other socket types that need this too and we eventually want to see the socket2 crate (or a polished subset thereof) uplifted to std; but that's a larger project which will require an RFC. CC @de-vri-es |
Proposal
Problem statement
Currently
std::net
does not support socket configurations (i.e.setsockopt
) before binding to an address. This is also evident fromUDPSocket::bind
,TcpListener::bind
andTcpStream::connect
methods.Motivation, use-cases
Example 1: setting SO_REUSEADDR is common for UDP sockets but it is not possible using
std::net
.Example 2: setting SO_EXCLUSIVEADDRUSE in useful on Windows, but it is not possible using
std::net
.Solution sketches
Primary solution
Add a new
UnboundUdpSocket
to support UDP socket configurations before binding to an address. Once bound,UnboundUdpSocket
will be consumed and become a regularUdpSocket
. The API looks like this:Note:
SocketAddrFamily
is a new type to simplify creating socket without an address.Similarly add
UnboundTcpSocket
to support TCP socket configurations before connecting or binding.The benefits of this solution:
std::net
API.Alternative solution
There have been other solutions suggested in the PR opened earlier. One is adding
UDPSocket::new()
tocreate an unbound UDP socket. I think it has following downsides:
UDPSocket
semantics: anUDPSocket
is always bound.UDPSocket
.Links and related work
Original discussion(s)
The original discussion that prompted this proposal is in Rust internal forum.
Existing crate(s)
The most popular crate for socket programming is probably
socket2
. However, I believe it's worth it to enhancestd::net
to support socket configurations before binding.Note that there is also a deprecated crate
net2
. I just looked its code now and found it was using aBuilder
pattern which is surprising similar with what I proposed earlier in the internal forum. For the record, I didn't know the design ofnet2
until now. In any case, the current proposal no longer suggests the Builder pattern and hopefully is different enough from thenet2
.PR
I have had opened an PR on this issue before the ACP process came out (AFAIK). Here is the PR.
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered: