Path: blob/main/crates/component-macro/tests/expanded/many-arguments_async.rs
3092 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 _: () = {105impl TheWorldIndices {106/// Creates a new copy of `TheWorldIndices` bindings which can then107/// be used to instantiate into a particular store.108///109/// This method may fail if the component does not have the110/// required exports.111pub fn new<_T>(112_instance_pre: &wasmtime::component::InstancePre<_T>,113) -> wasmtime::Result<Self> {114let _component = _instance_pre.component();115let _instance_type = _instance_pre.instance_type();116let interface0 = exports::foo::foo::manyarg::GuestIndices::new(117_instance_pre,118)?;119Ok(TheWorldIndices { interface0 })120}121/// Uses the indices stored in `self` to load an instance122/// of [`TheWorld`] 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<TheWorld> {131let _ = &mut store;132let _instance = instance;133let interface0 = self.interface0.load(&mut store, &_instance)?;134Ok(TheWorld { interface0 })135}136}137impl TheWorld {138/// Convenience wrapper around [`TheWorldPre::new`] and139/// [`TheWorldPre::instantiate`].140pub fn instantiate<_T>(141store: impl wasmtime::AsContextMut<Data = _T>,142component: &wasmtime::component::Component,143linker: &wasmtime::component::Linker<_T>,144) -> wasmtime::Result<TheWorld> {145let pre = linker.instantiate_pre(component)?;146TheWorldPre::new(pre)?.instantiate(store)147}148/// Convenience wrapper around [`TheWorldIndices::new`] and149/// [`TheWorldIndices::load`].150pub fn new(151mut store: impl wasmtime::AsContextMut,152instance: &wasmtime::component::Instance,153) -> wasmtime::Result<TheWorld> {154let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;155indices.load(&mut store, instance)156}157/// Convenience wrapper around [`TheWorldPre::new`] and158/// [`TheWorldPre::instantiate_async`].159pub async fn instantiate_async<_T>(160store: impl wasmtime::AsContextMut<Data = _T>,161component: &wasmtime::component::Component,162linker: &wasmtime::component::Linker<_T>,163) -> wasmtime::Result<TheWorld>164where165_T: Send,166{167let pre = linker.instantiate_pre(component)?;168TheWorldPre::new(pre)?.instantiate_async(store).await169}170pub fn add_to_linker<T, D>(171linker: &mut wasmtime::component::Linker<T>,172host_getter: fn(&mut T) -> D::Data<'_>,173) -> wasmtime::Result<()>174where175D: foo::foo::manyarg::HostWithStore + Send,176for<'a> D::Data<'a>: foo::foo::manyarg::Host + Send,177T: 'static + Send,178{179foo::foo::manyarg::add_to_linker::<T, D>(linker, host_getter)?;180Ok(())181}182pub fn foo_foo_manyarg(&self) -> &exports::foo::foo::manyarg::Guest {183&self.interface0184}185}186};187pub mod foo {188pub mod foo {189#[allow(clippy::all)]190pub mod manyarg {191#[allow(unused_imports)]192use wasmtime::component::__internal::Box;193#[derive(wasmtime::component::ComponentType)]194#[derive(wasmtime::component::Lift)]195#[derive(wasmtime::component::Lower)]196#[component(record)]197#[derive(Clone)]198pub struct BigStruct {199#[component(name = "a1")]200pub a1: wasmtime::component::__internal::String,201#[component(name = "a2")]202pub a2: wasmtime::component::__internal::String,203#[component(name = "a3")]204pub a3: wasmtime::component::__internal::String,205#[component(name = "a4")]206pub a4: wasmtime::component::__internal::String,207#[component(name = "a5")]208pub a5: wasmtime::component::__internal::String,209#[component(name = "a6")]210pub a6: wasmtime::component::__internal::String,211#[component(name = "a7")]212pub a7: wasmtime::component::__internal::String,213#[component(name = "a8")]214pub a8: wasmtime::component::__internal::String,215#[component(name = "a9")]216pub a9: wasmtime::component::__internal::String,217#[component(name = "a10")]218pub a10: wasmtime::component::__internal::String,219#[component(name = "a11")]220pub a11: wasmtime::component::__internal::String,221#[component(name = "a12")]222pub a12: wasmtime::component::__internal::String,223#[component(name = "a13")]224pub a13: wasmtime::component::__internal::String,225#[component(name = "a14")]226pub a14: wasmtime::component::__internal::String,227#[component(name = "a15")]228pub a15: wasmtime::component::__internal::String,229#[component(name = "a16")]230pub a16: wasmtime::component::__internal::String,231#[component(name = "a17")]232pub a17: wasmtime::component::__internal::String,233#[component(name = "a18")]234pub a18: wasmtime::component::__internal::String,235#[component(name = "a19")]236pub a19: wasmtime::component::__internal::String,237#[component(name = "a20")]238pub a20: wasmtime::component::__internal::String,239}240impl core::fmt::Debug for BigStruct {241fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {242f.debug_struct("BigStruct")243.field("a1", &self.a1)244.field("a2", &self.a2)245.field("a3", &self.a3)246.field("a4", &self.a4)247.field("a5", &self.a5)248.field("a6", &self.a6)249.field("a7", &self.a7)250.field("a8", &self.a8)251.field("a9", &self.a9)252.field("a10", &self.a10)253.field("a11", &self.a11)254.field("a12", &self.a12)255.field("a13", &self.a13)256.field("a14", &self.a14)257.field("a15", &self.a15)258.field("a16", &self.a16)259.field("a17", &self.a17)260.field("a18", &self.a18)261.field("a19", &self.a19)262.field("a20", &self.a20)263.finish()264}265}266const _: () = {267assert!(268160 == < BigStruct as wasmtime::component::ComponentType >::SIZE32269);270assert!(2714 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32272);273};274pub trait HostWithStore: wasmtime::component::HasData + Send {}275impl<_T: ?Sized> HostWithStore for _T276where277_T: wasmtime::component::HasData + Send,278{}279pub trait Host: Send {280fn many_args(281&mut self,282a1: u64,283a2: u64,284a3: u64,285a4: u64,286a5: u64,287a6: u64,288a7: u64,289a8: u64,290a9: u64,291a10: u64,292a11: u64,293a12: u64,294a13: u64,295a14: u64,296a15: u64,297a16: u64,298) -> impl ::core::future::Future<Output = ()> + Send;299fn big_argument(300&mut self,301x: BigStruct,302) -> impl ::core::future::Future<Output = ()> + Send;303}304impl<_T: Host + ?Sized + Send> Host for &mut _T {305fn many_args(306&mut self,307a1: u64,308a2: u64,309a3: u64,310a4: u64,311a5: u64,312a6: u64,313a7: u64,314a8: u64,315a9: u64,316a10: u64,317a11: u64,318a12: u64,319a13: u64,320a14: u64,321a15: u64,322a16: u64,323) -> impl ::core::future::Future<Output = ()> + Send {324async move {325Host::many_args(326*self,327a1,328a2,329a3,330a4,331a5,332a6,333a7,334a8,335a9,336a10,337a11,338a12,339a13,340a14,341a15,342a16,343)344.await345}346}347fn big_argument(348&mut self,349x: BigStruct,350) -> impl ::core::future::Future<Output = ()> + Send {351async move { Host::big_argument(*self, x).await }352}353}354pub fn add_to_linker<T, D>(355linker: &mut wasmtime::component::Linker<T>,356host_getter: fn(&mut T) -> D::Data<'_>,357) -> wasmtime::Result<()>358where359D: HostWithStore,360for<'a> D::Data<'a>: Host,361T: 'static + Send,362{363let mut inst = linker.instance("foo:foo/manyarg")?;364inst.func_wrap_async(365"many-args",366move |367mut caller: wasmtime::StoreContextMut<'_, T>,368(369arg0,370arg1,371arg2,372arg3,373arg4,374arg5,375arg6,376arg7,377arg8,378arg9,379arg10,380arg11,381arg12,382arg13,383arg14,384arg15,385): (386u64,387u64,388u64,389u64,390u64,391u64,392u64,393u64,394u64,395u64,396u64,397u64,398u64,399u64,400u64,401u64,402)|403{404wasmtime::component::__internal::Box::new(async move {405let host = &mut host_getter(caller.data_mut());406let r = Host::many_args(407host,408arg0,409arg1,410arg2,411arg3,412arg4,413arg5,414arg6,415arg7,416arg8,417arg9,418arg10,419arg11,420arg12,421arg13,422arg14,423arg15,424)425.await;426Ok(r)427})428},429)?;430inst.func_wrap_async(431"big-argument",432move |433mut caller: wasmtime::StoreContextMut<'_, T>,434(arg0,): (BigStruct,)|435{436wasmtime::component::__internal::Box::new(async move {437let host = &mut host_getter(caller.data_mut());438let r = Host::big_argument(host, arg0).await;439Ok(r)440})441},442)?;443Ok(())444}445}446}447}448pub mod exports {449pub mod foo {450pub mod foo {451#[allow(clippy::all)]452pub mod manyarg {453#[allow(unused_imports)]454use wasmtime::component::__internal::Box;455#[derive(wasmtime::component::ComponentType)]456#[derive(wasmtime::component::Lift)]457#[derive(wasmtime::component::Lower)]458#[component(record)]459#[derive(Clone)]460pub struct BigStruct {461#[component(name = "a1")]462pub a1: wasmtime::component::__internal::String,463#[component(name = "a2")]464pub a2: wasmtime::component::__internal::String,465#[component(name = "a3")]466pub a3: wasmtime::component::__internal::String,467#[component(name = "a4")]468pub a4: wasmtime::component::__internal::String,469#[component(name = "a5")]470pub a5: wasmtime::component::__internal::String,471#[component(name = "a6")]472pub a6: wasmtime::component::__internal::String,473#[component(name = "a7")]474pub a7: wasmtime::component::__internal::String,475#[component(name = "a8")]476pub a8: wasmtime::component::__internal::String,477#[component(name = "a9")]478pub a9: wasmtime::component::__internal::String,479#[component(name = "a10")]480pub a10: wasmtime::component::__internal::String,481#[component(name = "a11")]482pub a11: wasmtime::component::__internal::String,483#[component(name = "a12")]484pub a12: wasmtime::component::__internal::String,485#[component(name = "a13")]486pub a13: wasmtime::component::__internal::String,487#[component(name = "a14")]488pub a14: wasmtime::component::__internal::String,489#[component(name = "a15")]490pub a15: wasmtime::component::__internal::String,491#[component(name = "a16")]492pub a16: wasmtime::component::__internal::String,493#[component(name = "a17")]494pub a17: wasmtime::component::__internal::String,495#[component(name = "a18")]496pub a18: wasmtime::component::__internal::String,497#[component(name = "a19")]498pub a19: wasmtime::component::__internal::String,499#[component(name = "a20")]500pub a20: wasmtime::component::__internal::String,501}502impl core::fmt::Debug for BigStruct {503fn fmt(504&self,505f: &mut core::fmt::Formatter<'_>,506) -> core::fmt::Result {507f.debug_struct("BigStruct")508.field("a1", &self.a1)509.field("a2", &self.a2)510.field("a3", &self.a3)511.field("a4", &self.a4)512.field("a5", &self.a5)513.field("a6", &self.a6)514.field("a7", &self.a7)515.field("a8", &self.a8)516.field("a9", &self.a9)517.field("a10", &self.a10)518.field("a11", &self.a11)519.field("a12", &self.a12)520.field("a13", &self.a13)521.field("a14", &self.a14)522.field("a15", &self.a15)523.field("a16", &self.a16)524.field("a17", &self.a17)525.field("a18", &self.a18)526.field("a19", &self.a19)527.field("a20", &self.a20)528.finish()529}530}531const _: () = {532assert!(533160 == < BigStruct as wasmtime::component::ComponentType534>::SIZE32535);536assert!(5374 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32538);539};540#[derive(Clone)]541pub struct Guest {542many_args: wasmtime::component::Func,543big_argument: wasmtime::component::Func,544}545#[derive(Clone)]546pub struct GuestIndices {547many_args: wasmtime::component::ComponentExportIndex,548big_argument: wasmtime::component::ComponentExportIndex,549}550impl GuestIndices {551/// Constructor for [`GuestIndices`] which takes a552/// [`Component`](wasmtime::component::Component) as input and can be executed553/// before instantiation.554///555/// This constructor can be used to front-load string lookups to find exports556/// within a component.557pub fn new<_T>(558_instance_pre: &wasmtime::component::InstancePre<_T>,559) -> wasmtime::Result<GuestIndices> {560let instance = _instance_pre561.component()562.get_export_index(None, "foo:foo/manyarg")563.ok_or_else(|| {564wasmtime::format_err!(565"no exported instance named `foo:foo/manyarg`"566)567})?;568let mut lookup = move |name| {569_instance_pre570.component()571.get_export_index(Some(&instance), name)572.ok_or_else(|| {573wasmtime::format_err!(574"instance export `foo:foo/manyarg` does \575not have export `{name}`"576)577})578};579let _ = &mut lookup;580let many_args = lookup("many-args")?;581let big_argument = lookup("big-argument")?;582Ok(GuestIndices {583many_args,584big_argument,585})586}587pub fn load(588&self,589mut store: impl wasmtime::AsContextMut,590instance: &wasmtime::component::Instance,591) -> wasmtime::Result<Guest> {592let _instance = instance;593let _instance_pre = _instance.instance_pre(&store);594let _instance_type = _instance_pre.instance_type();595let mut store = store.as_context_mut();596let _ = &mut store;597let many_args = *_instance598.get_typed_func::<599(600u64,601u64,602u64,603u64,604u64,605u64,606u64,607u64,608u64,609u64,610u64,611u64,612u64,613u64,614u64,615u64,616),617(),618>(&mut store, &self.many_args)?619.func();620let big_argument = *_instance621.get_typed_func::<622(&BigStruct,),623(),624>(&mut store, &self.big_argument)?625.func();626Ok(Guest { many_args, big_argument })627}628}629impl Guest {630pub async fn call_many_args<S: wasmtime::AsContextMut>(631&self,632mut store: S,633arg0: u64,634arg1: u64,635arg2: u64,636arg3: u64,637arg4: u64,638arg5: u64,639arg6: u64,640arg7: u64,641arg8: u64,642arg9: u64,643arg10: u64,644arg11: u64,645arg12: u64,646arg13: u64,647arg14: u64,648arg15: u64,649) -> wasmtime::Result<()>650where651<S as wasmtime::AsContext>::Data: Send,652{653let callee = unsafe {654wasmtime::component::TypedFunc::<655(656u64,657u64,658u64,659u64,660u64,661u64,662u64,663u64,664u64,665u64,666u64,667u64,668u64,669u64,670u64,671u64,672),673(),674>::new_unchecked(self.many_args)675};676let () = callee677.call_async(678store.as_context_mut(),679(680arg0,681arg1,682arg2,683arg3,684arg4,685arg5,686arg6,687arg7,688arg8,689arg9,690arg10,691arg11,692arg12,693arg13,694arg14,695arg15,696),697)698.await?;699Ok(())700}701pub async fn call_big_argument<S: wasmtime::AsContextMut>(702&self,703mut store: S,704arg0: &BigStruct,705) -> wasmtime::Result<()>706where707<S as wasmtime::AsContext>::Data: Send,708{709let callee = unsafe {710wasmtime::component::TypedFunc::<711(&BigStruct,),712(),713>::new_unchecked(self.big_argument)714};715let () = callee716.call_async(store.as_context_mut(), (arg0,))717.await?;718Ok(())719}720}721}722}723}724}725726727