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