pub struct Memory(_);
Expand description
A WebAssembly memory
instance.
A memory instance is the runtime representation of a linear memory. It consists of a vector of bytes and an optional maximum size.
The length of the vector always is a multiple of the WebAssembly page size, which is defined to be the constant 65536 – abbreviated 64Ki. Like in a memory type, the maximum size in a memory instance is given in units of this page size.
A memory created by the host or in WebAssembly code will be accessible and mutable from both host and WebAssembly.
Spec: https://webassembly.github.io/spec/core/exec/runtime.html#memory-instances
Implementations§
source§impl Memory
impl Memory
sourcepub fn new(
store: &mut impl AsStoreMut,
ty: MemoryType
) -> Result<Self, MemoryError>
pub fn new( store: &mut impl AsStoreMut, ty: MemoryType ) -> Result<Self, MemoryError>
Creates a new host Memory
from the provided MemoryType
.
This function will construct the Memory
using the store
BaseTunables
.
Example
let m = Memory::new(&mut store, MemoryType::new(1, None, false)).unwrap();
sourcepub fn new_from_existing(
new_store: &mut impl AsStoreMut,
memory: VMMemory
) -> Self
pub fn new_from_existing( new_store: &mut impl AsStoreMut, memory: VMMemory ) -> Self
Create a memory object from an existing memory and attaches it to the store
sourcepub fn ty(&self, store: &impl AsStoreRef) -> MemoryType
pub fn ty(&self, store: &impl AsStoreRef) -> MemoryType
Returns the MemoryType
of the Memory
.
Example
let mt = MemoryType::new(1, None, false);
let m = Memory::new(&mut store, mt).unwrap();
assert_eq!(m.ty(&mut store), mt);
sourcepub fn view<'a>(&self, store: &'a impl AsStoreRef) -> MemoryView<'a>
pub fn view<'a>(&self, store: &'a impl AsStoreRef) -> MemoryView<'a>
Creates a view into the memory that then allows for read and write
sourcepub fn grow<IntoPages>(
&self,
store: &mut impl AsStoreMut,
delta: IntoPages
) -> Result<Pages, MemoryError>where
IntoPages: Into<Pages>,
pub fn grow<IntoPages>( &self, store: &mut impl AsStoreMut, delta: IntoPages ) -> Result<Pages, MemoryError>where IntoPages: Into<Pages>,
Grow memory by the specified amount of WebAssembly Pages
and return
the previous memory size.
Example
let m = Memory::new(&mut store, MemoryType::new(1, Some(3), false)).unwrap();
let p = m.grow(&mut store, 2).unwrap();
assert_eq!(p, Pages(1));
assert_eq!(m.view(&mut store).size(), Pages(3));
Errors
Returns an error if memory can’t be grown by the specified amount of pages.
let m = Memory::new(&mut store, MemoryType::new(1, Some(1), false)).unwrap();
// This results in an error: `MemoryError::CouldNotGrow`.
let s = m.grow(&mut store, 1).unwrap();
sourcepub fn copy_to_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut
) -> Result<Self, MemoryError>
pub fn copy_to_store( &self, store: &impl AsStoreRef, new_store: &mut impl AsStoreMut ) -> Result<Self, MemoryError>
Copies the memory to a new store and returns a memory reference to it
sourcepub fn is_from_store(&self, store: &impl AsStoreRef) -> bool
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool
Checks whether this Memory
can be used with the given context.
sourcepub fn try_clone(&self, store: &impl AsStoreRef) -> Option<VMMemory>
pub fn try_clone(&self, store: &impl AsStoreRef) -> Option<VMMemory>
Attempts to clone this memory (if its clonable)
sourcepub fn clone_in_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut
) -> Option<Self>
pub fn clone_in_store( &self, store: &impl AsStoreRef, new_store: &mut impl AsStoreMut ) -> Option<Self>
Attempts to clone this memory (if its clonable) in a new store
sourcepub fn duplicate_in_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut
) -> Option<Self>
pub fn duplicate_in_store( &self, store: &impl AsStoreRef, new_store: &mut impl AsStoreMut ) -> Option<Self>
Attempts to duplicate this memory (if its clonable) in a new store
Trait Implementations§
source§impl<'a> Exportable<'a> for Memory
impl<'a> Exportable<'a> for Memory
source§fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError>
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError>
Instance
by name.source§impl PartialEq<Memory> for Memory
impl PartialEq<Memory> for Memory
impl Eq for Memory
impl StructuralPartialEq for Memory
Auto Trait Implementations§
impl RefUnwindSafe for Memory
impl Send for Memory
impl Sync for Memory
impl Unpin for Memory
impl UnwindSafe for Memory
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere
W: DeserializeWith<F, T, D>,
D: Fallible + ?Sized,
F: ?Sized,
impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,
§fn deserialize(
&self,
deserializer: &mut D
) -> Result<With<T, W>, <D as Fallible>::Error>
fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.