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, bytes: &mut [u8]);
    fn write(&self, offset: u64, bytes: &[u8]);

    // Provided method
    unsafe fn read_unsafe(&self, offset: u64, dst: *mut u8, count: usize) { ... }
}
Expand description

Memory

Page-addressed storage with initialized reads and nonshrinking capacity. Implementations validate complete access ranges before touching backing bytes.

Required Methods§

Source

fn size(&self) -> u64

Returns the number of complete accessible Wasm pages.

Source

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

Adds zero-filled pages, returning the previous size or -1 on refusal. Refusal leaves the existing bytes and extent unchanged.

Source

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

Copies the selected backing bytes into an initialized destination.

§Panics

Panics or traps if the complete range is outside the backing.

Source

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

Replaces the selected backing bytes.

§Panics

Panics or traps if the complete range is outside the backing.

Provided Methods§

Source

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

Initializes the destination from the selected backing bytes.

The default implementation admits the entire source range, then reads through a bounded, initialized scratch buffer. Custom backings pay an additional copy per chunk; direct implementations can override this. Failure may leave an initialized prefix in the destination.

§Safety

dst must satisfy the validity, alignment, and exclusive writable access requirements of a Rust slice of count bytes, including for zero length. That destination must not overlap the source or the backing’s own state.

§Panics

Panics or traps if the complete source range is outside the backing or if the backing’s safe read fails.

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, bytes: &mut [u8])

Source§

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

Source§

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

Source§

impl<M: Memory + ?Sized> Memory for &M

Source§

fn size(&self) -> u64

Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl<M: Memory + ?Sized> Memory for Rc<M>

Source§

fn size(&self) -> u64

Source§

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

Source§

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

Source§

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

Source§

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

Implementors§