Struct wasmtime::component::ResourceAny

source ·
pub struct ResourceAny { /* private fields */ }
Expand description

Representation of a resource in the component model, either a guest-defined or a host-defined resource.

This type is similar to Resource except that it can be used to represent any resource, either host or guest. This type cannot be directly constructed and is only available if the guest returns it to the host (e.g. a function returning a guest-defined resource) or by a conversion from Resource via ResourceAny::try_from_resource. This type also does not carry a static type parameter T for example and does not have as much information about its type. This means that it’s possible to get runtime type-errors when using this type because it cannot statically prevent mismatching resource types.

Like Resource this type represents either an own or a borrow resource internally. Unlike Resource, however, a ResourceAny must always be explicitly destroyed with the ResourceAny::resource_drop method. This will update internal dynamic state tracking and invoke the WebAssembly-defined destructor for a resource, if any.

Note that it is required to call resource_drop for all instances of ResourceAny: even borrows. Both borrows and own handles have state associated with them that must be discarded by the time they’re done being used.

Implementations§

source§

impl ResourceAny

source

pub fn try_from_resource<T: 'static>( resource: Resource<T>, store: impl AsContextMut, ) -> Result<Self>

Attempts to convert an imported Resource into ResourceAny.

  • resource is the resource to convert.
  • store is the store to place the returned resource into.

The returned ResourceAny will not have a destructor attached to it meaning that if resource_drop is called then it will not invoked a host-defined destructor. This is similar to how Resource<T> does not have a destructor associated with it.

§Errors

This method will return an error if resource has already been “taken” and has ownership transferred elsewhere which can happen in situations such as when it’s already lowered into a component.

source

pub fn try_into_resource<T: 'static>( self, store: impl AsContextMut, ) -> Result<Resource<T>>

source

pub fn ty(&self) -> ResourceType

Returns the corresponding type associated with this resource, either a host-defined type or a guest-defined type.

This can be compared against ResourceType::host for example to see if it’s a host-resource or against a type extracted with Instance::get_resource to see if it’s a guest-defined resource.

source

pub fn owned(&self) -> bool

Returns whether this is an owned resource, and if not it’s a borrowed resource.

source

pub fn resource_drop(self, store: impl AsContextMut) -> Result<()>

Destroy this resource and release any state associated with it.

This is required to be called (or the async version) for all instances of ResourceAny to ensure that state associated with this resource is properly cleaned up. For owned resources this may execute the guest-defined destructor if applicable (or the host-defined destructor if one was specified).

source

pub async fn resource_drop_async<T>( self, store: impl AsContextMut<Data = T>, ) -> Result<()>
where T: Send,

Same as ResourceAny::resource_drop except for use with async stores to execute the destructor asynchronously.

Trait Implementations§

source§

impl Clone for ResourceAny

source§

fn clone(&self) -> ResourceAny

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for ResourceAny

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for ResourceAny

source§

fn eq(&self, other: &ResourceAny) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl ComponentType for ResourceAny

source§

impl Copy for ResourceAny

source§

impl Eq for ResourceAny

source§

impl Lift for ResourceAny

source§

impl Lower for ResourceAny

source§

impl StructuralPartialEq for ResourceAny

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
source§

type Init = T

The type for initializers.
source§

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

Initializes a with the given initializer. Read more
source§

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

Dereferences the given pointer. Read more
source§

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

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.