Path: blob/main/crates/component-macro/tests/expanded/many-arguments_async.rs
1692 views
/// Auto-generated bindings for a pre-instantiated version of a1/// component which implements the world `the-world`.2///3/// This structure is created through [`TheWorldPre::new`] which4/// takes a [`InstancePre`](wasmtime::component::InstancePre) that5/// has been created through a [`Linker`](wasmtime::component::Linker).6///7/// For more information see [`TheWorld`] as well.8pub struct TheWorldPre<T: 'static> {9instance_pre: wasmtime::component::InstancePre<T>,10indices: TheWorldIndices,11}12impl<T: 'static> Clone for TheWorldPre<T> {13fn clone(&self) -> Self {14Self {15instance_pre: self.instance_pre.clone(),16indices: self.indices.clone(),17}18}19}20impl<_T: 'static> TheWorldPre<_T> {21/// Creates a new copy of `TheWorldPre` 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 = TheWorldIndices::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 [`TheWorld`] 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<TheWorld> {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> TheWorldPre<_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<TheWorld> {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/// `the-world`.67///68/// This is an implementation detail of [`TheWorldPre`] and can69/// be constructed if needed as well.70///71/// For more information see [`TheWorld`] as well.72#[derive(Clone)]73pub struct TheWorldIndices {74interface0: exports::foo::foo::manyarg::GuestIndices,75}76/// Auto-generated bindings for an instance a component which77/// implements the world `the-world`.78///79/// This structure can be created through a number of means80/// depending on your requirements and what you have on hand:81///82/// * The most convenient way is to use83/// [`TheWorld::instantiate`] which only needs a84/// [`Store`], [`Component`], and [`Linker`].85///86/// * Alternatively you can create a [`TheWorldPre`] ahead of87/// time with a [`Component`] to front-load string lookups88/// of exports once instead of per-instantiation. This89/// method then uses [`TheWorldPre::instantiate`] to90/// create a [`TheWorld`].91///92/// * If you've instantiated the instance yourself already93/// then you can use [`TheWorld::new`].94///95/// These methods are all equivalent to one another and move96/// around the tradeoff of what work is performed when.97///98/// [`Store`]: wasmtime::Store99/// [`Component`]: wasmtime::component::Component100/// [`Linker`]: wasmtime::component::Linker101pub struct TheWorld {102interface0: exports::foo::foo::manyarg::Guest,103}104const _: () = {105#[allow(unused_imports)]106use wasmtime::component::__internal::anyhow;107impl TheWorldIndices {108/// Creates a new copy of `TheWorldIndices` bindings which can then109/// be used to instantiate into a particular store.110///111/// This method may fail if the component does not have the112/// required exports.113pub fn new<_T>(114_instance_pre: &wasmtime::component::InstancePre<_T>,115) -> wasmtime::Result<Self> {116let _component = _instance_pre.component();117let _instance_type = _instance_pre.instance_type();118let interface0 = exports::foo::foo::manyarg::GuestIndices::new(119_instance_pre,120)?;121Ok(TheWorldIndices { interface0 })122}123/// Uses the indices stored in `self` to load an instance124/// of [`TheWorld`] from the instance provided.125///126/// Note that at this time this method will additionally127/// perform type-checks of all exports.128pub fn load(129&self,130mut store: impl wasmtime::AsContextMut,131instance: &wasmtime::component::Instance,132) -> wasmtime::Result<TheWorld> {133let _ = &mut store;134let _instance = instance;135let interface0 = self.interface0.load(&mut store, &_instance)?;136Ok(TheWorld { interface0 })137}138}139impl TheWorld {140/// Convenience wrapper around [`TheWorldPre::new`] and141/// [`TheWorldPre::instantiate`].142pub fn instantiate<_T>(143store: impl wasmtime::AsContextMut<Data = _T>,144component: &wasmtime::component::Component,145linker: &wasmtime::component::Linker<_T>,146) -> wasmtime::Result<TheWorld> {147let pre = linker.instantiate_pre(component)?;148TheWorldPre::new(pre)?.instantiate(store)149}150/// Convenience wrapper around [`TheWorldIndices::new`] and151/// [`TheWorldIndices::load`].152pub fn new(153mut store: impl wasmtime::AsContextMut,154instance: &wasmtime::component::Instance,155) -> wasmtime::Result<TheWorld> {156let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;157indices.load(&mut store, instance)158}159/// Convenience wrapper around [`TheWorldPre::new`] and160/// [`TheWorldPre::instantiate_async`].161pub async fn instantiate_async<_T>(162store: impl wasmtime::AsContextMut<Data = _T>,163component: &wasmtime::component::Component,164linker: &wasmtime::component::Linker<_T>,165) -> wasmtime::Result<TheWorld>166where167_T: Send,168{169let pre = linker.instantiate_pre(component)?;170TheWorldPre::new(pre)?.instantiate_async(store).await171}172pub fn add_to_linker<T, D>(173linker: &mut wasmtime::component::Linker<T>,174host_getter: fn(&mut T) -> D::Data<'_>,175) -> wasmtime::Result<()>176where177D: foo::foo::manyarg::HostWithStore + Send,178for<'a> D::Data<'a>: foo::foo::manyarg::Host + Send,179T: 'static + Send,180{181foo::foo::manyarg::add_to_linker::<T, D>(linker, host_getter)?;182Ok(())183}184pub fn foo_foo_manyarg(&self) -> &exports::foo::foo::manyarg::Guest {185&self.interface0186}187}188};189pub mod foo {190pub mod foo {191#[allow(clippy::all)]192pub mod manyarg {193#[allow(unused_imports)]194use wasmtime::component::__internal::{anyhow, Box};195#[derive(wasmtime::component::ComponentType)]196#[derive(wasmtime::component::Lift)]197#[derive(wasmtime::component::Lower)]198#[component(record)]199#[derive(Clone)]200pub struct BigStruct {201#[component(name = "a1")]202pub a1: wasmtime::component::__internal::String,203#[component(name = "a2")]204pub a2: wasmtime::component::__internal::String,205#[component(name = "a3")]206pub a3: wasmtime::component::__internal::String,207#[component(name = "a4")]208pub a4: wasmtime::component::__internal::String,209#[component(name = "a5")]210pub a5: wasmtime::component::__internal::String,211#[component(name = "a6")]212pub a6: wasmtime::component::__internal::String,213#[component(name = "a7")]214pub a7: wasmtime::component::__internal::String,215#[component(name = "a8")]216pub a8: wasmtime::component::__internal::String,217#[component(name = "a9")]218pub a9: wasmtime::component::__internal::String,219#[component(name = "a10")]220pub a10: wasmtime::component::__internal::String,221#[component(name = "a11")]222pub a11: wasmtime::component::__internal::String,223#[component(name = "a12")]224pub a12: wasmtime::component::__internal::String,225#[component(name = "a13")]226pub a13: wasmtime::component::__internal::String,227#[component(name = "a14")]228pub a14: wasmtime::component::__internal::String,229#[component(name = "a15")]230pub a15: wasmtime::component::__internal::String,231#[component(name = "a16")]232pub a16: wasmtime::component::__internal::String,233#[component(name = "a17")]234pub a17: wasmtime::component::__internal::String,235#[component(name = "a18")]236pub a18: wasmtime::component::__internal::String,237#[component(name = "a19")]238pub a19: wasmtime::component::__internal::String,239#[component(name = "a20")]240pub a20: wasmtime::component::__internal::String,241}242impl core::fmt::Debug for BigStruct {243fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {244f.debug_struct("BigStruct")245.field("a1", &self.a1)246.field("a2", &self.a2)247.field("a3", &self.a3)248.field("a4", &self.a4)249.field("a5", &self.a5)250.field("a6", &self.a6)251.field("a7", &self.a7)252.field("a8", &self.a8)253.field("a9", &self.a9)254.field("a10", &self.a10)255.field("a11", &self.a11)256.field("a12", &self.a12)257.field("a13", &self.a13)258.field("a14", &self.a14)259.field("a15", &self.a15)260.field("a16", &self.a16)261.field("a17", &self.a17)262.field("a18", &self.a18)263.field("a19", &self.a19)264.field("a20", &self.a20)265.finish()266}267}268const _: () = {269assert!(270160 == < BigStruct as wasmtime::component::ComponentType >::SIZE32271);272assert!(2734 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32274);275};276pub trait HostWithStore: wasmtime::component::HasData + Send {}277impl<_T: ?Sized> HostWithStore for _T278where279_T: wasmtime::component::HasData + Send,280{}281pub trait Host: Send {282fn many_args(283&mut self,284a1: u64,285a2: u64,286a3: u64,287a4: u64,288a5: u64,289a6: u64,290a7: u64,291a8: u64,292a9: u64,293a10: u64,294a11: u64,295a12: u64,296a13: u64,297a14: u64,298a15: u64,299a16: u64,300) -> impl ::core::future::Future<Output = ()> + Send;301fn big_argument(302&mut self,303x: BigStruct,304) -> impl ::core::future::Future<Output = ()> + Send;305}306impl<_T: Host + ?Sized + Send> Host for &mut _T {307fn many_args(308&mut self,309a1: u64,310a2: u64,311a3: u64,312a4: u64,313a5: u64,314a6: u64,315a7: u64,316a8: u64,317a9: u64,318a10: u64,319a11: u64,320a12: u64,321a13: u64,322a14: u64,323a15: u64,324a16: u64,325) -> impl ::core::future::Future<Output = ()> + Send {326async move {327Host::many_args(328*self,329a1,330a2,331a3,332a4,333a5,334a6,335a7,336a8,337a9,338a10,339a11,340a12,341a13,342a14,343a15,344a16,345)346.await347}348}349fn big_argument(350&mut self,351x: BigStruct,352) -> impl ::core::future::Future<Output = ()> + Send {353async move { Host::big_argument(*self, x).await }354}355}356pub fn add_to_linker<T, D>(357linker: &mut wasmtime::component::Linker<T>,358host_getter: fn(&mut T) -> D::Data<'_>,359) -> wasmtime::Result<()>360where361D: HostWithStore,362for<'a> D::Data<'a>: Host,363T: 'static + Send,364{365let mut inst = linker.instance("foo:foo/manyarg")?;366inst.func_wrap_async(367"many-args",368move |369mut caller: wasmtime::StoreContextMut<'_, T>,370(371arg0,372arg1,373arg2,374arg3,375arg4,376arg5,377arg6,378arg7,379arg8,380arg9,381arg10,382arg11,383arg12,384arg13,385arg14,386arg15,387): (388u64,389u64,390u64,391u64,392u64,393u64,394u64,395u64,396u64,397u64,398u64,399u64,400u64,401u64,402u64,403u64,404)|405{406wasmtime::component::__internal::Box::new(async move {407let host = &mut host_getter(caller.data_mut());408let r = Host::many_args(409host,410arg0,411arg1,412arg2,413arg3,414arg4,415arg5,416arg6,417arg7,418arg8,419arg9,420arg10,421arg11,422arg12,423arg13,424arg14,425arg15,426)427.await;428Ok(r)429})430},431)?;432inst.func_wrap_async(433"big-argument",434move |435mut caller: wasmtime::StoreContextMut<'_, T>,436(arg0,): (BigStruct,)|437{438wasmtime::component::__internal::Box::new(async move {439let host = &mut host_getter(caller.data_mut());440let r = Host::big_argument(host, arg0).await;441Ok(r)442})443},444)?;445Ok(())446}447}448}449}450pub mod exports {451pub mod foo {452pub mod foo {453#[allow(clippy::all)]454pub mod manyarg {455#[allow(unused_imports)]456use wasmtime::component::__internal::{anyhow, Box};457#[derive(wasmtime::component::ComponentType)]458#[derive(wasmtime::component::Lift)]459#[derive(wasmtime::component::Lower)]460#[component(record)]461#[derive(Clone)]462pub struct BigStruct {463#[component(name = "a1")]464pub a1: wasmtime::component::__internal::String,465#[component(name = "a2")]466pub a2: wasmtime::component::__internal::String,467#[component(name = "a3")]468pub a3: wasmtime::component::__internal::String,469#[component(name = "a4")]470pub a4: wasmtime::component::__internal::String,471#[component(name = "a5")]472pub a5: wasmtime::component::__internal::String,473#[component(name = "a6")]474pub a6: wasmtime::component::__internal::String,475#[component(name = "a7")]476pub a7: wasmtime::component::__internal::String,477#[component(name = "a8")]478pub a8: wasmtime::component::__internal::String,479#[component(name = "a9")]480pub a9: wasmtime::component::__internal::String,481#[component(name = "a10")]482pub a10: wasmtime::component::__internal::String,483#[component(name = "a11")]484pub a11: wasmtime::component::__internal::String,485#[component(name = "a12")]486pub a12: wasmtime::component::__internal::String,487#[component(name = "a13")]488pub a13: wasmtime::component::__internal::String,489#[component(name = "a14")]490pub a14: wasmtime::component::__internal::String,491#[component(name = "a15")]492pub a15: wasmtime::component::__internal::String,493#[component(name = "a16")]494pub a16: wasmtime::component::__internal::String,495#[component(name = "a17")]496pub a17: wasmtime::component::__internal::String,497#[component(name = "a18")]498pub a18: wasmtime::component::__internal::String,499#[component(name = "a19")]500pub a19: wasmtime::component::__internal::String,501#[component(name = "a20")]502pub a20: wasmtime::component::__internal::String,503}504impl core::fmt::Debug for BigStruct {505fn fmt(506&self,507f: &mut core::fmt::Formatter<'_>,508) -> core::fmt::Result {509f.debug_struct("BigStruct")510.field("a1", &self.a1)511.field("a2", &self.a2)512.field("a3", &self.a3)513.field("a4", &self.a4)514.field("a5", &self.a5)515.field("a6", &self.a6)516.field("a7", &self.a7)517.field("a8", &self.a8)518.field("a9", &self.a9)519.field("a10", &self.a10)520.field("a11", &self.a11)521.field("a12", &self.a12)522.field("a13", &self.a13)523.field("a14", &self.a14)524.field("a15", &self.a15)525.field("a16", &self.a16)526.field("a17", &self.a17)527.field("a18", &self.a18)528.field("a19", &self.a19)529.field("a20", &self.a20)530.finish()531}532}533const _: () = {534assert!(535160 == < BigStruct as wasmtime::component::ComponentType536>::SIZE32537);538assert!(5394 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32540);541};542pub struct Guest {543many_args: wasmtime::component::Func,544big_argument: wasmtime::component::Func,545}546#[derive(Clone)]547pub struct GuestIndices {548many_args: wasmtime::component::ComponentExportIndex,549big_argument: wasmtime::component::ComponentExportIndex,550}551impl GuestIndices {552/// Constructor for [`GuestIndices`] which takes a553/// [`Component`](wasmtime::component::Component) as input and can be executed554/// before instantiation.555///556/// This constructor can be used to front-load string lookups to find exports557/// within a component.558pub fn new<_T>(559_instance_pre: &wasmtime::component::InstancePre<_T>,560) -> wasmtime::Result<GuestIndices> {561let instance = _instance_pre562.component()563.get_export_index(None, "foo:foo/manyarg")564.ok_or_else(|| {565anyhow::anyhow!(566"no exported instance named `foo:foo/manyarg`"567)568})?;569let mut lookup = move |name| {570_instance_pre571.component()572.get_export_index(Some(&instance), name)573.ok_or_else(|| {574anyhow::anyhow!(575"instance export `foo:foo/manyarg` does \576not have export `{name}`"577)578})579};580let _ = &mut lookup;581let many_args = lookup("many-args")?;582let big_argument = lookup("big-argument")?;583Ok(GuestIndices {584many_args,585big_argument,586})587}588pub fn load(589&self,590mut store: impl wasmtime::AsContextMut,591instance: &wasmtime::component::Instance,592) -> wasmtime::Result<Guest> {593let _instance = instance;594let _instance_pre = _instance.instance_pre(&store);595let _instance_type = _instance_pre.instance_type();596let mut store = store.as_context_mut();597let _ = &mut store;598let many_args = *_instance599.get_typed_func::<600(601u64,602u64,603u64,604u64,605u64,606u64,607u64,608u64,609u64,610u64,611u64,612u64,613u64,614u64,615u64,616u64,617),618(),619>(&mut store, &self.many_args)?620.func();621let big_argument = *_instance622.get_typed_func::<623(&BigStruct,),624(),625>(&mut store, &self.big_argument)?626.func();627Ok(Guest { many_args, big_argument })628}629}630impl Guest {631pub async fn call_many_args<S: wasmtime::AsContextMut>(632&self,633mut store: S,634arg0: u64,635arg1: u64,636arg2: u64,637arg3: u64,638arg4: u64,639arg5: u64,640arg6: u64,641arg7: u64,642arg8: u64,643arg9: u64,644arg10: u64,645arg11: u64,646arg12: u64,647arg13: u64,648arg14: u64,649arg15: u64,650) -> wasmtime::Result<()>651where652<S as wasmtime::AsContext>::Data: Send,653{654let callee = unsafe {655wasmtime::component::TypedFunc::<656(657u64,658u64,659u64,660u64,661u64,662u64,663u64,664u64,665u64,666u64,667u64,668u64,669u64,670u64,671u64,672u64,673),674(),675>::new_unchecked(self.many_args)676};677let () = callee678.call_async(679store.as_context_mut(),680(681arg0,682arg1,683arg2,684arg3,685arg4,686arg5,687arg6,688arg7,689arg8,690arg9,691arg10,692arg11,693arg12,694arg13,695arg14,696arg15,697),698)699.await?;700callee.post_return_async(store.as_context_mut()).await?;701Ok(())702}703pub async fn call_big_argument<S: wasmtime::AsContextMut>(704&self,705mut store: S,706arg0: &BigStruct,707) -> wasmtime::Result<()>708where709<S as wasmtime::AsContext>::Data: Send,710{711let callee = unsafe {712wasmtime::component::TypedFunc::<713(&BigStruct,),714(),715>::new_unchecked(self.big_argument)716};717let () = callee718.call_async(store.as_context_mut(), (arg0,))719.await?;720callee.post_return_async(store.as_context_mut()).await?;721Ok(())722}723}724}725}726}727}728729730