pub struct ResourceAny { /* private fields */ }component-model and runtime only.Expand description
Representation of a dynamically typed guest-defined or host-defined resource in the component model.
§Guest-defined resources
Guest-defined resources enter the host through generated bindings, such as
a function that returns an owned resource. Their methods are called through
the generated type for that resource, for example GuestLogger in the
exported resources example in bindgen_examples. A guest-defined
ResourceAny cannot be converted to Resource or ResourceDynamic,
because those types represent host-defined resources.
§Host-defined resources
Convert a host-defined Resource<T> to this type with
ResourceAny::try_from_resource. Convert it back with
ResourceAny::try_into_resource or
ResourceAny::try_into_resource_dynamic. These conversions check the
resource type at runtime.
§Ownership and destruction
Like Resource this type represents either an own or a borrow
resource internally, and the WIT signature controls which one a value is.
Passing a resource to an own parameter transfers ownership to the callee,
while passing it to a borrow parameter keeps ownership with the caller.
When a function returns an own resource, the caller acquires ownership.
The same applies to each owned resource nested in a record, variant, list,
or other value.
A ResourceAny with a host table entry that the host still owns must
eventually be passed to an own parameter, converted to a typed resource,
or explicitly destroyed with ResourceAny::resource_drop. Destroying it
updates dynamic state tracking and invokes the WebAssembly-defined
destructor for a resource, if any. ResourceAny is Copy, but once
ownership has been transferred the handle must not be used again.
ResourceAny has no static type parameter, so using one with the wrong
generated function produces a runtime type error.
Borrows lifted from a component have host table state and must be dropped.
Synthetic borrows converted from Resource::new_borrow have no host table
state; calling resource_drop on one is harmless but unnecessary.
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 a host-defined Resource into ResourceAny.
resourceis the resource to convert.storeis the store to place the returned resource into.
The returned ResourceAny has no destructor attached to it, so
resource_drop will not invoke a host-defined destructor. This matches
Resource, which has no associated destructor.
§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.
This function will return an OutOfMemory error when
memory allocation fails. See the OutOfMemory type’s documentation for
details on Wasmtime’s out-of-memory handling.
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>>
Attempts to convert this value into a statically typed, host-defined
Resource.
This conversion accepts only host-defined resources of type T.
Guest-defined resources must remain ResourceAny values and be used
through their generated functions and resource projection.
§Errors
This function will return an OutOfMemory error when
memory allocation fails. See the OutOfMemory type’s documentation for
details on Wasmtime’s out-of-memory handling.
Sourcepub fn try_into_resource_dynamic(
self,
store: impl AsContextMut,
) -> Result<ResourceDynamic>
pub fn try_into_resource_dynamic( self, store: impl AsContextMut, ) -> Result<ResourceDynamic>
Attempts to convert this value into a dynamically typed, host-defined
ResourceDynamic.
This conversion accepts only host-defined resources. Guest-defined
resources must remain ResourceAny values and be used through their
generated functions and resource projection.
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 for resources with host table state. For synthetic
borrows converted from Resource::new_borrow it has no effect.
For owned resources this may execute the guest-defined destructor if
applicable (or the host-defined destructor if one was specified).
Exactly one of the following must be called for each ResourceAny,
depending on how the store is being driven:
ResourceAny::resource_dropfor synchronous stores.ResourceAny::resource_drop_asyncfor async stores when aStoreContextMutis available.ResourceAny::resource_drop_concurrentwhen only anAccessoris available, such as insideStore::run_concurrentor anAccessorTask.
§Errors
This function will return an OutOfMemory error when
memory allocation fails. See the OutOfMemory type’s documentation for
details on Wasmtime’s out-of-memory handling.
Sourcepub async fn resource_drop_async(
self,
store: impl AsContextMut<Data: Send>,
) -> Result<()>
Available on crate feature async only.
pub async fn resource_drop_async( self, store: impl AsContextMut<Data: Send>, ) -> Result<()>
async only.Same as ResourceAny::resource_drop except for use with async stores
to execute the destructor asynchronously.
§Errors
This function will return an OutOfMemory error when
memory allocation fails. See the OutOfMemory type’s documentation for
details on Wasmtime’s out-of-memory handling.
Sourcepub async fn resource_drop_concurrent(
self,
accessor: impl AsAccessor,
) -> Result<()>
Available on crate feature component-model-async only.
pub async fn resource_drop_concurrent( self, accessor: impl AsAccessor, ) -> Result<()>
component-model-async only.Same as ResourceAny::resource_drop except for use with an
Accessor while a store is executing
Store::run_concurrent.
The resource drop is queued for execution on the store’s worker fiber. This method must be awaited while the store’s concurrent event loop is running so the queued drop can make progress.
Once this future has been polled and the drop has been queued, dropping the future does not cancel the resource drop.
§Errors
This function will return an OutOfMemory error when
memory allocation fails. See the OutOfMemory type’s documentation for
details on Wasmtime’s out-of-memory handling.
Trait Implementations§
Source§impl Clone for ResourceAny
impl Clone for ResourceAny
impl ComponentType for ResourceAny
impl Copy for ResourceAny
Source§impl Debug for ResourceAny
impl Debug for ResourceAny
impl Eq for ResourceAny
Source§impl From<ResourceAny> for Val
impl From<ResourceAny> for Val
Source§fn from(i: ResourceAny) -> Self
fn from(i: ResourceAny) -> Self
impl Lift for ResourceAny
impl Lower for ResourceAny
Source§impl PartialEq for ResourceAny
impl PartialEq 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 UnsafeUnpin 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,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
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