Struct wasmer::WasmPtr[][src]

#[repr(transparent)]
pub struct WasmPtr<T: Copy, Ty = Item> { /* fields omitted */ }
Expand description

A zero-cost type that represents a pointer to something in Wasm linear memory.

This type can be used directly in the host function arguments:

pub fn host_import(memory: Memory, ptr: WasmPtr<u32>) {
    let derefed_ptr = ptr.deref(&memory).expect("pointer in bounds");
    let inner_val: u32 = derefed_ptr.get();
    println!("Got {} from Wasm memory address 0x{:X}", inner_val, ptr.offset());
    // update the value being pointed to
    derefed_ptr.set(inner_val + 1);
}

This type can also be used with primitive-filled structs, but be careful of guarantees required by ValueType.


#[derive(Copy, Clone, Debug)]
#[repr(C)]
struct V3 {
    x: f32,
    y: f32,
    z: f32
}
// This is safe as the 12 bytes represented by this struct
// are valid for all bit combinations.
unsafe impl ValueType for V3 {
}

fn update_vector_3(memory: Memory, ptr: WasmPtr<V3>) {
    let derefed_ptr = ptr.deref(&memory).expect("pointer in bounds");
    let mut inner_val: V3 = derefed_ptr.get();
    println!("Got {:?} from Wasm memory address 0x{:X}", inner_val, ptr.offset());
    // update the value being pointed to
    inner_val.x = 10.4;
    derefed_ptr.set(inner_val);
}

Implementations

impl<T: Copy, Ty> WasmPtr<T, Ty>[src]

Methods relevant to all types of WasmPtr.

pub fn new(offset: u32) -> Self[src]

Create a new WasmPtr at the given offset.

pub fn offset(self) -> u32[src]

Get the offset into Wasm linear memory for this WasmPtr.

impl<T: Copy + ValueType> WasmPtr<T, Item>[src]

Methods for WasmPtrs to data that can be dereferenced, namely to types that implement ValueType, meaning that they’re valid for all possible bit patterns.

pub fn deref<'a>(self, memory: &'a Memory) -> Option<&'a Cell<T>>[src]

Dereference the WasmPtr getting access to a &Cell<T> allowing for reading and mutating of the inner value.

This method is unsound if used with unsynchronized shared memory. If you’re unsure what that means, it likely does not apply to you. This invariant will be enforced in the future.

pub unsafe fn deref_mut<'a>(self, memory: &'a Memory) -> Option<&'a mut Cell<T>>[src]

Mutably dereference this WasmPtr getting a &mut Cell<T> allowing for direct access to a &mut T.

Safety

  • This method does not do any aliasing checks: it’s possible to create &mut T that point to the same memory. You should ensure that you have exclusive access to Wasm linear memory before calling this method.

impl<T: Copy + ValueType> WasmPtr<T, Array>[src]

Methods for WasmPtrs to arrays of data that can be dereferenced, namely to types that implement ValueType, meaning that they’re valid for all possible bit patterns.

pub fn deref(
    self,
    memory: &Memory,
    index: u32,
    length: u32
) -> Option<&[Cell<T>]>
[src]

Dereference the WasmPtr getting access to a &[Cell<T>] allowing for reading and mutating of the inner values.

This method is unsound if used with unsynchronized shared memory. If you’re unsure what that means, it likely does not apply to you. This invariant will be enforced in the future.

pub unsafe fn deref_mut(
    self,
    memory: &Memory,
    index: u32,
    length: u32
) -> Option<&mut [Cell<T>]>
[src]

Mutably dereference this WasmPtr getting a &mut [Cell<T>] allowing for direct access to a &mut [T].

Safety

  • This method does not do any aliasing checks: it’s possible to create &mut T that point to the same memory. You should ensure that you have exclusive access to Wasm linear memory before calling this method.

pub unsafe fn get_utf8_str<'a>(
    self,
    memory: &'a Memory,
    str_len: u32
) -> Option<&'a str>
[src]

Get a UTF-8 string from the WasmPtr with the given length.

Note that . The underlying data can be mutated if the Wasm is allowed to execute or an aliasing WasmPtr is used to mutate memory.

Safety

This method returns a reference to Wasm linear memory. The underlying data can be mutated if the Wasm is allowed to execute or an aliasing WasmPtr is used to mutate memory.

str has invariants that must not be broken by mutating Wasm memory. Thus the caller must ensure that the backing memory is not modified while the reference is held.

Additionally, if memory is dynamic, the caller must also ensure that memory is not grown while the reference is held.

pub fn get_utf8_string(self, memory: &Memory, str_len: u32) -> Option<String>[src]

Get a UTF-8 String from the WasmPtr with the given length.

an aliasing WasmPtr is used to mutate memory.

pub unsafe fn get_utf8_str_with_nul<'a>(
    self,
    memory: &'a Memory
) -> Option<&'a str>
[src]

Get a UTF-8 string from the WasmPtr, where the string is nul-terminated.

Note that this does not account for UTF-8 strings that contain nul themselves, WasmPtr::get_utf8_str has to be used for those.

Safety

This method behaves similarly to WasmPtr::get_utf8_str, all safety invariants on that method must also be upheld here.

pub fn get_utf8_string_with_nul(self, memory: &Memory) -> Option<String>[src]

Get a UTF-8 String from the WasmPtr, where the string is nul-terminated.

Note that this does not account for UTF-8 strings that contain nul themselves, WasmPtr::get_utf8_string has to be used for those.

Trait Implementations

impl<T: Copy, Ty> Clone for WasmPtr<T, Ty>[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<T: Copy, Ty> Debug for WasmPtr<T, Ty>[src]

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

Formats the value using the given formatter. Read more

impl<T: Copy, Ty> FromToNativeWasmType for WasmPtr<T, Ty>[src]

type Native = i32

Native Wasm type.

fn to_native(self) -> Self::Native[src]

Convert self to Self::Native. Read more

fn from_native(n: Self::Native) -> Self[src]

Convert a value of kind Self::Native to Self. Read more

impl<T: Copy, Ty> PartialEq<WasmPtr<T, Ty>> for WasmPtr<T, Ty>[src]

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

This method tests for self and other values to be equal, and is used by ==. Read more

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl<T: Copy, Ty> Copy for WasmPtr<T, Ty>[src]

impl<T: Copy, Ty> Eq for WasmPtr<T, Ty>[src]

impl<T: Copy, Ty> ValueType for WasmPtr<T, Ty>[src]

Auto Trait Implementations

impl<T, Ty> RefUnwindSafe for WasmPtr<T, Ty> where
    T: RefUnwindSafe,
    Ty: RefUnwindSafe

impl<T, Ty> Send for WasmPtr<T, Ty> where
    T: Send,
    Ty: Send

impl<T, Ty> Sync for WasmPtr<T, Ty> where
    T: Sync,
    Ty: Sync

impl<T, Ty> Unpin for WasmPtr<T, Ty> where
    T: Unpin,
    Ty: Unpin

impl<T, Ty> UnwindSafe for WasmPtr<T, Ty> where
    T: UnwindSafe,
    Ty: UnwindSafe

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<Q, K> Equivalent<K> for Q where
    K: Borrow<Q> + ?Sized,
    Q: Eq + ?Sized
[src]

pub fn equivalent(&self, key: &K) -> bool[src]

Compare self to key and return true if they are equal.

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.