Path: blob/main/crates/component-macro/tests/expanded/many-arguments_tracing_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{406use tracing::Instrument;407let span = tracing::span!(408tracing::Level::TRACE, "wit-bindgen import", module =409"manyarg", function = "many-args",410);411wasmtime::component::__internal::Box::new(412async move {413tracing::event!(414tracing::Level::TRACE, a1 = tracing::field::debug(& arg0),415a2 = tracing::field::debug(& arg1), a3 =416tracing::field::debug(& arg2), a4 = tracing::field::debug(&417arg3), a5 = tracing::field::debug(& arg4), a6 =418tracing::field::debug(& arg5), a7 = tracing::field::debug(&419arg6), a8 = tracing::field::debug(& arg7), a9 =420tracing::field::debug(& arg8), a10 = tracing::field::debug(&421arg9), a11 = tracing::field::debug(& arg10), a12 =422tracing::field::debug(& arg11), a13 =423tracing::field::debug(& arg12), a14 =424tracing::field::debug(& arg13), a15 =425tracing::field::debug(& arg14), a16 =426tracing::field::debug(& arg15), "call"427);428let host = &mut host_getter(caller.data_mut());429let r = Host::many_args(430host,431arg0,432arg1,433arg2,434arg3,435arg4,436arg5,437arg6,438arg7,439arg8,440arg9,441arg10,442arg11,443arg12,444arg13,445arg14,446arg15,447)448.await;449tracing::event!(450tracing::Level::TRACE, result = tracing::field::debug(& r),451"return"452);453Ok(r)454}455.instrument(span),456)457},458)?;459inst.func_wrap_async(460"big-argument",461move |462mut caller: wasmtime::StoreContextMut<'_, T>,463(arg0,): (BigStruct,)|464{465use tracing::Instrument;466let span = tracing::span!(467tracing::Level::TRACE, "wit-bindgen import", module =468"manyarg", function = "big-argument",469);470wasmtime::component::__internal::Box::new(471async move {472tracing::event!(473tracing::Level::TRACE, x = tracing::field::debug(& arg0),474"call"475);476let host = &mut host_getter(caller.data_mut());477let r = Host::big_argument(host, arg0).await;478tracing::event!(479tracing::Level::TRACE, result = tracing::field::debug(& r),480"return"481);482Ok(r)483}484.instrument(span),485)486},487)?;488Ok(())489}490}491}492}493pub mod exports {494pub mod foo {495pub mod foo {496#[allow(clippy::all)]497pub mod manyarg {498#[allow(unused_imports)]499use wasmtime::component::__internal::{anyhow, Box};500#[derive(wasmtime::component::ComponentType)]501#[derive(wasmtime::component::Lift)]502#[derive(wasmtime::component::Lower)]503#[component(record)]504#[derive(Clone)]505pub struct BigStruct {506#[component(name = "a1")]507pub a1: wasmtime::component::__internal::String,508#[component(name = "a2")]509pub a2: wasmtime::component::__internal::String,510#[component(name = "a3")]511pub a3: wasmtime::component::__internal::String,512#[component(name = "a4")]513pub a4: wasmtime::component::__internal::String,514#[component(name = "a5")]515pub a5: wasmtime::component::__internal::String,516#[component(name = "a6")]517pub a6: wasmtime::component::__internal::String,518#[component(name = "a7")]519pub a7: wasmtime::component::__internal::String,520#[component(name = "a8")]521pub a8: wasmtime::component::__internal::String,522#[component(name = "a9")]523pub a9: wasmtime::component::__internal::String,524#[component(name = "a10")]525pub a10: wasmtime::component::__internal::String,526#[component(name = "a11")]527pub a11: wasmtime::component::__internal::String,528#[component(name = "a12")]529pub a12: wasmtime::component::__internal::String,530#[component(name = "a13")]531pub a13: wasmtime::component::__internal::String,532#[component(name = "a14")]533pub a14: wasmtime::component::__internal::String,534#[component(name = "a15")]535pub a15: wasmtime::component::__internal::String,536#[component(name = "a16")]537pub a16: wasmtime::component::__internal::String,538#[component(name = "a17")]539pub a17: wasmtime::component::__internal::String,540#[component(name = "a18")]541pub a18: wasmtime::component::__internal::String,542#[component(name = "a19")]543pub a19: wasmtime::component::__internal::String,544#[component(name = "a20")]545pub a20: wasmtime::component::__internal::String,546}547impl core::fmt::Debug for BigStruct {548fn fmt(549&self,550f: &mut core::fmt::Formatter<'_>,551) -> core::fmt::Result {552f.debug_struct("BigStruct")553.field("a1", &self.a1)554.field("a2", &self.a2)555.field("a3", &self.a3)556.field("a4", &self.a4)557.field("a5", &self.a5)558.field("a6", &self.a6)559.field("a7", &self.a7)560.field("a8", &self.a8)561.field("a9", &self.a9)562.field("a10", &self.a10)563.field("a11", &self.a11)564.field("a12", &self.a12)565.field("a13", &self.a13)566.field("a14", &self.a14)567.field("a15", &self.a15)568.field("a16", &self.a16)569.field("a17", &self.a17)570.field("a18", &self.a18)571.field("a19", &self.a19)572.field("a20", &self.a20)573.finish()574}575}576const _: () = {577assert!(578160 == < BigStruct as wasmtime::component::ComponentType579>::SIZE32580);581assert!(5824 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32583);584};585pub struct Guest {586many_args: wasmtime::component::Func,587big_argument: wasmtime::component::Func,588}589#[derive(Clone)]590pub struct GuestIndices {591many_args: wasmtime::component::ComponentExportIndex,592big_argument: wasmtime::component::ComponentExportIndex,593}594impl GuestIndices {595/// Constructor for [`GuestIndices`] which takes a596/// [`Component`](wasmtime::component::Component) as input and can be executed597/// before instantiation.598///599/// This constructor can be used to front-load string lookups to find exports600/// within a component.601pub fn new<_T>(602_instance_pre: &wasmtime::component::InstancePre<_T>,603) -> wasmtime::Result<GuestIndices> {604let instance = _instance_pre605.component()606.get_export_index(None, "foo:foo/manyarg")607.ok_or_else(|| {608anyhow::anyhow!(609"no exported instance named `foo:foo/manyarg`"610)611})?;612let mut lookup = move |name| {613_instance_pre614.component()615.get_export_index(Some(&instance), name)616.ok_or_else(|| {617anyhow::anyhow!(618"instance export `foo:foo/manyarg` does \619not have export `{name}`"620)621})622};623let _ = &mut lookup;624let many_args = lookup("many-args")?;625let big_argument = lookup("big-argument")?;626Ok(GuestIndices {627many_args,628big_argument,629})630}631pub fn load(632&self,633mut store: impl wasmtime::AsContextMut,634instance: &wasmtime::component::Instance,635) -> wasmtime::Result<Guest> {636let _instance = instance;637let _instance_pre = _instance.instance_pre(&store);638let _instance_type = _instance_pre.instance_type();639let mut store = store.as_context_mut();640let _ = &mut store;641let many_args = *_instance642.get_typed_func::<643(644u64,645u64,646u64,647u64,648u64,649u64,650u64,651u64,652u64,653u64,654u64,655u64,656u64,657u64,658u64,659u64,660),661(),662>(&mut store, &self.many_args)?663.func();664let big_argument = *_instance665.get_typed_func::<666(&BigStruct,),667(),668>(&mut store, &self.big_argument)?669.func();670Ok(Guest { many_args, big_argument })671}672}673impl Guest {674pub async fn call_many_args<S: wasmtime::AsContextMut>(675&self,676mut store: S,677arg0: u64,678arg1: u64,679arg2: u64,680arg3: u64,681arg4: u64,682arg5: u64,683arg6: u64,684arg7: u64,685arg8: u64,686arg9: u64,687arg10: u64,688arg11: u64,689arg12: u64,690arg13: u64,691arg14: u64,692arg15: u64,693) -> wasmtime::Result<()>694where695<S as wasmtime::AsContext>::Data: Send,696{697use tracing::Instrument;698let span = tracing::span!(699tracing::Level::TRACE, "wit-bindgen export", module =700"foo:foo/manyarg", function = "many-args",701);702let callee = unsafe {703wasmtime::component::TypedFunc::<704(705u64,706u64,707u64,708u64,709u64,710u64,711u64,712u64,713u64,714u64,715u64,716u64,717u64,718u64,719u64,720u64,721),722(),723>::new_unchecked(self.many_args)724};725let () = callee726.call_async(727store.as_context_mut(),728(729arg0,730arg1,731arg2,732arg3,733arg4,734arg5,735arg6,736arg7,737arg8,738arg9,739arg10,740arg11,741arg12,742arg13,743arg14,744arg15,745),746)747.instrument(span.clone())748.await?;749callee750.post_return_async(store.as_context_mut())751.instrument(span)752.await?;753Ok(())754}755pub async fn call_big_argument<S: wasmtime::AsContextMut>(756&self,757mut store: S,758arg0: &BigStruct,759) -> wasmtime::Result<()>760where761<S as wasmtime::AsContext>::Data: Send,762{763use tracing::Instrument;764let span = tracing::span!(765tracing::Level::TRACE, "wit-bindgen export", module =766"foo:foo/manyarg", function = "big-argument",767);768let callee = unsafe {769wasmtime::component::TypedFunc::<770(&BigStruct,),771(),772>::new_unchecked(self.big_argument)773};774let () = callee775.call_async(store.as_context_mut(), (arg0,))776.instrument(span.clone())777.await?;778callee779.post_return_async(store.as_context_mut())780.instrument(span)781.await?;782Ok(())783}784}785}786}787}788}789790791