Path: blob/main/crates/component-macro/tests/expanded/direct-import_concurrent.rs
3073 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 + Send {101fn foo<T: Send>(102accessor: &wasmtime::component::Accessor<T, Self>,103) -> impl ::core::future::Future<Output = ()> + Send;104}105pub trait FooImports: Send {}106impl<_T: FooImports + ?Sized + Send> FooImports for &mut _T {}107const _: () = {108impl FooIndices {109/// Creates a new copy of `FooIndices` bindings which can then110/// be used to instantiate into a particular store.111///112/// This method may fail if the component does not have the113/// required exports.114pub fn new<_T>(115_instance_pre: &wasmtime::component::InstancePre<_T>,116) -> wasmtime::Result<Self> {117let _component = _instance_pre.component();118let _instance_type = _instance_pre.instance_type();119Ok(FooIndices {})120}121/// Uses the indices stored in `self` to load an instance122/// of [`Foo`] from the instance provided.123///124/// Note that at this time this method will additionally125/// perform type-checks of all exports.126pub fn load(127&self,128mut store: impl wasmtime::AsContextMut,129instance: &wasmtime::component::Instance,130) -> wasmtime::Result<Foo> {131let _ = &mut store;132let _instance = instance;133Ok(Foo {})134}135}136impl Foo {137/// Convenience wrapper around [`FooPre::new`] and138/// [`FooPre::instantiate`].139pub fn instantiate<_T>(140store: impl wasmtime::AsContextMut<Data = _T>,141component: &wasmtime::component::Component,142linker: &wasmtime::component::Linker<_T>,143) -> wasmtime::Result<Foo> {144let pre = linker.instantiate_pre(component)?;145FooPre::new(pre)?.instantiate(store)146}147/// Convenience wrapper around [`FooIndices::new`] and148/// [`FooIndices::load`].149pub fn new(150mut store: impl wasmtime::AsContextMut,151instance: &wasmtime::component::Instance,152) -> wasmtime::Result<Foo> {153let indices = FooIndices::new(&instance.instance_pre(&store))?;154indices.load(&mut store, instance)155}156/// Convenience wrapper around [`FooPre::new`] and157/// [`FooPre::instantiate_async`].158pub async fn instantiate_async<_T>(159store: impl wasmtime::AsContextMut<Data = _T>,160component: &wasmtime::component::Component,161linker: &wasmtime::component::Linker<_T>,162) -> wasmtime::Result<Foo>163where164_T: Send,165{166let pre = linker.instantiate_pre(component)?;167FooPre::new(pre)?.instantiate_async(store).await168}169pub fn add_to_linker_imports<T, D>(170linker: &mut wasmtime::component::Linker<T>,171host_getter: fn(&mut T) -> D::Data<'_>,172) -> wasmtime::Result<()>173where174D: FooImportsWithStore,175for<'a> D::Data<'a>: FooImports,176T: 'static + Send,177{178let mut linker = linker.root();179linker180.func_wrap_concurrent(181"foo",182move |caller: &wasmtime::component::Accessor<T>, (): ()| {183wasmtime::component::__internal::Box::pin(async move {184let host = &caller.with_getter(host_getter);185let r = <D as FooImportsWithStore>::foo(host).await;186Ok(r)187})188},189)?;190Ok(())191}192pub fn add_to_linker<T, D>(193linker: &mut wasmtime::component::Linker<T>,194host_getter: fn(&mut T) -> D::Data<'_>,195) -> wasmtime::Result<()>196where197D: FooImportsWithStore + Send,198for<'a> D::Data<'a>: FooImports + Send,199T: 'static + Send,200{201Self::add_to_linker_imports::<T, D>(linker, host_getter)?;202Ok(())203}204}205};206207208