Struct wasmer::WasmPtr

source ·
#[repr(transparent)]
pub struct WasmPtr<T, M: MemorySize = Memory32> { /* private fields */ }
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(mut env: FunctionEnvMut<()>, memory: Memory, ptr: WasmPtr<u32>) {
    let memory = memory.view(&env);
    let derefed_ptr = ptr.deref(&memory);
    let inner_val: u32 = derefed_ptr.read().expect("pointer in bounds");
    println!("Got {} from Wasm memory address 0x{:X}", inner_val, ptr.offset());
    // update the value being pointed to
    derefed_ptr.write(inner_val + 1).expect("pointer in bounds");
}

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


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

fn update_vector_3(mut env: FunctionEnvMut<()>, memory: Memory, ptr: WasmPtr<V3>) {
    let memory = memory.view(&env);
    let derefed_ptr = ptr.deref(&memory);
    let mut inner_val: V3 = derefed_ptr.read().expect("pointer in bounds");
    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.write(inner_val).expect("pointer in bounds");
}

Implementations§

Create a new WasmPtr at the given offset.

Get the offset into Wasm linear memory for this WasmPtr.

Casts this WasmPtr to a WasmPtr of a different type.

Returns a null UserPtr.

Checks whether the WasmPtr is null.

Calculates an offset from the current pointer address. The argument is in units of T.

This method returns an error if an address overflow occurs.

Calculates an offset from the current pointer address. The argument is in units of T.

This method returns an error if an address underflow occurs.

Creates a WasmRef from this WasmPtr which allows reading and mutating of the value being pointed to.

Reads the address pointed to by this WasmPtr in a memory.

Writes to the address pointed to by this WasmPtr in a memory.

Creates a WasmSlice starting at this WasmPtr which allows reading and mutating of an array of value being pointed to.

Returns a MemoryAccessError if the slice length overflows a 64-bit address.

Reads a sequence of values from this WasmPtr until a value that matches the given condition is found.

This last value is not included in the returned vector.

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

This method is safe to call even if the memory is being concurrently modified.

Reads a null-terminated UTF-8 string from the WasmPtr.

This method is safe to call even if the memory is being concurrently modified.

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
Returns whether the given value is from the given store. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method is passed a byte slice which contains the byte representation of self. It must zero out any bytes which are uninitialized (e.g. padding bytes).

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
Deserializes using the given deserializer
Compare self to key and return true if they are equal.

Returns the argument unchanged.

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

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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
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.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more