Path: blob/main/crates/component-macro/tests/expanded/floats_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::floats::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::floats::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::floats::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::floats::HostWithStore + Send,178for<'a> D::Data<'a>: foo::foo::floats::Host + Send,179T: 'static + Send,180{181foo::foo::floats::add_to_linker::<T, D>(linker, host_getter)?;182Ok(())183}184pub fn foo_foo_floats(&self) -> &exports::foo::foo::floats::Guest {185&self.interface0186}187}188};189pub mod foo {190pub mod foo {191#[allow(clippy::all)]192pub mod floats {193#[allow(unused_imports)]194use wasmtime::component::__internal::{anyhow, Box};195pub trait HostWithStore: wasmtime::component::HasData + Send {}196impl<_T: ?Sized> HostWithStore for _T197where198_T: wasmtime::component::HasData + Send,199{}200pub trait Host: Send {201fn f32_param(202&mut self,203x: f32,204) -> impl ::core::future::Future<Output = ()> + Send;205fn f64_param(206&mut self,207x: f64,208) -> impl ::core::future::Future<Output = ()> + Send;209fn f32_result(210&mut self,211) -> impl ::core::future::Future<Output = f32> + Send;212fn f64_result(213&mut self,214) -> impl ::core::future::Future<Output = f64> + Send;215}216impl<_T: Host + ?Sized + Send> Host for &mut _T {217fn f32_param(218&mut self,219x: f32,220) -> impl ::core::future::Future<Output = ()> + Send {221async move { Host::f32_param(*self, x).await }222}223fn f64_param(224&mut self,225x: f64,226) -> impl ::core::future::Future<Output = ()> + Send {227async move { Host::f64_param(*self, x).await }228}229fn f32_result(230&mut self,231) -> impl ::core::future::Future<Output = f32> + Send {232async move { Host::f32_result(*self).await }233}234fn f64_result(235&mut self,236) -> impl ::core::future::Future<Output = f64> + Send {237async move { Host::f64_result(*self).await }238}239}240pub fn add_to_linker<T, D>(241linker: &mut wasmtime::component::Linker<T>,242host_getter: fn(&mut T) -> D::Data<'_>,243) -> wasmtime::Result<()>244where245D: HostWithStore,246for<'a> D::Data<'a>: Host,247T: 'static + Send,248{249let mut inst = linker.instance("foo:foo/floats")?;250inst.func_wrap_async(251"f32-param",252move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (f32,)| {253wasmtime::component::__internal::Box::new(async move {254let host = &mut host_getter(caller.data_mut());255let r = Host::f32_param(host, arg0).await;256Ok(r)257})258},259)?;260inst.func_wrap_async(261"f64-param",262move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (f64,)| {263wasmtime::component::__internal::Box::new(async move {264let host = &mut host_getter(caller.data_mut());265let r = Host::f64_param(host, arg0).await;266Ok(r)267})268},269)?;270inst.func_wrap_async(271"f32-result",272move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {273wasmtime::component::__internal::Box::new(async move {274let host = &mut host_getter(caller.data_mut());275let r = Host::f32_result(host).await;276Ok((r,))277})278},279)?;280inst.func_wrap_async(281"f64-result",282move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {283wasmtime::component::__internal::Box::new(async move {284let host = &mut host_getter(caller.data_mut());285let r = Host::f64_result(host).await;286Ok((r,))287})288},289)?;290Ok(())291}292}293}294}295pub mod exports {296pub mod foo {297pub mod foo {298#[allow(clippy::all)]299pub mod floats {300#[allow(unused_imports)]301use wasmtime::component::__internal::{anyhow, Box};302pub struct Guest {303f32_param: wasmtime::component::Func,304f64_param: wasmtime::component::Func,305f32_result: wasmtime::component::Func,306f64_result: wasmtime::component::Func,307}308#[derive(Clone)]309pub struct GuestIndices {310f32_param: wasmtime::component::ComponentExportIndex,311f64_param: wasmtime::component::ComponentExportIndex,312f32_result: wasmtime::component::ComponentExportIndex,313f64_result: wasmtime::component::ComponentExportIndex,314}315impl GuestIndices {316/// Constructor for [`GuestIndices`] which takes a317/// [`Component`](wasmtime::component::Component) as input and can be executed318/// before instantiation.319///320/// This constructor can be used to front-load string lookups to find exports321/// within a component.322pub fn new<_T>(323_instance_pre: &wasmtime::component::InstancePre<_T>,324) -> wasmtime::Result<GuestIndices> {325let instance = _instance_pre326.component()327.get_export_index(None, "foo:foo/floats")328.ok_or_else(|| {329anyhow::anyhow!(330"no exported instance named `foo:foo/floats`"331)332})?;333let mut lookup = move |name| {334_instance_pre335.component()336.get_export_index(Some(&instance), name)337.ok_or_else(|| {338anyhow::anyhow!(339"instance export `foo:foo/floats` does \340not have export `{name}`"341)342})343};344let _ = &mut lookup;345let f32_param = lookup("f32-param")?;346let f64_param = lookup("f64-param")?;347let f32_result = lookup("f32-result")?;348let f64_result = lookup("f64-result")?;349Ok(GuestIndices {350f32_param,351f64_param,352f32_result,353f64_result,354})355}356pub fn load(357&self,358mut store: impl wasmtime::AsContextMut,359instance: &wasmtime::component::Instance,360) -> wasmtime::Result<Guest> {361let _instance = instance;362let _instance_pre = _instance.instance_pre(&store);363let _instance_type = _instance_pre.instance_type();364let mut store = store.as_context_mut();365let _ = &mut store;366let f32_param = *_instance367.get_typed_func::<(f32,), ()>(&mut store, &self.f32_param)?368.func();369let f64_param = *_instance370.get_typed_func::<(f64,), ()>(&mut store, &self.f64_param)?371.func();372let f32_result = *_instance373.get_typed_func::<(), (f32,)>(&mut store, &self.f32_result)?374.func();375let f64_result = *_instance376.get_typed_func::<(), (f64,)>(&mut store, &self.f64_result)?377.func();378Ok(Guest {379f32_param,380f64_param,381f32_result,382f64_result,383})384}385}386impl Guest {387pub async fn call_f32_param<S: wasmtime::AsContextMut>(388&self,389mut store: S,390arg0: f32,391) -> wasmtime::Result<()>392where393<S as wasmtime::AsContext>::Data: Send,394{395let callee = unsafe {396wasmtime::component::TypedFunc::<397(f32,),398(),399>::new_unchecked(self.f32_param)400};401let () = callee402.call_async(store.as_context_mut(), (arg0,))403.await?;404callee.post_return_async(store.as_context_mut()).await?;405Ok(())406}407pub async fn call_f64_param<S: wasmtime::AsContextMut>(408&self,409mut store: S,410arg0: f64,411) -> wasmtime::Result<()>412where413<S as wasmtime::AsContext>::Data: Send,414{415let callee = unsafe {416wasmtime::component::TypedFunc::<417(f64,),418(),419>::new_unchecked(self.f64_param)420};421let () = callee422.call_async(store.as_context_mut(), (arg0,))423.await?;424callee.post_return_async(store.as_context_mut()).await?;425Ok(())426}427pub async fn call_f32_result<S: wasmtime::AsContextMut>(428&self,429mut store: S,430) -> wasmtime::Result<f32>431where432<S as wasmtime::AsContext>::Data: Send,433{434let callee = unsafe {435wasmtime::component::TypedFunc::<436(),437(f32,),438>::new_unchecked(self.f32_result)439};440let (ret0,) = callee441.call_async(store.as_context_mut(), ())442.await?;443callee.post_return_async(store.as_context_mut()).await?;444Ok(ret0)445}446pub async fn call_f64_result<S: wasmtime::AsContextMut>(447&self,448mut store: S,449) -> wasmtime::Result<f64>450where451<S as wasmtime::AsContext>::Data: Send,452{453let callee = unsafe {454wasmtime::component::TypedFunc::<455(),456(f64,),457>::new_unchecked(self.f64_result)458};459let (ret0,) = callee460.call_async(store.as_context_mut(), ())461.await?;462callee.post_return_async(store.as_context_mut()).await?;463Ok(ret0)464}465}466}467}468}469}470471472