pub trait Memory {
// Required methods
fn size(&self) -> u64;
fn grow(&self, pages: u64) -> i64;
fn read(&self, offset: u64, dst: &mut [u8]);
fn write(&self, offset: u64, src: &[u8]);
// Provided method
unsafe fn read_unsafe(&self, offset: u64, dst: *mut u8, count: usize) { ... }
}Expand description
Abstraction over a WebAssembly-style linear memory (e.g., stable memory).
Implementations are expected to mirror WebAssembly semantics: out-of-bounds accesses will cause a panic (in native) or trap (in Wasm).
Required Methods§
Sourcefn size(&self) -> u64
fn size(&self) -> u64
Returns the current size of the memory in WebAssembly pages.
One WebAssembly page is 64 KiB.
Sourcefn grow(&self, pages: u64) -> i64
fn grow(&self, pages: u64) -> i64
Grows the memory by pages pages filled with zeroes.
Returns the previous size in pages on success, or -1 if the memory could not be grown.
Provided Methods§
Sourceunsafe fn read_unsafe(&self, offset: u64, dst: *mut u8, count: usize)
unsafe fn read_unsafe(&self, offset: u64, dst: *mut u8, count: usize)
Unsafe variant of read for advanced use.
Copies count bytes from memory starting at offset into the
raw pointer dst. Initializes the destination before reading.
§Safety
Caller must ensure:
dstpoints to valid writable memory of at leastcountbytes.- The memory range
dst..dst+countdoes not overlap withself.
Panics or traps if the read would go out of bounds.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".