wasmtime::component

Struct Instance

source
pub struct Instance(/* private fields */);
Available on crate features runtime and component-model only.
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

source

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.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.export_index(None, "i").unwrap();
let (_, func_index) = component.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` method.
let instance_index = instance.get_export(&mut store, None, "i").unwrap();
let func_index = instance.get_export(&mut store, Some(&instance_index), "f").unwrap();
let func = instance.get_func(&mut store, &func_index).unwrap();
source

pub fn get_typed_func<Params, Results>( &self, store: impl AsContextMut, name: impl InstanceExportLookup, ) -> Result<TypedFunc<Params, Results>>
where Params: ComponentNamedList + Lower, Results: ComponentNamedList + Lift,

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.

source

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.

source

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.

source

pub fn get_export( &self, store: impl AsContextMut, instance: Option<&ComponentExportIndex>, name: &str, ) -> Option<ComponentExportIndex>

A methods similar to Component::export_index except for this instance.

This method will lookup the name provided within the instance provided and return a ComponentExportIndex which can be used to pass to other get_* functions like Instance::get_func.

§Panics

Panics if store does not own this instance.

Trait Implementations§

source§

impl Clone for Instance

source§

fn clone(&self) -> Instance

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Copy for Instance

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 = 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.