pub struct WasmPtr<T, Ty = Item>where
T: Copy,{ /* 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(ctx: &mut Ctx, ptr: WasmPtr<u32>) {
let memory = ctx.memory(0);
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);
}Implementations§
Source§impl<T> WasmPtr<T>
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.
impl<T> WasmPtr<T>
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.
Sourcepub fn deref<'a>(self, memory: &'a Memory) -> Option<&'a Cell<T>>
pub fn deref<'a>(self, memory: &'a Memory) -> Option<&'a Cell<T>>
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.
Sourcepub unsafe fn deref_mut<'a>(self, memory: &'a Memory) -> Option<&'a mut Cell<T>>
pub unsafe fn deref_mut<'a>(self, memory: &'a Memory) -> Option<&'a mut Cell<T>>
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 Tthat point to the same memory. You should ensure that you have exclusive access to Wasm linear memory before calling this method.
Source§impl<T> WasmPtr<T, Array>
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.
impl<T> WasmPtr<T, Array>
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.
Sourcepub fn deref(
self,
memory: &Memory,
index: u32,
length: u32,
) -> Option<&[Cell<T>]>
pub fn deref( self, memory: &Memory, index: u32, length: u32, ) -> Option<&[Cell<T>]>
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.
Sourcepub unsafe fn deref_mut(
self,
memory: &Memory,
index: u32,
length: u32,
) -> Option<&mut [Cell<T>]>
pub unsafe fn deref_mut( self, memory: &Memory, index: u32, length: u32, ) -> Option<&mut [Cell<T>]>
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 Tthat point to the same memory. You should ensure that you have exclusive access to Wasm linear memory before calling this method.
Sourcepub fn get_utf8_string(self, memory: &Memory, str_len: u32) -> Option<&str>
pub fn get_utf8_string(self, memory: &Memory, str_len: u32) -> Option<&str>
Get a UTF-8 string from the WasmPtr with the given length.
Note that 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.
Sourcepub fn get_utf8_string_with_nul(self, memory: &Memory) -> Option<&str>
pub fn get_utf8_string_with_nul(self, memory: &Memory) -> Option<&str>
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,
[get_utf8_string] has to be used for those.
Also note that 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.
Trait Implementations§
Source§impl<T, Ty> WasmExternType for WasmPtr<T, Ty>where
T: Copy,
impl<T, Ty> WasmExternType for WasmPtr<T, Ty>where
T: Copy,
impl<T, Ty> Copy for WasmPtr<T, Ty>where
T: Copy,
impl<T, Ty> Eq for WasmPtr<T, Ty>where
T: Copy,
impl<T, Ty> ValueType for WasmPtr<T, Ty>where
T: Copy,
Auto Trait Implementations§
impl<T, Ty> Freeze for WasmPtr<T, Ty>
impl<T, Ty> RefUnwindSafe for WasmPtr<T, Ty>where
T: RefUnwindSafe,
Ty: RefUnwindSafe,
impl<T, Ty> Send for WasmPtr<T, Ty>
impl<T, Ty> Sync for WasmPtr<T, Ty>
impl<T, Ty> Unpin for WasmPtr<T, Ty>
impl<T, Ty> UnwindSafe for WasmPtr<T, Ty>where
T: UnwindSafe,
Ty: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Rets> TrapEarly<Rets> for Retswhere
Rets: WasmTypeList,
impl<Rets> TrapEarly<Rets> for Retswhere
Rets: WasmTypeList,
Source§type Error = Infallible
type Error = Infallible
Source§fn report(self) -> Result<Rets, Infallible>
fn report(self) -> Result<Rets, Infallible>
Source§impl<A> WasmTypeList for Awhere
A: WasmExternType,
impl<A> WasmTypeList for Awhere
A: WasmExternType,
Source§fn from_ret_array(array: <A as WasmTypeList>::RetArray) -> A
fn from_ret_array(array: <A as WasmTypeList>::RetArray) -> A
Self based on an array of returned values.Source§fn empty_ret_array() -> <A as WasmTypeList>::RetArray
fn empty_ret_array() -> <A as WasmTypeList>::RetArray
Source§fn from_c_struct(c_struct: <A as WasmTypeList>::CStruct) -> A
fn from_c_struct(c_struct: <A as WasmTypeList>::CStruct) -> A
Source§fn into_c_struct(self) -> <A as WasmTypeList>::CStruct
fn into_c_struct(self) -> <A as WasmTypeList>::CStruct
Source§unsafe fn call<Rets>(
self,
f: NonNull<Func>,
wasm: Wasm,
ctx: *mut Ctx,
) -> Result<Rets, RuntimeError>where
Rets: WasmTypeList,
unsafe fn call<Rets>(
self,
f: NonNull<Func>,
wasm: Wasm,
ctx: *mut Ctx,
) -> Result<Rets, RuntimeError>where
Rets: WasmTypeList,
(1, 2).call(func, …). This form is unlikely to be used
directly in the code, see the Func::call implementation.