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
sourceimpl<T: Copy + ValueType> WasmPtr<T, Item>
impl<T: Copy + ValueType> WasmPtr<T, Item>
Methods for WasmPtr
s 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<WasmCell<'a, T>>
pub fn deref<'a>(self, memory: &'a Memory) -> Option<WasmCell<'a, 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.
sourceimpl<T: Copy + ValueType> WasmPtr<T, Array>
impl<T: Copy + ValueType> WasmPtr<T, Array>
Methods for WasmPtr
s 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<'a>(
self,
memory: &'a Memory,
index: u32,
length: u32
) -> Option<Vec<WasmCell<'a, T>>>
pub fn deref<'a>(
self,
memory: &'a Memory,
index: u32,
length: u32
) -> Option<Vec<WasmCell<'a, 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 get_utf8_str<'a>(
self,
memory: &'a Memory,
str_len: u32
) -> Option<&'a str>
pub unsafe fn get_utf8_str<'a>(
self,
memory: &'a Memory,
str_len: u32
) -> Option<&'a str>
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.
sourcepub fn get_utf8_string(self, memory: &Memory, str_len: u32) -> Option<String>
pub fn get_utf8_string(self, memory: &Memory, str_len: u32) -> Option<String>
Get a UTF-8 String
from the WasmPtr
with the given length.
an aliasing WasmPtr
is used to mutate memory.
sourcepub unsafe fn get_utf8_str_with_nul<'a>(
self,
memory: &'a Memory
) -> Option<&'a str>
pub unsafe fn get_utf8_str_with_nul<'a>(
self,
memory: &'a Memory
) -> Option<&'a 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,
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.
sourcepub fn get_utf8_string_with_nul(self, memory: &Memory) -> Option<String>
pub fn get_utf8_string_with_nul(self, memory: &Memory) -> Option<String>
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
sourceimpl<T: Copy, Ty> FromToNativeWasmType for WasmPtr<T, Ty>
impl<T: Copy, Ty> FromToNativeWasmType for WasmPtr<T, Ty>
impl<T: Copy, Ty> Copy for WasmPtr<T, Ty>
impl<T: Copy, Ty> Eq for WasmPtr<T, Ty>
impl<T: Copy, Ty> ValueType for WasmPtr<T, Ty>
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
sourceimpl<T> ArchivePointee for T
impl<T> ArchivePointee for T
type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
sourcepub fn pointer_metadata(
&<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata
pub fn pointer_metadata(
&<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata
Converts some archived metadata to the pointer metadata for itself.
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<F, W, T, D> Deserialize<With<T, W>, D> for F where
W: DeserializeWith<F, T, D>,
D: Fallible + ?Sized,
F: ?Sized,
impl<F, W, T, D> Deserialize<With<T, W>, D> for F where
W: DeserializeWith<F, T, D>,
D: Fallible + ?Sized,
F: ?Sized,
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcepub fn equivalent(&self, key: &K) -> bool
pub fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more