Struct wasmer::Memory[][src]

pub struct Memory { /* fields omitted */ }
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

impl Memory[src]

pub fn new(store: &Store, ty: MemoryType) -> Result<Self, MemoryError>[src]

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();

pub fn ty(&self) -> MemoryType[src]

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);

pub fn store(&self) -> &Store[src]

Returns the Store where the Memory belongs.

Example

let m = Memory::new(&store, MemoryType::new(1, None, false)).unwrap();

assert_eq!(m.store(), &store);

pub unsafe fn data_unchecked(&self) -> &[u8]

Notable traits for &'_ [u8]

impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
[src]

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.

pub unsafe fn data_unchecked_mut(&self) -> &mut [u8]

Notable traits for &'_ [u8]

impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
[src]

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.

pub fn data_ptr(&self) -> *mut u8[src]

Returns the pointer to the raw bytes of the Memory.

pub fn data_size(&self) -> u64[src]

Returns the size (in bytes) of the Memory.

pub fn size(&self) -> Pages[src]

Returns the size (in Pages) of the Memory.

Example

let m = Memory::new(&store, MemoryType::new(1, None, false)).unwrap();

assert_eq!(m.size(), Pages(1));

pub fn grow<IntoPages>(&self, delta: IntoPages) -> Result<Pages, MemoryError> where
    IntoPages: Into<Pages>, 
[src]

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();

pub fn view<T: ValueType>(&self) -> MemoryView<'_, T>[src]

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);
}

pub fn same(&self, other: &Self) -> bool[src]

Returns whether or not these two memories refer to the same data.

Example

let m = Memory::new(&store, MemoryType::new(1, None, false)).unwrap();

assert!(m.same(&m));

Trait Implementations

impl Clone for Memory[src]

fn clone(&self) -> Self[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Memory[src]

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

Formats the value using the given formatter. Read more

impl<'a> Exportable<'a> for Memory[src]

fn to_export(&self) -> Export[src]

This function is used when providedd the Extern as exportable, so it can be used while instantiating the Module. Read more

fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError>[src]

Implementation of how to get the export corresponding to the implementing type from an Instance by name. Read more

fn into_weak_instance_ref(&mut self)[src]

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

impl From<Memory> for Extern[src]

fn from(r: Memory) -> Self[src]

Performs the conversion.

impl MemoryUsage for Memory[src]

fn size_of_val(&self, visited: &mut dyn MemoryUsageTracker) -> usize[src]

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> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> ArchivePointee for T

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.

pub fn pointer_metadata(
    &<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Pointable for T

pub const ALIGN: usize

The alignment of pointer.

type Init = T

The type for initializers.

pub unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more

pub unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more

pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more

pub unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more

impl<T> Pointee for T

type Metadata = ()

The type for metadata in pointers and references to Self.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.