[PATCH crosvm 2/2 v2] sys_util: expand on UnixSeqpacket documentation

Alyssa Ross
2020-07-06T23:15:20Z

---
Incorporated Cole's feedback.

sys_util/src/net.rs | 30 ++++++++++++++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/sys_util/src/net.rs b/sys_util/src/net.rs
index 82b4f5bb0..8df1743b6 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 or not `buf` was large enough to hold all of
+ /// it.
///
/// # Arguments
/// * `buf` - A mut reference to the data buffer.
@@ -205,8 +207,32 @@ 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.
+ /// Returns an error when `libc::recv` failed.
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
// Safe since we make sure the input `count` == `buf.len()` and handle the returned error.
unsafe {
--
2.26.2