pub struct Memory { /* private fields */ }
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
sourceimpl Memory
impl Memory
sourcepub fn new(store: &Store, ty: MemoryType) -> Result<Self, MemoryError>
pub fn new(store: &Store, 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(&store, MemoryType::new(1, None, false)).unwrap();
sourcepub fn ty(&self) -> MemoryType
pub fn ty(&self) -> MemoryType
Returns the MemoryType
of the Memory
.
Example
let mt = MemoryType::new(1, None, false);
let m = Memory::new(&store, mt).unwrap();
assert_eq!(m.ty(), mt);
sourcepub unsafe fn data_unchecked(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl Read for &'_ [u8]impl Write for &'_ mut [u8]
pub unsafe fn data_unchecked(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl Read for &'_ [u8]impl Write for &'_ mut [u8]
Retrieve a slice of the memory contents.
Safety
Until the returned slice is dropped, it is undefined behaviour to modify the memory contents in any way including by calling a wasm function that writes to the memory or by resizing the memory.
sourcepub unsafe fn data_unchecked_mut(&self) -> &mut [u8]ⓘNotable traits for &'_ [u8]impl Read for &'_ [u8]impl Write for &'_ mut [u8]
pub unsafe fn data_unchecked_mut(&self) -> &mut [u8]ⓘNotable traits for &'_ [u8]impl Read for &'_ [u8]impl Write for &'_ mut [u8]
Retrieve a mutable slice of the memory contents.
Safety
This method provides interior mutability without an UnsafeCell. Until the returned value is dropped, it is undefined behaviour to read or write to the pointed-to memory in any way except through this slice, including by calling a wasm function that reads the memory contents or by resizing this Memory.
sourcepub fn grow<IntoPages>(&self, delta: IntoPages) -> Result<Pages, MemoryError> where
IntoPages: Into<Pages>,
pub fn grow<IntoPages>(&self, 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(&store, MemoryType::new(1, Some(3), false)).unwrap();
let p = m.grow(2).unwrap();
assert_eq!(p, Pages(1));
assert_eq!(m.size(), Pages(3));
Errors
Returns an error if memory can’t be grown by the specified amount of pages.
let m = Memory::new(&store, MemoryType::new(1, Some(1), false)).unwrap();
// This results in an error: `MemoryError::CouldNotGrow`.
let s = m.grow(1).unwrap();
sourcepub fn view<T: ValueType>(&self) -> MemoryView<'_, T>
pub fn view<T: ValueType>(&self) -> MemoryView<'_, T>
Return a “view” of the currently accessible memory. By
default, the view is unsynchronized, using regular memory
accesses. You can force a memory view to use atomic accesses
by calling the MemoryView::atomically
method.
Notes:
This method is safe (as in, it won’t cause the host to crash or have UB), but it doesn’t obey rust’s rules involving data races, especially concurrent ones. Therefore, if this memory is shared between multiple threads, a single memory location can be mutated concurrently without synchronization.
Usage:
// Without synchronization.
let view: MemoryView<u8> = memory.view();
for byte in view[0x1000 .. 0x1010].iter().map(Cell::get) {
println!("byte: {}", byte);
}
// With synchronization.
let atomic_view = view.atomically();
for byte in atomic_view[0x1000 .. 0x1010].iter().map(|atom| atom.load(Ordering::SeqCst)) {
println!("byte: {}", byte);
}
Trait Implementations
sourceimpl<'a> Exportable<'a> for Memory
impl<'a> Exportable<'a> for Memory
sourcefn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError>
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError>
sourcefn into_weak_instance_ref(&mut self)
fn into_weak_instance_ref(&mut self)
Convert the extern internally to hold a weak reference to the InstanceRef
.
This is useful for preventing cycles, for example for data stored in a
type implementing WasmerEnv
. Read more
sourceimpl MemoryUsage for Memory
impl MemoryUsage for Memory
sourcefn size_of_val(&self, visited: &mut dyn MemoryUsageTracker) -> usize
fn size_of_val(&self, visited: &mut dyn MemoryUsageTracker) -> usize
Returns the size of the referenced value in bytes. Read more
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 = ()
The archived version of the pointer metadata for this type.
pub fn pointer_metadata(
&<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata
pub fn pointer_metadata(
&<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata
Converts some archived metadata to the pointer metadata for itself.
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<F, W, T, D> Deserialize<With<T, W>, D> for F where
W: DeserializeWith<F, T, D>,
D: Fallible + ?Sized,
F: ?Sized,
impl<F, W, T, D> Deserialize<With<T, W>, D> for F where
W: DeserializeWith<F, T, D>,
D: Fallible + ?Sized,
F: ?Sized,
pub fn deserialize(
&self,
deserializer: &mut D
) -> Result<With<T, W>, <D as Fallible>::Error>
pub fn deserialize(
&self,
deserializer: &mut D
) -> Result<With<T, W>, <D as Fallible>::Error>
Deserializes using the given deserializer
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more