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
impl ResourceAny
sourcepub fn try_from_resource<T: 'static>(
resource: Resource<T>,
store: impl AsContextMut,
) -> Result<Self>
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.
sourcepub fn try_into_resource<T: 'static>(
self,
store: impl AsContextMut,
) -> Result<Resource<T>>
pub fn try_into_resource<T: 'static>( self, store: impl AsContextMut, ) -> Result<Resource<T>>
sourcepub fn ty(&self) -> ResourceType
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.
sourcepub fn owned(&self) -> bool
pub fn owned(&self) -> bool
Returns whether this is an owned resource, and if not it’s a borrowed resource.
sourcepub fn resource_drop(self, store: impl AsContextMut) -> Result<()>
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).
sourcepub async fn resource_drop_async<T>(
self,
store: impl AsContextMut<Data = T>,
) -> Result<()>where
T: Send,
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
impl Clone for ResourceAny
source§fn clone(&self) -> ResourceAny
fn clone(&self) -> ResourceAny
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for ResourceAny
impl Debug for ResourceAny
source§impl PartialEq for ResourceAny
impl PartialEq for ResourceAny
impl ComponentType for ResourceAny
impl Copy for ResourceAny
impl Eq for ResourceAny
impl Lift for ResourceAny
impl Lower for ResourceAny
impl StructuralPartialEq for ResourceAny
Auto Trait Implementations§
impl Freeze for ResourceAny
impl RefUnwindSafe for ResourceAny
impl Send for ResourceAny
impl Sync for ResourceAny
impl Unpin for ResourceAny
impl UnwindSafe for ResourceAny
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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