Path: blob/main/crates/component-macro/tests/expanded/direct-import.rs
3085 views
/// Auto-generated bindings for a pre-instantiated version of a1/// component which implements the world `foo`.2///3/// This structure is created through [`FooPre::new`] which4/// takes a [`InstancePre`](wasmtime::component::InstancePre) that5/// has been created through a [`Linker`](wasmtime::component::Linker).6///7/// For more information see [`Foo`] as well.8pub struct FooPre<T: 'static> {9instance_pre: wasmtime::component::InstancePre<T>,10indices: FooIndices,11}12impl<T: 'static> Clone for FooPre<T> {13fn clone(&self) -> Self {14Self {15instance_pre: self.instance_pre.clone(),16indices: self.indices.clone(),17}18}19}20impl<_T: 'static> FooPre<_T> {21/// Creates a new copy of `FooPre` bindings which can then22/// be used to instantiate into a particular store.23///24/// This method may fail if the component behind `instance_pre`25/// does not have the required exports.26pub fn new(27instance_pre: wasmtime::component::InstancePre<_T>,28) -> wasmtime::Result<Self> {29let indices = FooIndices::new(&instance_pre)?;30Ok(Self { instance_pre, indices })31}32pub fn engine(&self) -> &wasmtime::Engine {33self.instance_pre.engine()34}35pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {36&self.instance_pre37}38/// Instantiates a new instance of [`Foo`] within the39/// `store` provided.40///41/// This function will use `self` as the pre-instantiated42/// instance to perform instantiation. Afterwards the preloaded43/// indices in `self` are used to lookup all exports on the44/// resulting instance.45pub fn instantiate(46&self,47mut store: impl wasmtime::AsContextMut<Data = _T>,48) -> wasmtime::Result<Foo> {49let mut store = store.as_context_mut();50let instance = self.instance_pre.instantiate(&mut store)?;51self.indices.load(&mut store, &instance)52}53}54impl<_T: Send + 'static> FooPre<_T> {55/// Same as [`Self::instantiate`], except with `async`.56pub async fn instantiate_async(57&self,58mut store: impl wasmtime::AsContextMut<Data = _T>,59) -> wasmtime::Result<Foo> {60let mut store = store.as_context_mut();61let instance = self.instance_pre.instantiate_async(&mut store).await?;62self.indices.load(&mut store, &instance)63}64}65/// Auto-generated bindings for index of the exports of66/// `foo`.67///68/// This is an implementation detail of [`FooPre`] and can69/// be constructed if needed as well.70///71/// For more information see [`Foo`] as well.72#[derive(Clone)]73pub struct FooIndices {}74/// Auto-generated bindings for an instance a component which75/// implements the world `foo`.76///77/// This structure can be created through a number of means78/// depending on your requirements and what you have on hand:79///80/// * The most convenient way is to use81/// [`Foo::instantiate`] which only needs a82/// [`Store`], [`Component`], and [`Linker`].83///84/// * Alternatively you can create a [`FooPre`] ahead of85/// time with a [`Component`] to front-load string lookups86/// of exports once instead of per-instantiation. This87/// method then uses [`FooPre::instantiate`] to88/// create a [`Foo`].89///90/// * If you've instantiated the instance yourself already91/// then you can use [`Foo::new`].92///93/// These methods are all equivalent to one another and move94/// around the tradeoff of what work is performed when.95///96/// [`Store`]: wasmtime::Store97/// [`Component`]: wasmtime::component::Component98/// [`Linker`]: wasmtime::component::Linker99pub struct Foo {}100pub trait FooImportsWithStore: wasmtime::component::HasData {}101impl<_T: ?Sized> FooImportsWithStore for _T102where103_T: wasmtime::component::HasData,104{}105pub trait FooImports {106fn foo(&mut self) -> ();107}108impl<_T: FooImports + ?Sized> FooImports for &mut _T {109fn foo(&mut self) -> () {110FooImports::foo(*self)111}112}113const _: () = {114impl FooIndices {115/// Creates a new copy of `FooIndices` bindings which can then116/// be used to instantiate into a particular store.117///118/// This method may fail if the component does not have the119/// required exports.120pub fn new<_T>(121_instance_pre: &wasmtime::component::InstancePre<_T>,122) -> wasmtime::Result<Self> {123let _component = _instance_pre.component();124let _instance_type = _instance_pre.instance_type();125Ok(FooIndices {})126}127/// Uses the indices stored in `self` to load an instance128/// of [`Foo`] from the instance provided.129///130/// Note that at this time this method will additionally131/// perform type-checks of all exports.132pub fn load(133&self,134mut store: impl wasmtime::AsContextMut,135instance: &wasmtime::component::Instance,136) -> wasmtime::Result<Foo> {137let _ = &mut store;138let _instance = instance;139Ok(Foo {})140}141}142impl Foo {143/// Convenience wrapper around [`FooPre::new`] and144/// [`FooPre::instantiate`].145pub fn instantiate<_T>(146store: impl wasmtime::AsContextMut<Data = _T>,147component: &wasmtime::component::Component,148linker: &wasmtime::component::Linker<_T>,149) -> wasmtime::Result<Foo> {150let pre = linker.instantiate_pre(component)?;151FooPre::new(pre)?.instantiate(store)152}153/// Convenience wrapper around [`FooIndices::new`] and154/// [`FooIndices::load`].155pub fn new(156mut store: impl wasmtime::AsContextMut,157instance: &wasmtime::component::Instance,158) -> wasmtime::Result<Foo> {159let indices = FooIndices::new(&instance.instance_pre(&store))?;160indices.load(&mut store, instance)161}162/// Convenience wrapper around [`FooPre::new`] and163/// [`FooPre::instantiate_async`].164pub async fn instantiate_async<_T>(165store: impl wasmtime::AsContextMut<Data = _T>,166component: &wasmtime::component::Component,167linker: &wasmtime::component::Linker<_T>,168) -> wasmtime::Result<Foo>169where170_T: Send,171{172let pre = linker.instantiate_pre(component)?;173FooPre::new(pre)?.instantiate_async(store).await174}175pub fn add_to_linker_imports<T, D>(176linker: &mut wasmtime::component::Linker<T>,177host_getter: fn(&mut T) -> D::Data<'_>,178) -> wasmtime::Result<()>179where180D: FooImportsWithStore,181for<'a> D::Data<'a>: FooImports,182T: 'static,183{184let mut linker = linker.root();185linker186.func_wrap(187"foo",188move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {189let host = &mut host_getter(caller.data_mut());190let r = FooImports::foo(host);191Ok(r)192},193)?;194Ok(())195}196pub fn add_to_linker<T, D>(197linker: &mut wasmtime::component::Linker<T>,198host_getter: fn(&mut T) -> D::Data<'_>,199) -> wasmtime::Result<()>200where201D: FooImportsWithStore,202for<'a> D::Data<'a>: FooImports,203T: 'static,204{205Self::add_to_linker_imports::<T, D>(linker, host_getter)?;206Ok(())207}208}209};210211212