Skip to main content

Memory

Trait Memory 

Source
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§

Source

fn size(&self) -> u64

Returns the current size of the memory in WebAssembly pages.

One WebAssembly page is 64 KiB.

Source

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.

Source

fn read(&self, offset: u64, dst: &mut [u8])

Copies dst.len() bytes from memory starting at offset into dst.

Panics or traps if the read would go out of bounds.

Source

fn write(&self, offset: u64, src: &[u8])

Copies src.len() bytes from src into memory starting at offset.

Panics or traps if the write would go out of bounds.

Provided Methods§

Source

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:

  • dst points to valid writable memory of at least count bytes.
  • The memory range dst..dst+count does not overlap with self.

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".

Implementations on Foreign Types§

Source§

impl Memory for RefCell<Vec<u8>>

Source§

fn size(&self) -> u64

Source§

fn grow(&self, pages: u64) -> i64

Source§

fn read(&self, offset: u64, dst: &mut [u8])

Source§

unsafe fn read_unsafe(&self, offset: u64, dst: *mut u8, count: usize)

Source§

fn write(&self, offset: u64, src: &[u8])

Source§

impl<M> Memory for Rc<M>
where M: Memory,

Source§

fn size(&self) -> u64

Source§

fn grow(&self, pages: u64) -> i64

Source§

fn read(&self, offset: u64, dst: &mut [u8])

Source§

unsafe fn read_unsafe(&self, offset: u64, dst: *mut u8, count: usize)

Source§

fn write(&self, offset: u64, src: &[u8])

Implementors§

Source§

impl Memory for FileMemory

Source§

impl<M> Memory for VirtualMemory<M>
where M: Memory,

Source§

impl<M> Memory for RestrictedMemory<M>
where M: Memory,