@@ -227,16 +227,38 @@ pub trait Bytes<A> {
227227 /// Returns the number of bytes written. The number of bytes written can
228228 /// be less than the length of the slice if there isn't enough room in the
229229 /// container.
230+ ///
231+ /// If the given slice is empty (e.g. has length 0), always returns `Ok(0)`, even if `addr`
232+ /// is otherwise out of bounds. However, if the container is empty, it will
233+ /// return an error (unless the slice is also empty, in which case the above takes precedence).
234+ ///
235+ /// ```rust
236+ /// # use vm_memory::{Bytes, VolatileMemoryError, VolatileSlice};
237+ /// let mut arr = [1, 2, 3, 4, 5];
238+ /// let slice = VolatileSlice::from(arr.as_mut_slice());
239+ ///
240+ /// assert_eq!(slice.write(&[1, 2, 3], 0).unwrap(), 3);
241+ /// assert_eq!(slice.write(&[1, 2, 3], 3).unwrap(), 2);
242+ /// assert!(matches!(slice.write(&[1, 2, 3], 5).unwrap_err(), VolatileMemoryError::OutOfBounds {addr: 5}));
243+ /// assert_eq!(slice.write(&[], 5).unwrap(), 0);
244+ /// ```
230245 fn write ( & self , buf : & [ u8 ] , addr : A ) -> Result < usize , Self :: E > ;
231246
232247 /// Reads data from the container at `addr` into a slice.
233248 ///
234249 /// Returns the number of bytes read. The number of bytes read can be less than the length
235250 /// of the slice if there isn't enough data within the container.
251+ ///
252+ /// If the given slice is empty (e.g. has length 0), always returns `Ok(0)`, even if `addr`
253+ /// is otherwise out of bounds. However, if the container is empty, it will
254+ /// return an error (unless the slice is also empty, in which case the above takes precedence).
236255 fn read ( & self , buf : & mut [ u8 ] , addr : A ) -> Result < usize , Self :: E > ;
237256
238257 /// Writes the entire content of a slice into the container at `addr`.
239258 ///
259+ /// If the given slice is empty (e.g. has length 0), always returns `Ok(0)`, even if `addr`
260+ /// is otherwise out of bounds.
261+ ///
240262 /// # Errors
241263 ///
242264 /// Returns an error if there isn't enough space within the container to write the entire slice.
@@ -245,6 +267,9 @@ pub trait Bytes<A> {
245267
246268 /// Reads data from the container at `addr` to fill an entire slice.
247269 ///
270+ /// If the given slice is empty (e.g. has length 0), always returns `Ok(0)`, even if `addr`
271+ /// is otherwise out of bounds.
272+ ///
248273 /// # Errors
249274 ///
250275 /// Returns an error if there isn't enough data within the container to fill the entire slice.
0 commit comments