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
impl ResourceDynamic
Sourcepub fn new_own(rep: u32, ty: u32) -> ResourceDynamic
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.
Sourcepub fn new_borrow(rep: u32, ty: u32) -> ResourceDynamic
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.
Sourcepub fn rep(&self) -> u32
pub fn rep(&self) -> u32
Returns the underlying 32-bit representation used to originally create this resource.
This is the same as Resource::rep.
Sourcepub fn ty(&self) -> u32
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.
Sourcepub fn owned(&self) -> bool
pub fn owned(&self) -> bool
Returns whether this is an owned resource or not.
This is the same as Resource::owned.
Sourcepub fn try_from_resource_any(
resource: ResourceAny,
store: impl AsContextMut,
) -> Result<ResourceDynamic, Error>
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.
Sourcepub fn try_into_resource_any(
self,
store: impl AsContextMut,
) -> Result<ResourceAny, Error>
pub fn try_into_resource_any( self, store: impl AsContextMut, ) -> Result<ResourceAny, Error>
Trait Implementations§
Source§impl Debug for ResourceDynamic
impl Debug for ResourceDynamic
impl ComponentType for ResourceDynamic
impl Lift for ResourceDynamic
impl Lower for ResourceDynamic
Auto Trait Implementations§
impl !Freeze for ResourceDynamic
impl RefUnwindSafe for ResourceDynamic
impl Send for ResourceDynamic
impl Sync for ResourceDynamic
impl Unpin for ResourceDynamic
impl UnwindSafe for ResourceDynamic
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
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