wasm_dbms_memory/memory_access.rs
1// Rust guideline compliant 2026-03-01
2// X-WHERE-CLAUSE, M-CANONICAL-DOCS
3
4//! Trait abstracting page-level memory operations.
5//!
6//! [`MemoryAccess`] is the primary interface consumed by registry and
7//! ledger code in this crate. [`MemoryManager`](crate::MemoryManager)
8//! implements it with direct writes; the DBMS crate provides a
9//! journaled wrapper that records original bytes before each write.
10
11use wasm_dbms_api::prelude::{Encode, MemoryResult, Page, PageOffset};
12
13/// Abstraction over page-level memory operations.
14///
15/// All table-registry and ledger functions are generic over this trait
16/// so that callers can transparently add write-ahead journaling or
17/// other interceptors without modifying the memory crate.
18pub trait MemoryAccess {
19 /// Returns the size of a single memory page.
20 fn page_size(&self) -> u64;
21
22 /// Allocates an additional page in memory and returns its number.
23 fn allocate_page(&mut self) -> MemoryResult<Page>;
24
25 /// Reads a typed value from the specified page and offset.
26 fn read_at<D>(&self, page: Page, offset: PageOffset) -> MemoryResult<D>
27 where
28 D: Encode;
29
30 /// Writes a typed value at the specified page and offset.
31 fn write_at<E>(&mut self, page: Page, offset: PageOffset, data: &E) -> MemoryResult<()>
32 where
33 E: Encode;
34
35 /// Zeros out the region occupied by `data` at the specified page
36 /// and offset.
37 fn zero<E>(&mut self, page: Page, offset: PageOffset, data: &E) -> MemoryResult<()>
38 where
39 E: Encode;
40
41 /// Reads raw bytes into `buf` at the specified page and offset.
42 ///
43 /// Returns the number of bytes actually read.
44 fn read_at_raw(&self, page: Page, offset: PageOffset, buf: &mut [u8]) -> MemoryResult<usize>;
45}