pub struct Memory(/* private fields */);Expand description
A memory instance in a store.
§Example
use tinywasm::types::MemoryType;
use tinywasm::{Memory, Store};
let mut store = Store::default();
let memory = Memory::new(&mut store, MemoryType::default().with_page_count_initial(1))?;
memory.copy_from_slice(&mut store, 0, b"hi")?;
assert_eq!(memory.read_vec(&store, 0, 2)?, b"hi");
assert_eq!(memory.page_count(&store)?, 1);
memory.grow(&mut store, 1)?;
assert_eq!(memory.page_count(&store)?, 2);Implementations§
Source§impl Memory
impl Memory
Sourcepub fn new(store: &mut Store, ty: MemoryType) -> Result<Self>
pub fn new(store: &mut Store, ty: MemoryType) -> Result<Self>
Create a new memory in the given store.
Sourcepub fn cursor<'a>(&self, store: &'a mut Store) -> Result<MemoryCursor<'a>>
Available on crate feature std only.
pub fn cursor<'a>(&self, store: &'a mut Store) -> Result<MemoryCursor<'a>>
std only.Creates a cursor positioned at the start of this memory.
Available with the std feature enabled.
Sourcepub fn cursor_at<'a>(
&self,
store: &'a mut Store,
position: u64,
) -> Result<MemoryCursor<'a>>
Available on crate feature std only.
pub fn cursor_at<'a>( &self, store: &'a mut Store, position: u64, ) -> Result<MemoryCursor<'a>>
std only.Creates a cursor positioned at position bytes from the start of this memory.
Available with the std feature enabled.
Sourcepub fn ty(&self, store: &Store) -> Result<MemoryType>
pub fn ty(&self, store: &Store) -> Result<MemoryType>
Returns the memory type, including page size and limits.
Sourcepub fn read(
&self,
store: &Store,
offset: usize,
dst: &mut [u8],
) -> Result<usize>
pub fn read( &self, store: &Store, offset: usize, dst: &mut [u8], ) -> Result<usize>
Reads up to dst.len() bytes from memory and returns the number of bytes read.
Depending on the configured backend, this may return fewer bytes than requested even when
more data is available. Use Self::read_exact or Self::read_vec when you need a full
range.
Sourcepub fn write(
&self,
store: &mut Store,
offset: usize,
src: &[u8],
) -> Result<usize>
pub fn write( &self, store: &mut Store, offset: usize, src: &[u8], ) -> Result<usize>
Writes up to src.len() bytes into memory and returns the number of bytes written.
Depending on the configured backend, this may return fewer bytes than requested even when
more space is available. Use Self::copy_from_slice when you need the full slice written.
Sourcepub fn read_exact(
&self,
store: &Store,
offset: usize,
dst: &mut [u8],
) -> Result<()>
pub fn read_exact( &self, store: &Store, offset: usize, dst: &mut [u8], ) -> Result<()>
Reads exactly dst.len() bytes from memory.
Sourcepub fn read_vec(
&self,
store: &Store,
offset: usize,
len: usize,
) -> Result<Vec<u8>>
pub fn read_vec( &self, store: &Store, offset: usize, len: usize, ) -> Result<Vec<u8>>
Reads len bytes from memory into a newly allocated buffer.
Sourcepub fn grow(&self, store: &mut Store, delta_pages: i64) -> Result<Option<i64>>
pub fn grow(&self, store: &mut Store, delta_pages: i64) -> Result<Option<i64>>
Grow the memory by the given number of pages.
Sourcepub fn page_count(&self, store: &Store) -> Result<usize>
pub fn page_count(&self, store: &Store) -> Result<usize>
Get the current size of the memory in pages.
Sourcepub fn copy_within(
&self,
store: &mut Store,
src: usize,
dst: usize,
len: usize,
) -> Result<()>
pub fn copy_within( &self, store: &mut Store, src: usize, dst: usize, len: usize, ) -> Result<()>
Copy a slice of memory to another place in memory.
Sourcepub fn fill(
&self,
store: &mut Store,
offset: usize,
len: usize,
val: u8,
) -> Result<()>
pub fn fill( &self, store: &mut Store, offset: usize, len: usize, val: u8, ) -> Result<()>
Fill a slice of memory with a value.
Sourcepub fn copy_from_slice(
&self,
store: &mut Store,
offset: usize,
data: &[u8],
) -> Result<()>
pub fn copy_from_slice( &self, store: &mut Store, offset: usize, data: &[u8], ) -> Result<()>
Copies a full slice into memory.
Sourcepub fn write_cstring(
&self,
store: &mut Store,
offset: usize,
string: &CString,
) -> Result<()>
pub fn write_cstring( &self, store: &mut Store, offset: usize, string: &CString, ) -> Result<()>
Copies a nul-terminated C string into memory.
Sourcepub fn write_cstring_bytes(
&self,
store: &mut Store,
offset: usize,
string: &str,
) -> Result<()>
pub fn write_cstring_bytes( &self, store: &mut Store, offset: usize, string: &str, ) -> Result<()>
Copies a UTF-8 string into memory and appends a trailing nul byte.
Sourcepub fn read_cstring(
&self,
store: &Store,
offset: usize,
len: usize,
) -> Result<CString>
pub fn read_cstring( &self, store: &Store, offset: usize, len: usize, ) -> Result<CString>
Reads a C-style string from memory.
Sourcepub fn read_cstring_until_null(
&self,
store: &Store,
offset: usize,
max_len: usize,
) -> Result<CString>
pub fn read_cstring_until_null( &self, store: &Store, offset: usize, max_len: usize, ) -> Result<CString>
Reads a C-style string from memory, stopping at the first null byte.