Path: blob/main/crates/component-macro/tests/expanded/many-arguments_concurrent.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 {277fn many_args<T>(278accessor: &wasmtime::component::Accessor<T, Self>,279a1: u64,280a2: u64,281a3: u64,282a4: u64,283a5: u64,284a6: u64,285a7: u64,286a8: u64,287a9: u64,288a10: u64,289a11: u64,290a12: u64,291a13: u64,292a14: u64,293a15: u64,294a16: u64,295) -> impl ::core::future::Future<Output = ()> + Send;296fn big_argument<T>(297accessor: &wasmtime::component::Accessor<T, Self>,298x: BigStruct,299) -> impl ::core::future::Future<Output = ()> + Send;300}301pub trait Host: Send {}302impl<_T: Host + ?Sized + Send> Host for &mut _T {}303pub fn add_to_linker<T, D>(304linker: &mut wasmtime::component::Linker<T>,305host_getter: fn(&mut T) -> D::Data<'_>,306) -> wasmtime::Result<()>307where308D: HostWithStore,309for<'a> D::Data<'a>: Host,310T: 'static + Send,311{312let mut inst = linker.instance("foo:foo/manyarg")?;313inst.func_wrap_concurrent(314"many-args",315move |316caller: &wasmtime::component::Accessor<T>,317(318arg0,319arg1,320arg2,321arg3,322arg4,323arg5,324arg6,325arg7,326arg8,327arg9,328arg10,329arg11,330arg12,331arg13,332arg14,333arg15,334): (335u64,336u64,337u64,338u64,339u64,340u64,341u64,342u64,343u64,344u64,345u64,346u64,347u64,348u64,349u64,350u64,351)|352{353wasmtime::component::__internal::Box::pin(async move {354let host = &caller.with_getter(host_getter);355let r = <D as HostWithStore>::many_args(356host,357arg0,358arg1,359arg2,360arg3,361arg4,362arg5,363arg6,364arg7,365arg8,366arg9,367arg10,368arg11,369arg12,370arg13,371arg14,372arg15,373)374.await;375Ok(r)376})377},378)?;379inst.func_wrap_concurrent(380"big-argument",381move |382caller: &wasmtime::component::Accessor<T>,383(arg0,): (BigStruct,)|384{385wasmtime::component::__internal::Box::pin(async move {386let host = &caller.with_getter(host_getter);387let r = <D as HostWithStore>::big_argument(host, arg0).await;388Ok(r)389})390},391)?;392Ok(())393}394}395}396}397pub mod exports {398pub mod foo {399pub mod foo {400#[allow(clippy::all)]401pub mod manyarg {402#[allow(unused_imports)]403use wasmtime::component::__internal::{anyhow, Box};404#[derive(wasmtime::component::ComponentType)]405#[derive(wasmtime::component::Lift)]406#[derive(wasmtime::component::Lower)]407#[component(record)]408#[derive(Clone)]409pub struct BigStruct {410#[component(name = "a1")]411pub a1: wasmtime::component::__internal::String,412#[component(name = "a2")]413pub a2: wasmtime::component::__internal::String,414#[component(name = "a3")]415pub a3: wasmtime::component::__internal::String,416#[component(name = "a4")]417pub a4: wasmtime::component::__internal::String,418#[component(name = "a5")]419pub a5: wasmtime::component::__internal::String,420#[component(name = "a6")]421pub a6: wasmtime::component::__internal::String,422#[component(name = "a7")]423pub a7: wasmtime::component::__internal::String,424#[component(name = "a8")]425pub a8: wasmtime::component::__internal::String,426#[component(name = "a9")]427pub a9: wasmtime::component::__internal::String,428#[component(name = "a10")]429pub a10: wasmtime::component::__internal::String,430#[component(name = "a11")]431pub a11: wasmtime::component::__internal::String,432#[component(name = "a12")]433pub a12: wasmtime::component::__internal::String,434#[component(name = "a13")]435pub a13: wasmtime::component::__internal::String,436#[component(name = "a14")]437pub a14: wasmtime::component::__internal::String,438#[component(name = "a15")]439pub a15: wasmtime::component::__internal::String,440#[component(name = "a16")]441pub a16: wasmtime::component::__internal::String,442#[component(name = "a17")]443pub a17: wasmtime::component::__internal::String,444#[component(name = "a18")]445pub a18: wasmtime::component::__internal::String,446#[component(name = "a19")]447pub a19: wasmtime::component::__internal::String,448#[component(name = "a20")]449pub a20: wasmtime::component::__internal::String,450}451impl core::fmt::Debug for BigStruct {452fn fmt(453&self,454f: &mut core::fmt::Formatter<'_>,455) -> core::fmt::Result {456f.debug_struct("BigStruct")457.field("a1", &self.a1)458.field("a2", &self.a2)459.field("a3", &self.a3)460.field("a4", &self.a4)461.field("a5", &self.a5)462.field("a6", &self.a6)463.field("a7", &self.a7)464.field("a8", &self.a8)465.field("a9", &self.a9)466.field("a10", &self.a10)467.field("a11", &self.a11)468.field("a12", &self.a12)469.field("a13", &self.a13)470.field("a14", &self.a14)471.field("a15", &self.a15)472.field("a16", &self.a16)473.field("a17", &self.a17)474.field("a18", &self.a18)475.field("a19", &self.a19)476.field("a20", &self.a20)477.finish()478}479}480const _: () = {481assert!(482160 == < BigStruct as wasmtime::component::ComponentType483>::SIZE32484);485assert!(4864 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32487);488};489pub struct Guest {490many_args: wasmtime::component::Func,491big_argument: wasmtime::component::Func,492}493#[derive(Clone)]494pub struct GuestIndices {495many_args: wasmtime::component::ComponentExportIndex,496big_argument: wasmtime::component::ComponentExportIndex,497}498impl GuestIndices {499/// Constructor for [`GuestIndices`] which takes a500/// [`Component`](wasmtime::component::Component) as input and can be executed501/// before instantiation.502///503/// This constructor can be used to front-load string lookups to find exports504/// within a component.505pub fn new<_T>(506_instance_pre: &wasmtime::component::InstancePre<_T>,507) -> wasmtime::Result<GuestIndices> {508let instance = _instance_pre509.component()510.get_export_index(None, "foo:foo/manyarg")511.ok_or_else(|| {512anyhow::anyhow!(513"no exported instance named `foo:foo/manyarg`"514)515})?;516let mut lookup = move |name| {517_instance_pre518.component()519.get_export_index(Some(&instance), name)520.ok_or_else(|| {521anyhow::anyhow!(522"instance export `foo:foo/manyarg` does \523not have export `{name}`"524)525})526};527let _ = &mut lookup;528let many_args = lookup("many-args")?;529let big_argument = lookup("big-argument")?;530Ok(GuestIndices {531many_args,532big_argument,533})534}535pub fn load(536&self,537mut store: impl wasmtime::AsContextMut,538instance: &wasmtime::component::Instance,539) -> wasmtime::Result<Guest> {540let _instance = instance;541let _instance_pre = _instance.instance_pre(&store);542let _instance_type = _instance_pre.instance_type();543let mut store = store.as_context_mut();544let _ = &mut store;545let many_args = *_instance546.get_typed_func::<547(548u64,549u64,550u64,551u64,552u64,553u64,554u64,555u64,556u64,557u64,558u64,559u64,560u64,561u64,562u64,563u64,564),565(),566>(&mut store, &self.many_args)?567.func();568let big_argument = *_instance569.get_typed_func::<570(&BigStruct,),571(),572>(&mut store, &self.big_argument)?573.func();574Ok(Guest { many_args, big_argument })575}576}577impl Guest {578pub async fn call_many_args<_T, _D>(579&self,580accessor: &wasmtime::component::Accessor<_T, _D>,581arg0: u64,582arg1: u64,583arg2: u64,584arg3: u64,585arg4: u64,586arg5: u64,587arg6: u64,588arg7: u64,589arg8: u64,590arg9: u64,591arg10: u64,592arg11: u64,593arg12: u64,594arg13: u64,595arg14: u64,596arg15: u64,597) -> wasmtime::Result<()>598where599_T: Send,600_D: wasmtime::component::HasData,601{602let callee = unsafe {603wasmtime::component::TypedFunc::<604(605u64,606u64,607u64,608u64,609u64,610u64,611u64,612u64,613u64,614u64,615u64,616u64,617u64,618u64,619u64,620u64,621),622(),623>::new_unchecked(self.many_args)624};625let () = callee626.call_concurrent(627accessor,628(629arg0,630arg1,631arg2,632arg3,633arg4,634arg5,635arg6,636arg7,637arg8,638arg9,639arg10,640arg11,641arg12,642arg13,643arg14,644arg15,645),646)647.await?;648Ok(())649}650pub async fn call_big_argument<_T, _D>(651&self,652accessor: &wasmtime::component::Accessor<_T, _D>,653arg0: BigStruct,654) -> wasmtime::Result<()>655where656_T: Send,657_D: wasmtime::component::HasData,658{659let callee = unsafe {660wasmtime::component::TypedFunc::<661(BigStruct,),662(),663>::new_unchecked(self.big_argument)664};665let () = callee.call_concurrent(accessor, (arg0,)).await?;666Ok(())667}668}669}670}671}672}673674675