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

Methods relevant to all types of WasmPtr.

Create a new WasmPtr at the given offset.

Get the offset into Wasm linear memory for this WasmPtr.

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.

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.

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.

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.

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.

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.

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.

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

an aliasing WasmPtr is used to mutate memory.

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.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Native Wasm type.

Convert self to Self::Native. Read more

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

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

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

The archived version of the pointer metadata for this type.

Converts some archived metadata to the pointer metadata for itself.

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

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

Performs the conversion.

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

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

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

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

The type for metadata in pointers and references to Self.

The resulting type after obtaining ownership.

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

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

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.