Skip to main content

Memory

Struct Memory 

Source
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

Source

pub fn new(store: &mut Store, ty: MemoryType) -> Result<Self>

Create a new memory in the given store.

Source

pub fn cursor<'a>(&self, store: &'a mut Store) -> Result<MemoryCursor<'a>>

Available on crate feature std only.

Creates a cursor positioned at the start of this memory.

Available with the std feature enabled.

Source

pub fn cursor_at<'a>( &self, store: &'a mut Store, position: u64, ) -> Result<MemoryCursor<'a>>

Available on crate feature std only.

Creates a cursor positioned at position bytes from the start of this memory.

Available with the std feature enabled.

Source

pub fn len(&self, store: &Store) -> Result<usize>

Returns the raw memory byte length.

Source

pub fn ty(&self, store: &Store) -> Result<MemoryType>

Returns the memory type, including page size and limits.

Source

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.

Source

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.

Source

pub fn read_exact( &self, store: &Store, offset: usize, dst: &mut [u8], ) -> Result<()>

Reads exactly dst.len() bytes from memory.

Source

pub fn read_vec( &self, store: &Store, offset: usize, len: usize, ) -> Result<Vec<u8>>

Reads len bytes from memory into a newly allocated buffer.

Source

pub fn grow(&self, store: &mut Store, delta_pages: i64) -> Result<Option<i64>>

Grow the memory by the given number of pages.

Source

pub fn page_count(&self, store: &Store) -> Result<usize>

Get the current size of the memory in pages.

Source

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.

Source

pub fn fill( &self, store: &mut Store, offset: usize, len: usize, val: u8, ) -> Result<()>

Fill a slice of memory with a value.

Source

pub fn copy_from_slice( &self, store: &mut Store, offset: usize, data: &[u8], ) -> Result<()>

Copies a full slice into memory.

Source

pub fn write_cstring( &self, store: &mut Store, offset: usize, string: &CString, ) -> Result<()>

Copies a nul-terminated C string into memory.

Source

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.

Source

pub fn read_cstring( &self, store: &Store, offset: usize, len: usize, ) -> Result<CString>

Reads a C-style string from memory.

Source

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.

Source

pub fn read_string( &self, store: &Store, offset: usize, len: usize, ) -> Result<String>

Reads a UTF-8 string from memory.

Source

pub fn read_js_string( &self, store: &Store, offset: usize, len: usize, ) -> Result<String>

Reads a JavaScript-style utf-16 string from memory.

Trait Implementations§

Source§

impl Clone for Memory

Source§

fn clone(&self) -> Memory

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Memory

Source§

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

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

impl From<Memory> for Extern

Source§

fn from(value: Memory) -> Self

Converts to this type from the input type.
Source§

impl Hash for Memory

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Memory

Source§

fn eq(&self, other: &Memory) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Memory

Source§

impl Eq for Memory

Source§

impl StructuralPartialEq for Memory

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.