wiggle

Enum GuestMemory

source
pub enum GuestMemory<'a> {
    Unshared(&'a mut [u8]),
    Shared(&'a [UnsafeCell<u8>]),
}
Expand description

Representation of guest memory for wiggle-generated trait methods.

Guest memory is represented as an array of bytes. Memories are either “unshared” or “shared”. Unshared means that the host has exclusive access to the entire array of memory. This allows safe borrows into wasm linear memory. Shared memories can be modified at any time and are represented as an array of UnsafeCell<u8>.

This is generated by the wiggle bindings macros.

Variants§

§

Unshared(&'a mut [u8])

§

Shared(&'a [UnsafeCell<u8>])

Implementations§

source§

impl<'a> GuestMemory<'a>

source

pub fn read<T>(&self, ptr: GuestPtr<T>) -> Result<T, GuestError>
where T: GuestType,

Read a value from the provided pointer.

This method will delegate to T’s implementation of read which will read a value from the ptr provided.

§Errors

An error is returned if ptr is out of bounds, misaligned, or otherwise not valid to read from.

source

pub fn write<T>(&mut self, ptr: GuestPtr<T>, val: T) -> Result<(), GuestError>
where T: GuestType,

Writes the val provided to the ptr provided.

This commit will write a val into a guest’s linear memory. This will delegate to T’s implementation of write.

§Errors

An error is returned if ptr is out of bounds, misaligned, or otherwise not valid to read from.

source

pub fn as_cow(&self, ptr: GuestPtr<[u8]>) -> Result<Cow<'_, [u8]>, GuestError>

Acquires a slice or owned copy of the memory pointed to by ptr.

This method will attempt to borrow ptr directly from linear memory. If memory is shared and cannot be borrowed directly then an owned copy is returned instead.

§Errors

An error is returned if ptr is out of bounds, misaligned, or otherwise not valid to read from.

source

pub fn as_cow_str(&self, ptr: GuestPtr<str>) -> Result<Cow<'_, str>, GuestError>

Same as GuestMemory::as_cow but for strings.

§Errors

An error is returned if ptr is out of bounds, misaligned, or otherwise not valid to read from.

source

pub fn as_slice(&self, ptr: GuestPtr<[u8]>) -> Result<Option<&[u8]>, GuestError>

Attempts to borrow a raw guest slice of memory pointed to by ptr.

This method will attempt to return a raw pointer into guest memory. This can only be done for Unshared memories. A Shared memory will return Ok(None) here.

§Errors

An error is returned if ptr is out of bounds, misaligned, or otherwise not valid to read from.

source

pub fn as_str(&self, ptr: GuestPtr<str>) -> Result<Option<&str>, GuestError>

Same as GuestMemory::as_slice but for strings.

source

pub fn as_slice_mut( &mut self, ptr: GuestPtr<[u8]>, ) -> Result<Option<&mut [u8]>, GuestError>

Attempts return ptr as a raw slice of mutable bytes in wasm linear memory.

Like GuestMemory::as_slice this only works for Unshared memories and will not work for Shared memories.

source

pub fn to_vec<T>(&self, ptr: GuestPtr<[T]>) -> Result<Vec<T>, GuestError>

Copies the data in the guest region into a Vec.

This is useful when one cannot use GuestMemory::as_slice, e.g., when pointing to a region of WebAssembly shared memory.

source

pub fn copy_from_slice<T>( &mut self, slice: &[T], ptr: GuestPtr<[T]>, ) -> Result<(), GuestError>

Copies the data pointed to by slice into this guest region.

This method is a safe method to copy data from the host to the guest. This requires that self and slice have the same length. The pointee type T requires the GuestTypeTransparent trait which is an assertion that the representation on the host and on the guest is the same.

§Errors

Returns an error if this guest pointer is out of bounds or if the length of this guest pointer is not equal to the length of the slice provided.

source

pub fn is_shared_memory(&self) -> bool

Returns whether this is a shared memory or not.

Trait Implementations§

Auto Trait Implementations§

§

impl<'a> Freeze for GuestMemory<'a>

§

impl<'a> !RefUnwindSafe for GuestMemory<'a>

§

impl<'a> Unpin for GuestMemory<'a>

§

impl<'a> !UnwindSafe for GuestMemory<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Pointee for T

source§

type Pointer = u32

source§

fn debug( pointer: <T as Pointee>::Pointer, f: &mut Formatter<'_>, ) -> Result<(), Error>

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more