Skip to main content

Memory

Struct Memory 

Source
pub struct Memory(/* private fields */);
Expand description

Newtype wrapper around wasmtime::Memory.

Implementations§

Source§

impl Memory

Source

pub fn new(inner: Memory) -> Self

Create a wasm_runtime_layer::Memory-compatible Memory from a wasmtime::Memory.

Source

pub fn into_inner(self) -> Memory

Consume a Memory to obtain the inner wasmtime::Memory.

Methods from Deref<Target = Memory>§

Source

pub fn ty(&self, store: impl AsContext) -> MemoryType

Returns the underlying type of this memory.

§Panics

Panics if this memory doesn’t belong to store.

§Examples
let engine = Engine::default();
let mut store = Store::new(&engine, ());
let module = Module::new(&engine, "(module (memory (export \"mem\") 1))")?;
let instance = Instance::new(&mut store, &module, &[])?;
let memory = instance.get_memory(&mut store, "mem").unwrap();
let ty = memory.ty(&store);
assert_eq!(ty.minimum(), 1);
Source

pub fn read( &self, store: impl AsContext, offset: usize, buffer: &mut [u8], ) -> Result<(), MemoryAccessError>

Safely reads memory contents at the given offset into a buffer.

The entire buffer will be filled.

If offset + buffer.len() exceed the current memory capacity, then the buffer is left untouched and a MemoryAccessError is returned.

§Errors

This function will return an OutOfMemory error when memory allocation fails. See the OutOfMemory type’s documentation for details on Wasmtime’s out-of-memory handling.

§Panics

Panics if this memory doesn’t belong to store.

Source

pub fn write( &self, store: impl AsContextMut, offset: usize, buffer: &[u8], ) -> Result<(), MemoryAccessError>

Safely writes contents of a buffer to this memory at the given offset.

If the offset + buffer.len() exceeds the current memory capacity, then none of the buffer is written to memory and a MemoryAccessError is returned.

§Errors

This function will return an OutOfMemory error when memory allocation fails. See the OutOfMemory type’s documentation for details on Wasmtime’s out-of-memory handling.

§Panics

Panics if this memory doesn’t belong to store.

Source

pub fn data<'a, T>(&self, store: impl Into<StoreContext<'a, T>>) -> &'a [u8]
where T: 'static,

Returns this memory as a native Rust slice.

Note that this method will consider the entire store context provided as borrowed for the duration of the lifetime of the returned slice.

§Panics

Panics if this memory doesn’t belong to store.

Source

pub fn data_mut<'a, T>( &self, store: impl Into<StoreContextMut<'a, T>>, ) -> &'a mut [u8]
where T: 'static,

Returns this memory as a native Rust mutable slice.

Note that this method will consider the entire store context provided as borrowed for the duration of the lifetime of the returned slice.

§Panics

Panics if this memory doesn’t belong to store.

Source

pub fn data_and_store_mut<'a, T>( &self, store: impl Into<StoreContextMut<'a, T>>, ) -> (&'a mut [u8], &'a mut T)
where T: 'static,

Same as Memory::data_mut, but also returns the T from the StoreContextMut.

This method can be used when you want to simultaneously work with the T in the store as well as the memory behind this Memory. Using Memory::data_mut would consider the entire store borrowed, whereas this method allows the Rust compiler to see that the borrow of this memory and the borrow of T are disjoint.

§Panics

Panics if this memory doesn’t belong to store.

Source

pub fn data_ptr(&self, store: impl AsContext) -> *mut u8

Returns the base pointer, in the host’s address space, that the memory is located at.

For more information and examples see the documentation on the Memory type.

§Panics

Panics if this memory doesn’t belong to store.

Source

pub fn data_size(&self, store: impl AsContext) -> usize

Returns the byte length of this memory.

WebAssembly memories are made up of a whole number of pages, so the byte size returned will always be a multiple of this memory’s page size. Note that different Wasm memories may have different page sizes. You can get a memory’s page size via the Memory::page_size method.

By default the page size is 64KiB (aka 0x10000, 2**16, 1<<16, or 65536) but the custom-page-sizes proposal allows a memory to opt into a page size of 1. Future extensions might allow any power of two as a page size.

For more information and examples see the documentation on the Memory type.

§Panics

Panics if this memory doesn’t belong to store.

Source

pub fn size(&self, store: impl AsContext) -> u64

Returns the size, in units of pages, of this Wasm memory.

WebAssembly memories are made up of a whole number of pages, so the byte size returned will always be a multiple of this memory’s page size. Note that different Wasm memories may have different page sizes. You can get a memory’s page size via the Memory::page_size method.

By default the page size is 64KiB (aka 0x10000, 2**16, 1<<16, or 65536) but the custom-page-sizes proposal allows a memory to opt into a page size of 1. Future extensions might allow any power of two as a page size.

§Panics

Panics if this memory doesn’t belong to store.

Source

pub fn page_size(&self, store: impl AsContext) -> u64

Returns the size of a page, in bytes, for this memory.

WebAssembly memories are made up of a whole number of pages, so the byte size (as returned by Memory::data_size) will always be a multiple of their page size. Different Wasm memories may have different page sizes.

By default this is 64KiB (aka 0x10000, 2**16, 1<<16, or 65536) but the custom-page-sizes proposal allows opting into a page size of 1. Future extensions might allow any power of two as a page size.

Source

pub fn page_size_log2(&self, store: impl AsContext) -> u8

Returns the log2 of this memory’s page size, in bytes.

WebAssembly memories are made up of a whole number of pages, so the byte size (as returned by Memory::data_size) will always be a multiple of their page size. Different Wasm memories may have different page sizes.

By default the page size is 64KiB (aka 0x10000, 2**16, 1<<16, or 65536) but the custom-page-sizes proposal allows opting into a page size of 1. Future extensions might allow any power of two as a page size.

Source

pub fn grow(&self, store: impl AsContextMut, delta: u64) -> Result<u64, Error>

Grows this WebAssembly memory by delta pages.

This will attempt to add delta more pages of memory on to the end of this Memory instance. If successful this may relocate the memory and cause Memory::data_ptr to return a new value. Additionally any unsafely constructed slices into this memory may no longer be valid.

On success returns the number of pages this memory previously had before the growth succeeded.

Note that, by default, a WebAssembly memory’s page size is 64KiB (aka 65536 or 216). The custom-page-sizes proposal allows Wasm memories to opt into a page size of one byte (and this may be further relaxed to any power of two in a future extension).

§Errors

Returns an error if memory could not be grown, for example if it exceeds the maximum limits of this memory. A ResourceLimiter is another example of preventing a memory to grow.

This function will return an error if the Store has a ResourceLimiterAsync (see also: Store::limiter_async. When using an async resource limiter, use [Memory::grow_async] instead.

This function will return an OutOfMemory error when memory allocation fails. See the OutOfMemory type’s documentation for details on Wasmtime’s out-of-memory handling.

§Panics

Panics if this memory doesn’t belong to store.

§Examples
let engine = Engine::default();
let mut store = Store::new(&engine, ());
let module = Module::new(&engine, "(module (memory (export \"mem\") 1 2))")?;
let instance = Instance::new(&mut store, &module, &[])?;
let memory = instance.get_memory(&mut store, "mem").unwrap();

assert_eq!(memory.size(&store), 1);
assert_eq!(memory.grow(&mut store, 1)?, 1);
assert_eq!(memory.size(&store), 2);
assert!(memory.grow(&mut store, 1).is_err());
assert_eq!(memory.size(&store), 2);
assert_eq!(memory.grow(&mut store, 0)?, 2);

Trait Implementations§

Source§

impl AsMut<Memory> for Memory

Source§

fn as_mut(&mut self) -> &mut Memory

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<Memory> for Memory

Source§

fn as_mut(&mut self) -> &mut Memory

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<Memory> for Memory

Source§

fn as_ref(&self) -> &Memory

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Memory> for Memory

Source§

fn as_ref(&self) -> &Memory

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Memory

Source§

fn clone(&self) -> Memory

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Deref for Memory

Source§

type Target = Memory

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for Memory

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl From<Memory> for Memory

Source§

fn from(inner: Memory) -> Self

Converts to this type from the input type.
Source§

impl From<Memory> for Memory

Source§

fn from(wrapper: Memory) -> Self

Converts to this type from the input type.
Source§

impl RefCast for Memory

Source§

type From = Memory

Source§

fn ref_cast(_from: &Self::From) -> &Self

Source§

fn ref_cast_mut(_from: &mut Self::From) -> &mut Self

Source§

impl WasmMemory<Engine> for Memory

Source§

fn new(ctx: impl AsContextMut<Engine>, ty: MemoryType) -> Result<Self>

Creates a new linear memory to the store.
Source§

fn ty(&self, ctx: impl AsContext<Engine>) -> MemoryType

Returns the memory type of the linear memory.
Source§

fn grow(&self, ctx: impl AsContextMut<Engine>, additional: u32) -> Result<u32>

Grows the linear memory by the given amount of new pages.
Source§

fn current_pages(&self, ctx: impl AsContext<Engine>) -> u32

Returns the amount of pages in use by the linear memory.
Source§

fn read( &self, ctx: impl AsContext<Engine>, offset: usize, buffer: &mut [u8], ) -> Result<()>

Reads n bytes from memory[offset..offset+n] into buffer where n is the length of buffer.
Source§

fn write( &self, ctx: impl AsContextMut<Engine>, offset: usize, buffer: &[u8], ) -> Result<()>

Writes n bytes to memory[offset..offset+n] from buffer where n if the length of buffer.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.