diff options
| author | Frank Rehberger <frehberg@gmail.com> | 2017-09-07 00:54:28 +0200 |
|---|---|---|
| committer | Frank Rehberger <frehberg@gmail.com> | 2017-09-12 01:50:32 +0200 |
| commit | 85a9d978844fd37a9458db0921de5443aa680c1c (patch) | |
| tree | 1ec627bd67a81405997c771dd7fbceeab7380483 /src/libstd/net | |
| parent | a20953906056f85f71896795e762ac242e1891aa (diff) | |
| download | rust-85a9d978844fd37a9458db0921de5443aa680c1c.tar.gz rust-85a9d978844fd37a9458db0921de5443aa680c1c.zip | |
rustdoc: extend UdpSocket API doc (#657)
rustdoc: type-fixes
Diffstat (limited to 'src/libstd/net')
| -rw-r--r-- | src/libstd/net/udp.rs | 54 |
1 files changed, 41 insertions, 13 deletions
diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs index 35001833383..cefe7af8ff3 100644 --- a/src/libstd/net/udp.rs +++ b/src/libstd/net/udp.rs @@ -48,11 +48,12 @@ use time::Duration; /// { /// let mut socket = UdpSocket::bind("127.0.0.1:34254")?; /// -/// // read from the socket +/// // Receives a single datagram message on the socket. If `buf` is too small to hold +/// // the message, it will be cut off. /// let mut buf = [0; 10]; /// let (amt, src) = socket.recv_from(&mut buf)?; /// -/// // send a reply to the socket we received data from +/// // Redeclare `buf` as slice of the received data and send reverse data back to origin. /// let buf = &mut buf[..amt]; /// buf.reverse(); /// socket.send_to(buf, &src)?; @@ -103,8 +104,12 @@ impl UdpSocket { super::each_addr(addr, net_imp::UdpSocket::bind).map(UdpSocket) } - /// Receives data from the socket. On success, returns the number of bytes - /// read and the address from whence the data came. + /// Receives a single datagram message on the socket. On success, returns the number + /// of bytes read and the origin. + /// + /// The function must be called with valid byte array `buf` of sufficient size to + /// hold the message bytes. If a message is too long to fit in the supplied buffer, + /// excess bytes may be discarded. /// /// # Examples /// @@ -115,19 +120,25 @@ impl UdpSocket { /// let mut buf = [0; 10]; /// let (number_of_bytes, src_addr) = socket.recv_from(&mut buf) /// .expect("Didn't receive data"); + /// let filled_buf = &mut buf[..number_of_bytes]; /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { self.0.recv_from(buf) } - /// Receives data from the socket, without removing it from the queue. + /// Receives a single datagram message on the socket, without removing it from the + /// queue. On success, returns the number of bytes read and the origin. + /// + /// The function must be called with valid byte array `buf` of sufficient size to + /// hold the message bytes. If a message is too long to fit in the supplied buffer, + /// excess bytes may be discarded. /// /// Successive calls return the same data. This is accomplished by passing /// `MSG_PEEK` as a flag to the underlying `recvfrom` system call. /// - /// On success, returns the number of bytes peeked and the address from - /// whence the data came. + /// Do not use this function to implement busy waiting, instead use `libc::poll` to + /// synchronize IO events on one or more sockets. /// /// # Examples /// @@ -138,6 +149,7 @@ impl UdpSocket { /// let mut buf = [0; 10]; /// let (number_of_bytes, src_addr) = socket.peek_from(&mut buf) /// .expect("Didn't receive data"); + /// let filled_buf = &mut buf[..number_of_bytes]; /// ``` #[stable(feature = "peek", since = "1.18.0")] pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { @@ -642,8 +654,12 @@ impl UdpSocket { self.0.send(buf) } - /// Receives data on the socket from the remote address to which it is - /// connected. + /// Receives a single datagram message on the socket from the remote address to + /// which it is connected. On success, returns the number of bytes read. + /// + /// The function must be called with valid byte array `buf` of sufficient size to + /// hold the message bytes. If a message is too long to fit in the supplied buffer, + /// excess bytes may be discarded. /// /// The [`connect`] method will connect this socket to a remote address. This /// method will fail if the socket is not connected. @@ -659,7 +675,7 @@ impl UdpSocket { /// socket.connect("127.0.0.1:8080").expect("connect function failed"); /// let mut buf = [0; 10]; /// match socket.recv(&mut buf) { - /// Ok(received) => println!("received {} bytes", received), + /// Ok(received) => println!("received {} bytes {:?}", received, &buf[..received]), /// Err(e) => println!("recv function failed: {:?}", e), /// } /// ``` @@ -668,13 +684,25 @@ impl UdpSocket { self.0.recv(buf) } - /// Receives data on the socket from the remote address to which it is - /// connected, without removing that data from the queue. On success, - /// returns the number of bytes peeked. + /// Receives single datagram on the socket from the remote address to which it is + /// connected, without removing the message from input queue. On success, returns + /// the number of bytes peeked. + /// + /// The function must be called with valid byte array `buf` of sufficient size to + /// hold the message bytes. If a message is too long to fit in the supplied buffer, + /// excess bytes may be discarded. /// /// Successive calls return the same data. This is accomplished by passing /// `MSG_PEEK` as a flag to the underlying `recv` system call. /// + /// Do not use this function to implement busy waiting, instead use `libc::poll` to + /// synchronize IO events on one or more sockets. + /// + /// The [`connect`] method will connect this socket to a remote address. This + /// method will fail if the socket is not connected. + /// + /// [`connect`]: #method.connect + /// /// # Errors /// /// This method will fail if the socket is not connected. The `connect` method |
