@@ -227,16 +227,41 @@ pub trait Bytes<A> {
227
227
/// Returns the number of bytes written. The number of bytes written can
228
228
/// be less than the length of the slice if there isn't enough room in the
229
229
/// 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
+ /// ```
230
248
fn write ( & self , buf : & [ u8 ] , addr : A ) -> Result < usize , Self :: E > ;
231
249
232
250
/// Reads data from the container at `addr` into a slice.
233
251
///
234
252
/// Returns the number of bytes read. The number of bytes read can be less than the length
235
253
/// 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).
236
258
fn read ( & self , buf : & mut [ u8 ] , addr : A ) -> Result < usize , Self :: E > ;
237
259
238
260
/// Writes the entire content of a slice into the container at `addr`.
239
261
///
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
+ ///
240
265
/// # Errors
241
266
///
242
267
/// 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> {
245
270
246
271
/// Reads data from the container at `addr` to fill an entire slice.
247
272
///
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
+ ///
248
276
/// # Errors
249
277
///
250
278
/// Returns an error if there isn't enough data within the container to fill the entire slice.
0 commit comments