Safely reads memory contents at the given offset into a buffer. The entire buffer will be filled. If `offset + buffer.len()` exceed the current memory capacity, then the buffer is left untouched and a [`MemoryAccessError`] is returned. # Errors This function will return an [`OutOfMemory`][crate::OutOfMemory] error when memory allocation fails. See the `OutOfMemory` type's documentation for det
(
&self,
store: impl AsContext,
offset: usize,
buffer: &mut [u8],
)
| 352 | /// |
| 353 | /// Panics if this memory doesn't belong to `store`. |
| 354 | pub fn read( |
| 355 | &self, |
| 356 | store: impl AsContext, |
| 357 | offset: usize, |
| 358 | buffer: &mut [u8], |
| 359 | ) -> Result<(), MemoryAccessError> { |
| 360 | let store = store.as_context(); |
| 361 | let slice = self |
| 362 | .data(&store) |
| 363 | .get(offset..) |
| 364 | .and_then(|s| s.get(..buffer.len())) |
| 365 | .ok_or(MemoryAccessError { _private: () })?; |
| 366 | buffer.copy_from_slice(slice); |
| 367 | Ok(()) |
| 368 | } |
| 369 | |
| 370 | /// Safely writes contents of a buffer to this memory at the given offset. |
| 371 | /// |
no test coverage detected