@@ -227,16 +227,38 @@ 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!(slice.write(&[1, 2, 3], 5).unwrap_err(), VolatileMemoryError::OutOfBounds {addr: 5}));
243
+ /// assert_eq!(slice.write(&[], 5).unwrap(), 0);
244
+ /// ```
230
245
fn write ( & self , buf : & [ u8 ] , addr : A ) -> Result < usize , Self :: E > ;
231
246
232
247
/// Reads data from the container at `addr` into a slice.
233
248
///
234
249
/// Returns the number of bytes read. The number of bytes read can be less than the length
235
250
/// 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).
236
255
fn read ( & self , buf : & mut [ u8 ] , addr : A ) -> Result < usize , Self :: E > ;
237
256
238
257
/// Writes the entire content of a slice into the container at `addr`.
239
258
///
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
+ ///
240
262
/// # Errors
241
263
///
242
264
/// 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> {
245
267
246
268
/// Reads data from the container at `addr` to fill an entire slice.
247
269
///
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
+ ///
248
273
/// # Errors
249
274
///
250
275
/// Returns an error if there isn't enough data within the container to fill the entire slice.
0 commit comments