@@ -227,16 +227,41 @@ 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!(
243+ /// slice.write(&[1, 2, 3], 5).unwrap_err(),
244+ /// VolatileMemoryError::OutOfBounds { addr: 5 }
245+ /// ));
246+ /// assert_eq!(slice.write(&[], 5).unwrap(), 0);
247+ /// ```
230248 fn write ( & self , buf : & [ u8 ] , addr : A ) -> Result < usize , Self :: E > ;
231249
232250 /// Reads data from the container at `addr` into a slice.
233251 ///
234252 /// Returns the number of bytes read. The number of bytes read can be less than the length
235253 /// of the slice if there isn't enough data within the container.
254+ ///
255+ /// If the given slice is empty (e.g. has length 0), always returns `Ok(0)`, even if `addr`
256+ /// is otherwise out of bounds. However, if the container is empty, it will
257+ /// return an error (unless the slice is also empty, in which case the above takes precedence).
236258 fn read ( & self , buf : & mut [ u8 ] , addr : A ) -> Result < usize , Self :: E > ;
237259
238260 /// Writes the entire content of a slice into the container at `addr`.
239261 ///
262+ /// If the given slice is empty (e.g. has length 0), always returns `Ok(0)`, even if `addr`
263+ /// is otherwise out of bounds.
264+ ///
240265 /// # Errors
241266 ///
242267 /// Returns an error if there isn't enough space within the container to write the entire slice.
@@ -245,6 +270,9 @@ pub trait Bytes<A> {
245270
246271 /// Reads data from the container at `addr` to fill an entire slice.
247272 ///
273+ /// If the given slice is empty (e.g. has length 0), always returns `Ok(0)`, even if `addr`
274+ /// is otherwise out of bounds.
275+ ///
248276 /// # Errors
249277 ///
250278 /// Returns an error if there isn't enough data within the container to fill the entire slice.
0 commit comments