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