--- sys_util/src/net.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/sys_util/src/net.rs b/sys_util/src/net.rs index 82b4f5bb0..0e1cf6e55 100644 --- a/sys_util/src/net.rs +++ b/sys_util/src/net.rs @@ -197,7 +197,9 @@ impl UnixSeqpacket { } } - /// Receive data from the socket fd to a given buffer. + /// Receive a message from the socket fd to a given buffer. The message will then + /// be removed from the socket, whether `buf` was large enough to hold all of it or + /// not. /// /// # Arguments /// * `buf` - A mut reference to the data buffer. @@ -205,6 +207,30 @@ impl UnixSeqpacket { /// # Returns /// * `usize` - The size of bytes read to the buffer. /// + /// # Examples + /// + /// ``` + /// # fn main() -> Result<(), sys_util::Error> { + /// # use sys_util::net::UnixSeqpacket; + /// let (s1, s2) = UnixSeqpacket::pair()?; + /// s1.send(b"First message")?; + /// s1.send(b"Second message")?; + /// s1.send(b"Third message")?; + /// + /// let mut buf = [0; 13]; + /// assert_eq!(s2.recv(&mut buf)?, 13); + /// assert_eq!(&buf, b"First message"); + /// + /// // Even reading into a buffer zero bytes long will consume a message. + /// assert_eq!(s2.recv(&mut [])?, 0); + /// + /// let mut buf = [0; 5]; + /// assert_eq!(s2.recv(&mut buf)?, 5); + /// assert_eq!(&buf, b"Third"); + /// # Ok(()) + /// # } + /// ``` + /// /// # Errors /// Returns error when `libc::recv` failed. pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> { -- 2.26.2