diff options
| author | kennytm <kennytm@gmail.com> | 2017-10-28 15:56:20 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-10-28 15:56:20 +0800 |
| commit | 71b876bca0d5fe250f8579c853425f00fbd8e491 (patch) | |
| tree | 4687371f4d8dd28e867a8743a2ce59cbd78caff9 | |
| parent | b644339a167f8ab3303b7cbf9a4a97438f231b87 (diff) | |
| parent | 04f27f01cb25f63d25e07ed0a48a8ac4135371c8 (diff) | |
| download | rust-71b876bca0d5fe250f8579c853425f00fbd8e491.tar.gz rust-71b876bca0d5fe250f8579c853425f00fbd8e491.zip | |
Rollup merge of #45449 - frewsxcv:frewsxcv-udp-nonblocking, r=sfackler
Improve docs for UdpSocket::set_nonblocking. Closes https://github.com/rust-lang/rust/issues/44050.
| -rw-r--r-- | src/libstd/net/udp.rs | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs index 870d11298fe..84ceaa65951 100644 --- a/src/libstd/net/udp.rs +++ b/src/libstd/net/udp.rs @@ -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]); /// ``` #[stable(feature = "net2_mutators", since = "1.9.0")] pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { |
