Skip to main content

MemoryProvider

Trait MemoryProvider 

Source
pub trait MemoryProvider {
    const PAGE_SIZE: u64;

    // Required methods
    fn size(&self) -> u64;
    fn pages(&self) -> u64;
    fn grow(&mut self, new_pages: u64) -> MemoryResult<u64>;
    fn read(&mut self, offset: u64, buf: &mut [u8]) -> MemoryResult<()>;
    fn write(&mut self, offset: u64, buf: &[u8]) -> MemoryResult<()>;
}
Expand description

Memory Provider trait defines the interface for interacting with the underlying memory.

Abstracting memory access allows different implementations for production (e.g. stable memory) and testing (heap-based).

Required Associated Constants§

Source

const PAGE_SIZE: u64

The size of a memory page in bytes.

Required Methods§

Source

fn size(&self) -> u64

Gets the current size of the memory in bytes.

Source

fn pages(&self) -> u64

Gets the amount of pages currently allocated.

Source

fn grow(&mut self, new_pages: u64) -> MemoryResult<u64>

Attempts to grow the memory by new_pages (added pages).

Returns an error if it wasn’t possible. Otherwise, returns the previous size in bytes that was reserved.

Actual reserved size after the growth will be previous_size + (new_pages * PAGE_SIZE).

Source

fn read(&mut self, offset: u64, buf: &mut [u8]) -> MemoryResult<()>

Reads data from memory starting at offset into the provided buffer buf.

Returns an error if offset + buf.len() exceeds the current memory size.

Source

fn write(&mut self, offset: u64, buf: &[u8]) -> MemoryResult<()>

Writes data from the provided buffer buf into memory starting at offset.

Returns an error if offset + buf.len() exceeds the current memory size.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§