-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add more examples to UpdSocket #38067
Conversation
/// use std::net::UdpSocket; | ||
/// | ||
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address"); | ||
/// socket.take_error().expect("no error was expected"); |
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.
Can you just assert for either Ok(None)
or Ok(Some(something))
or Err(something)
? This is very mind-bending.
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.
I think I might like this example more if it was something like:
let error = socket.take_error.expect("could not take error");
if let Some(error) = error {
println!("UDP socket error: {:?}", error);
}
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.
/// use std::net::UdpSocket; | ||
/// | ||
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address"); | ||
/// socket.connect("127.0.0.1:8080").expect("couldn't connect to server"); |
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.
I would omit “to server” since UDP is a connection-less protocol and all this does is set the default addresses to send to.
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.
Well, you connect to something so it'd be strange to not precise the server part. :-/
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.
No, you do not connect to anything with UDP. Server will never know that you… uh… “connect” to it. The only thing that really changes is some internal field called default_send_address
on the machine which… uh… “connects”.
connect
method on UdpSocket
is only named this way because its named this way on the UNIX side, but you oughtn’t be misled by the naming.
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.
Well, I see your point.
61e0c9d
to
8b0b2b6
Compare
Updated. |
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address"); | ||
/// socket.connect("127.0.0.1:8080").expect("connect function failed"); | ||
/// let mut buf = [0; 10]; | ||
/// match socket.recv(&mut buf) { |
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.
Nit: You're using expect
everywhere else, why not here?
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.
In this case, I found this way to construct code better.
LGTM. @nagisa Have any other comments? |
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.
No more comments.
@GuillaumeGomez r=frewsxcv,nagisa unless you want to address my nit above |
@bors r=frewsxcv,nagisa |
📌 Commit 8b0b2b6 has been approved by |
@bors rollup |
…nagisa Add more examples to UpdSocket r? @frewsxcv
…nagisa Add more examples to UpdSocket r? @frewsxcv
r? @frewsxcv