pub struct LinearMemory { /* private fields */ }Expand description
Linear memory region for a plugin (WASM model)
Corresponds to Lean: structure LinearMemory
INVARIANTS:
- All reads from uninitialized addresses return 0
- Writes only modify the specified address
- Bounds are immutable after creation
Uses 4KiB paged storage – only touched pages are allocated.
Implementations§
Source§impl LinearMemory
impl LinearMemory
Sourcepub fn empty(size: Size) -> Self
pub fn empty(size: Size) -> Self
Create empty linear memory with given size
Corresponds to Lean: def LinearMemory.empty (size : Nat) : LinearMemory
Sourcepub fn addr_in_bounds(&self, addr: MemAddr, len: Size) -> bool
pub fn addr_in_bounds(&self, addr: MemAddr, len: Size) -> bool
Check if address is within bounds
Corresponds to Lean: def LinearMemory.addr_in_bounds
Sourcepub fn read(&self, addr: MemAddr) -> u8
pub fn read(&self, addr: MemAddr) -> u8
Read byte at address (returns 0 for uninitialized / missing pages)
Corresponds to Lean: def LinearMemory.read (mem : LinearMemory) (addr : MemAddr) : UInt8
Note: Lean uses mem.bytes.getD addr 0 which returns 0 for missing keys.
Sourcepub fn write(&self, addr: MemAddr, val: u8) -> Self
pub fn write(&self, addr: MemAddr, val: u8) -> Self
Write byte at address
Corresponds to Lean: def LinearMemory.write
Returns the new memory state with the byte written.
Sourcepub fn write_mut(&mut self, addr: MemAddr, val: u8)
pub fn write_mut(&mut self, addr: MemAddr, val: u8)
Write byte at address (mutating version)
This is an optimization for when we own the memory.
Sourcepub fn written_bytes(&self) -> usize
pub fn written_bytes(&self) -> usize
Get the number of allocated bytes (pages * PAGE_SIZE)
Sourcepub fn read_range(
&self,
addr: MemAddr,
len: Size,
) -> Result<Vec<u8>, MemoryError>
pub fn read_range( &self, addr: MemAddr, len: Size, ) -> Result<Vec<u8>, MemoryError>
Read a range of bytes
Returns Err if the range would exceed bounds.
§Errors
Returns MemoryError::OutOfBounds if the address plus length exceeds the memory bounds.
Sourcepub fn write_range(
&self,
addr: MemAddr,
data: &[u8],
) -> Result<Self, MemoryError>
pub fn write_range( &self, addr: MemAddr, data: &[u8], ) -> Result<Self, MemoryError>
Write a range of bytes
Returns the new memory state with all bytes written.
§Errors
Returns MemoryError::OutOfBounds if the address plus data length exceeds the memory bounds.
Sourcepub fn write_range_mut(&mut self, addr: MemAddr, data: &[u8])
pub fn write_range_mut(&mut self, addr: MemAddr, data: &[u8])
Write a range of bytes (mutating version)
Trait Implementations§
Source§impl Clone for LinearMemory
impl Clone for LinearMemory
Source§fn clone(&self) -> LinearMemory
fn clone(&self) -> LinearMemory
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for LinearMemory
impl Debug for LinearMemory
Source§impl Default for LinearMemory
impl Default for LinearMemory
Source§impl PartialEq for LinearMemory
impl PartialEq for LinearMemory
Source§fn eq(&self, other: &LinearMemory) -> bool
fn eq(&self, other: &LinearMemory) -> bool
self and other values to be equal, and is used by ==.