Skip to main content

WasiMemoryProvider

Struct WasiMemoryProvider 

Source
pub struct WasiMemoryProvider { /* private fields */ }
Expand description

File-backed MemoryProvider for WASI runtimes.

Persists database pages as a single flat file on the filesystem. Each page is 64 KiB, matching the WASM memory page size. The file layout is byte-for-byte equivalent to IC stable memory, enabling portable database snapshots.

Single-writer access is the caller’s responsibility. WASM is single-threaded by default and WASI lock support varies across runtimes.

§Examples

use wasi_dbms_memory::WasiMemoryProvider;
use wasm_dbms_memory::MemoryProvider;

let mut provider = WasiMemoryProvider::new("./data/mydb.bin").unwrap();
provider.grow(1).unwrap(); // allocate 1 page (64 KiB)

let data = b"hello";
provider.write(0, data).unwrap();

let mut buf = vec![0u8; 5];
provider.read(0, &mut buf).unwrap();
assert_eq!(&buf, data);

Implementations§

Source§

impl WasiMemoryProvider

Source

pub fn new(path: impl AsRef<Path>) -> MemoryResult<Self>

Opens or creates a file-backed memory provider at path.

If the file exists, the page count is inferred from the file size. If the file does not exist, it is created empty (0 pages).

§Errors

Returns MemoryError::ProviderError if:

  • The file cannot be opened or created.
  • The existing file size is not a multiple of the page size (64 KiB).
Source

pub fn path(&self) -> &Path

Returns the path to the backing file.

Trait Implementations§

Source§

impl Debug for WasiMemoryProvider

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl MemoryProvider for WasiMemoryProvider

Source§

const PAGE_SIZE: u64 = PAGE_SIZE

The size of a memory page in bytes.
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). Read more
Source§

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

Reads data from memory starting at offset into the provided buffer buf. Read more
Source§

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

Writes data from the provided buffer buf into memory starting at offset. Read more
Source§

impl TryFrom<&Path> for WasiMemoryProvider

Source§

type Error = MemoryError

The type returned in the event of a conversion error.
Source§

fn try_from(path: &Path) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<PathBuf> for WasiMemoryProvider

Source§

type Error = MemoryError

The type returned in the event of a conversion error.
Source§

fn try_from(path: PathBuf) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.