Skip to content

Improve docs for UdpSocket::set_nonblocking. #45449

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 1 commit into from
Oct 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/libstd/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,16 +721,45 @@ impl UdpSocket {

/// Moves this UDP socket into or out of nonblocking mode.
///
/// On Unix this corresponds to calling fcntl, and on Windows this
/// corresponds to calling ioctlsocket.
/// This will result in `recv`, `recv_from`, `send`, and `send_to`
/// operations becoming nonblocking, i.e. immediately returning from their
/// calls. If the IO operation is successful, `Ok` is returned and no
/// further action is required. If the IO operation could not be completed
/// and needs to be retried, an error with kind
/// [`io::ErrorKind::WouldBlock`] is returned.
///
/// On Unix platforms, calling this method corresponds to calling `fcntl`
/// `FIONBIO`. On Windows calling this method corresponds to calling
/// `ioctlsocket` `FIONBIO`.
///
/// [`io::ErrorKind::WouldBlock`]: ../io/enum.ErrorKind.html#variant.WouldBlock
///
/// # Examples
///
/// Create a UDP socket bound to `127.0.0.1:7878` and read bytes in
/// nonblocking mode:
///
/// ```no_run
/// use std::io;
/// use std::net::UdpSocket;
///
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
/// socket.set_nonblocking(true).expect("set_nonblocking call failed");
/// let socket = UdpSocket::bind("127.0.0.1:7878").unwrap();
/// socket.set_nonblocking(true).unwrap();
///
/// # fn wait_for_fd() { unimplemented!() }
/// let mut buf = [0; 10];
/// let (num_bytes_read, _) = loop {
/// match socket.recv_from(&mut buf) {
/// Ok(n) => break n,
/// Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
/// // wait until network socket is ready, typically implemented
/// // via platform-specific APIs such as epoll or IOCP
/// wait_for_fd();
/// }
/// Err(e) => panic!("encountered IO error: {}", e),
/// }
/// };
/// println!("bytes: {:?}", &buf[..num_bytes_read]);
Copy link
Member

Choose a reason for hiding this comment

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

CI is failing. Note that .recv_from returns a Result<(usize, SocketAddr)>, not Result<usize>.

[01:04:03] ---- net/udp.rs - net::udp::UdpSocket::set_nonblocking (line 742) stdout ----
[01:04:03] 	error[E0277]: the trait bound `std::ops::RangeTo<(usize, std::net::SocketAddr)>: std::slice::SliceIndex<[u8]>` is not satisfied
[01:04:03]   --> net/udp.rs:23:26
[01:04:03]    |
[01:04:03] 23 | println!("bytes: {:?}", &buf[..num_bytes_read]);
[01:04:03]    |                          ^^^^^^^^^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
[01:04:03]    |
[01:04:03]    = help: the trait `std::slice::SliceIndex<[u8]>` is not implemented for `std::ops::RangeTo<(usize, std::net::SocketAddr)>`
[01:04:03]    = note: required because of the requirements on the impl of `std::ops::Index<std::ops::RangeTo<(usize, std::net::SocketAddr)>>` for `[u8]`
[01:04:03] 
[01:04:03] thread 'rustc' panicked at 'couldn't compile the test', /checkout/src/librustdoc/test.rs:287:12
[01:04:03] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[01:04:03] 
[01:04:03] 
[01:04:03] failures:
[01:04:03]     net/udp.rs - net::udp::UdpSocket::set_nonblocking (line 742)
[01:04:03] 
[01:04:03] test result: FAILED. 876 passed; 1 failed; 10 ignored; 0 measured; 0 filtered out

/// ```
#[stable(feature = "net2_mutators", since = "1.9.0")]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
Expand Down