pub struct Instance { /* private fields */ }Expand description
An instantiated component.
This type represents an instantiated Component.
Instances have exports which can be accessed through functions such as
Instance::get_func or Instance::get_export. Instances are owned by a
Store and all methods require a handle to the store.
Component instances are created through
Linker::instantiate and its family of
methods.
This type is similar to the core wasm version
wasmtime::Instance except that it represents an
instantiated component instead of an instantiated module.
Implementations§
Source§impl Instance
impl Instance
Sourcepub async fn run_concurrent<T, R>(
self,
store: impl AsContextMut<Data = T>,
fun: impl AsyncFnOnce(&Accessor<T>) -> R,
) -> Result<R, Error>where
T: Send + 'static,
pub async fn run_concurrent<T, R>(
self,
store: impl AsContextMut<Data = T>,
fun: impl AsyncFnOnce(&Accessor<T>) -> R,
) -> Result<R, Error>where
T: Send + 'static,
Run the specified closure fun to completion as part of this instance’s
event loop.
Like [Self::run], this will run fun as part of this instance’s event
loop until it yields a result or there are no more tasks to run.
Unlike [Self::run], fun is provided an Accessor, which provides
controlled access to the Store and its data.
This function can be used to invoke Func::call_concurrent for
example within the async closure provided here.
§Example
instance.run_concurrent(&mut store, async |accessor| -> wasmtime::Result<_> {
let resource = accessor.with(|mut access| access.get().table.push(MyResource(42)))?;
let (another_resource,) = foo.call_concurrent(accessor, (resource,)).await?.0;
let value = accessor.with(|mut access| access.get().table.delete(another_resource))?;
bar.call_concurrent(accessor, (value.0,)).await?;
Ok(())
}).await??;Sourcepub fn spawn<U>(
self,
store: impl AsContextMut<Data = U>,
task: impl AccessorTask<U, HasSelf<U>, Result<(), Error>>,
) -> JoinHandlewhere
U: 'static,
pub fn spawn<U>(
self,
store: impl AsContextMut<Data = U>,
task: impl AccessorTask<U, HasSelf<U>, Result<(), Error>>,
) -> JoinHandlewhere
U: 'static,
Spawn a background task to run as part of this instance’s event loop.
The task will receive an &Accessor<U> and run concurrently with
any other tasks in progress for the instance.
Note that the task will only make progress if and when the event loop for this instance is run.
The returned [SpawnHandle] may be used to cancel the task.
Source§impl Instance
impl Instance
Sourcepub fn get_func(
&self,
store: impl AsContextMut,
name: impl InstanceExportLookup,
) -> Option<Func>
pub fn get_func( &self, store: impl AsContextMut, name: impl InstanceExportLookup, ) -> Option<Func>
Looks up an exported function by name within this Instance.
The store argument provided must be the store that this instance
lives within and the name argument is the lookup key by which to find
the exported function. If the function is found then Some is returned
and otherwise None is returned.
The name here can be a string such as &str or it can be a
ComponentExportIndex which is loaded prior from a Component.
§Panics
Panics if store does not own this instance.
§Examples
Looking up a function which is exported from the root of a component:
use wasmtime::{Engine, Store};
use wasmtime::component::{Component, Linker};
let engine = Engine::default();
let component = Component::new(
&engine,
r#"
(component
(core module $m
(func (export "f"))
)
(core instance $i (instantiate $m))
(func (export "f")
(canon lift (core func $i "f")))
)
"#,
)?;
// Look up the function by name
let mut store = Store::new(&engine, ());
let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
let func = instance.get_func(&mut store, "f").unwrap();
// The function can also be looked up by an index via a precomputed index.
let export = component.get_export_index(None, "f").unwrap();
let func = instance.get_func(&mut store, &export).unwrap();Looking up a function which is exported from a nested instance:
use wasmtime::{Engine, Store};
use wasmtime::component::{Component, Linker};
let engine = Engine::default();
let component = Component::new(
&engine,
r#"
(component
(core module $m
(func (export "f"))
)
(core instance $i (instantiate $m))
(func $f
(canon lift (core func $i "f")))
(instance $i
(export "f" (func $f)))
(export "i" (instance $i))
)
"#,
)?;
// First look up the exported instance, then use that to lookup the
// exported function.
let instance_index = component.get_export_index(None, "i").unwrap();
let func_index = component.get_export_index(Some(&instance_index), "f").unwrap();
// Then use `func_index` at runtime.
let mut store = Store::new(&engine, ());
let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
let func = instance.get_func(&mut store, &func_index).unwrap();
// Alternatively the `instance` can be used directly in conjunction with
// the `get_export_index` method.
let instance_index = instance.get_export_index(&mut store, None, "i").unwrap();
let func_index = instance.get_export_index(&mut store, Some(&instance_index), "f").unwrap();
let func = instance.get_func(&mut store, &func_index).unwrap();Sourcepub fn get_typed_func<Params, Results>(
&self,
store: impl AsContextMut,
name: impl InstanceExportLookup,
) -> Result<TypedFunc<Params, Results>, Error>
pub fn get_typed_func<Params, Results>( &self, store: impl AsContextMut, name: impl InstanceExportLookup, ) -> Result<TypedFunc<Params, Results>, Error>
Looks up an exported Func value by name and with its type.
This function is a convenience wrapper over Instance::get_func and
Func::typed. For more information see the linked documentation.
Returns an error if name isn’t a function export or if the export’s
type did not match Params or Results
§Panics
Panics if store does not own this instance.
Sourcepub fn get_module(
&self,
store: impl AsContextMut,
name: impl InstanceExportLookup,
) -> Option<Module>
pub fn get_module( &self, store: impl AsContextMut, name: impl InstanceExportLookup, ) -> Option<Module>
Looks up an exported module by name within this Instance.
The store argument provided must be the store that this instance
lives within and the name argument is the lookup key by which to find
the exported module. If the module is found then Some is returned
and otherwise None is returned.
The name here can be a string such as &str or it can be a
ComponentExportIndex which is loaded prior from a Component.
For some examples see Instance::get_func for loading values from a
component.
§Panics
Panics if store does not own this instance.
Sourcepub fn get_resource(
&self,
store: impl AsContextMut,
name: impl InstanceExportLookup,
) -> Option<ResourceType>
pub fn get_resource( &self, store: impl AsContextMut, name: impl InstanceExportLookup, ) -> Option<ResourceType>
Looks up an exported resource type by name within this Instance.
The store argument provided must be the store that this instance
lives within and the name argument is the lookup key by which to find
the exported resource. If the resource is found then Some is returned
and otherwise None is returned.
The name here can be a string such as &str or it can be a
ComponentExportIndex which is loaded prior from a Component.
For some examples see Instance::get_func for loading values from a
component.
§Panics
Panics if store does not own this instance.
Sourcepub fn get_export(
&self,
store: impl AsContextMut,
instance: Option<&ComponentExportIndex>,
name: &str,
) -> Option<(ComponentItem, ComponentExportIndex)>
pub fn get_export( &self, store: impl AsContextMut, instance: Option<&ComponentExportIndex>, name: &str, ) -> Option<(ComponentItem, ComponentExportIndex)>
A methods similar to Component::get_export except for this
instance.
This method will lookup the name provided within the instance
provided and return a ComponentItem describing the export,
and ComponentExportIndex which can be passed other get_*
functions like Instance::get_func.
The ComponentItem is more expensive to compute than the
ComponentExportIndex. If you are not consuming the
ComponentItem, use Instance::get_export_index instead.
§Panics
Panics if store does not own this instance.
Sourcepub fn get_export_index(
&self,
store: impl AsContextMut,
instance: Option<&ComponentExportIndex>,
name: &str,
) -> Option<ComponentExportIndex>
pub fn get_export_index( &self, store: impl AsContextMut, instance: Option<&ComponentExportIndex>, name: &str, ) -> Option<ComponentExportIndex>
A methods similar to Component::get_export_index except for this
instance.
This method will lookup the name provided within the instance
provided and return a ComponentExportIndex which can be passed
other get_* functions like Instance::get_func.
If you need the ComponentItem corresponding to this export, use
the Instance::get_export instead.
§Panics
Panics if store does not own this instance.
Sourcepub fn instance_pre<T>(&self, store: impl AsContext<Data = T>) -> InstancePre<T>
pub fn instance_pre<T>(&self, store: impl AsContext<Data = T>) -> InstancePre<T>
Returns the InstancePre that was used to create this instance.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Instance
impl RefUnwindSafe for Instance
impl Send for Instance
impl Sync for Instance
impl Unpin for Instance
impl UnwindSafe for Instance
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§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> 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