ResourceDynamic

Struct ResourceDynamic 

Source
pub struct ResourceDynamic(/* private fields */);
Expand description

A host-defined resource in the component model with a dynamic runtime value representing its type.

This type represents a host-owned resource in the same manner as Resource, and almost all of the documentation on that type is applicable to usage of this type as well. Where the two differ is how embedders use these types in a particular embedding.

§Use cases for Resource

The Resource type is intended to be used by Rust embedders and provides a T type parameter to provide a layer of type safety when using the resource. The type parameter prevents mixing up resources of the same type by accident. The bindgen! macro, for example, uses Resource by default to represent all resources imported by a wasm guest.

As the documentation on Resource indicates the T type parameter on Resource<T> is a hint and T isn’t stored inside. This means that the host can write a function which takes Resource<T>, for example, and prevent mistakes of passing a wrong-typed-resource to that function. Typically the 32-bit value that Resource<T> wraps is an index into some sort of table the host manages and the index points to a value of type T. The T type parameter can assist in writing helper functions to access these types.

The downside of Resource, however, is that all resource types must be statically assigned at compile time. It’s not possible to manufacture more types at runtime in some more dynamic situations. That’s where ResourceDynamic comes in.

§Use cases for ResourceDynamic

The general idea of ResourceDynamic is very similar to Resource – it represents a “trusted” 32-bit value that the host defines and assigns meaning to. There is no destructor on ResourceDynamic and the host has to know how to destroy the associated state, if any, that ResourceDynamic references. The difference with Resource is that is has runtime type information instead of static type information, meaning that it’s possible to mix these up at compile by by accident.

However a ResourceDynamic can be constructed dynamically at runtime with a runtime-defined type. For example an embedding that provides generic access to types in the host may want to take advantage of the dynamic nature of this type. Resources of type ResourceDynamic have a type of [ResourceType::host_dynamic(ty)] where ty is the value provided to the constructors of ResourceDynamic.

A ResourceDynamic implements Lift and Lower in the same manner as Resource, but the implementations may fail after type-checking unlike with Resource (due to the dynamic nature of the type which can’t be fully-checked during type-checking).

Implementations§

Source§

impl ResourceDynamic

Source

pub fn new_own(rep: u32, ty: u32) -> ResourceDynamic

Creates a new owned resource with the rep specified.

This is the same as Resource::new_own except that ty is an extra parameter for the host-defined type information.

Source

pub fn new_borrow(rep: u32, ty: u32) -> ResourceDynamic

Creates a new borrowed resource which isn’t actually rooted in any ownership.

This is the same as Resource::new_borrow except that ty is an extra parameter for the host-defined type information.

Source

pub fn rep(&self) -> u32

Returns the underlying 32-bit representation used to originally create this resource.

This is the same as Resource::rep.

Source

pub fn ty(&self) -> u32

Returns the 32-bit integer indicating the type of this resource.

This will return the same 32-bit integer provided to the ResourceDynamic::new_own constructor. The meaning of this integer is left to the host and this only serves as an accessor to provide the value back to the host.

Source

pub fn owned(&self) -> bool

Returns whether this is an owned resource or not.

This is the same as Resource::owned.

Source

pub fn try_from_resource_any( resource: ResourceAny, store: impl AsContextMut, ) -> Result<ResourceDynamic, Error>

Attempts to convert a ResourceAny into ResourceDynamic.

This is the same as Resource::try_from_resource_any.

Source

pub fn try_into_resource_any( self, store: impl AsContextMut, ) -> Result<ResourceAny, Error>

Trait Implementations§

Source§

impl Debug for ResourceDynamic

Source§

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

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

impl ComponentType for ResourceDynamic

Source§

impl Lift for ResourceDynamic

Source§

impl Lower for ResourceDynamic

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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