Skip to main content

ResourceAny

Struct ResourceAny 

Source
pub struct ResourceAny { /* private fields */ }
Available on crate features 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

Source

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

Attempts to convert a host-defined Resource into ResourceAny.

  • resource is the resource to convert.
  • store is 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.

Source

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.

Source

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.

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 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_drop for synchronous stores.
  • ResourceAny::resource_drop_async for async stores when a StoreContextMut is available.
  • ResourceAny::resource_drop_concurrent when only an Accessor is available, such as inside Store::run_concurrent or an AccessorTask.
§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.

Source

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

Available on crate feature 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.

Source

pub async fn resource_drop_concurrent( self, accessor: impl AsAccessor, ) -> Result<()>

Available on crate feature 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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl ComponentType for ResourceAny

Source§

impl Copy for ResourceAny

Source§

impl Debug for ResourceAny

Source§

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

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

impl Eq for ResourceAny

Source§

impl From<ResourceAny> for Val

Source§

fn from(i: ResourceAny) -> Self

Converts to this type from the input type.
Source§

impl Lift for ResourceAny

Source§

impl Lower for ResourceAny

Source§

impl PartialEq for ResourceAny

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
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, dest: *mut u8)

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

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

§

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

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

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

§

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

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

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

§

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
§

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.