Skip to content

Commit

Permalink
Merge #2257
Browse files Browse the repository at this point in the history
2257: Added a comment to WasmPtr about structs. r=syrusakbary a=Earthmark

# Description
This adds a comment about how to use compound c-like structs using WasmPtr.

# Review
This is possibly too small for a changelog entry.

Example tests ensuring this behavior should possibly be added.

I'm also curious as to if this is correct, cause it seems to be working in a side project but I'm unsure as to if it's a good idea!

Co-authored-by: Daniel Miller (Earthmark) <[email protected]>
Co-authored-by: Daniel Miller <[email protected]>
  • Loading branch information
bors[bot] and Earthmark authored May 8, 2021
2 parents f064bea + 25f013b commit f2e9a57
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/api/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ pub struct Item;
/// derefed_ptr.set(inner_val + 1);
/// }
/// ```
///
/// This type can also be used with primitive-filled structs, but be careful of
/// guarantees required by `ValueType`.
/// ```
/// # use wasmer::Memory;
/// # use wasmer::WasmPtr;
/// # use wasmer::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);
/// }
/// ```
#[repr(transparent)]
pub struct WasmPtr<T: Copy, Ty = Item> {
offset: u32,
Expand Down

0 comments on commit f2e9a57

Please sign in to comment.