Path: blob/main/crates/component-macro/tests/expanded/floats_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::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,)| {253use tracing::Instrument;254let span = tracing::span!(255tracing::Level::TRACE, "wit-bindgen import", module =256"floats", function = "f32-param",257);258wasmtime::component::__internal::Box::new(259async move {260tracing::event!(261tracing::Level::TRACE, x = tracing::field::debug(& arg0),262"call"263);264let host = &mut host_getter(caller.data_mut());265let r = Host::f32_param(host, arg0).await;266tracing::event!(267tracing::Level::TRACE, result = tracing::field::debug(& r),268"return"269);270Ok(r)271}272.instrument(span),273)274},275)?;276inst.func_wrap_async(277"f64-param",278move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (f64,)| {279use tracing::Instrument;280let span = tracing::span!(281tracing::Level::TRACE, "wit-bindgen import", module =282"floats", function = "f64-param",283);284wasmtime::component::__internal::Box::new(285async move {286tracing::event!(287tracing::Level::TRACE, x = tracing::field::debug(& arg0),288"call"289);290let host = &mut host_getter(caller.data_mut());291let r = Host::f64_param(host, arg0).await;292tracing::event!(293tracing::Level::TRACE, result = tracing::field::debug(& r),294"return"295);296Ok(r)297}298.instrument(span),299)300},301)?;302inst.func_wrap_async(303"f32-result",304move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {305use tracing::Instrument;306let span = tracing::span!(307tracing::Level::TRACE, "wit-bindgen import", module =308"floats", function = "f32-result",309);310wasmtime::component::__internal::Box::new(311async move {312tracing::event!(tracing::Level::TRACE, "call");313let host = &mut host_getter(caller.data_mut());314let r = Host::f32_result(host).await;315tracing::event!(316tracing::Level::TRACE, result = tracing::field::debug(& r),317"return"318);319Ok((r,))320}321.instrument(span),322)323},324)?;325inst.func_wrap_async(326"f64-result",327move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {328use tracing::Instrument;329let span = tracing::span!(330tracing::Level::TRACE, "wit-bindgen import", module =331"floats", function = "f64-result",332);333wasmtime::component::__internal::Box::new(334async move {335tracing::event!(tracing::Level::TRACE, "call");336let host = &mut host_getter(caller.data_mut());337let r = Host::f64_result(host).await;338tracing::event!(339tracing::Level::TRACE, result = tracing::field::debug(& r),340"return"341);342Ok((r,))343}344.instrument(span),345)346},347)?;348Ok(())349}350}351}352}353pub mod exports {354pub mod foo {355pub mod foo {356#[allow(clippy::all)]357pub mod floats {358#[allow(unused_imports)]359use wasmtime::component::__internal::{anyhow, Box};360pub struct Guest {361f32_param: wasmtime::component::Func,362f64_param: wasmtime::component::Func,363f32_result: wasmtime::component::Func,364f64_result: wasmtime::component::Func,365}366#[derive(Clone)]367pub struct GuestIndices {368f32_param: wasmtime::component::ComponentExportIndex,369f64_param: wasmtime::component::ComponentExportIndex,370f32_result: wasmtime::component::ComponentExportIndex,371f64_result: wasmtime::component::ComponentExportIndex,372}373impl GuestIndices {374/// Constructor for [`GuestIndices`] which takes a375/// [`Component`](wasmtime::component::Component) as input and can be executed376/// before instantiation.377///378/// This constructor can be used to front-load string lookups to find exports379/// within a component.380pub fn new<_T>(381_instance_pre: &wasmtime::component::InstancePre<_T>,382) -> wasmtime::Result<GuestIndices> {383let instance = _instance_pre384.component()385.get_export_index(None, "foo:foo/floats")386.ok_or_else(|| {387anyhow::anyhow!(388"no exported instance named `foo:foo/floats`"389)390})?;391let mut lookup = move |name| {392_instance_pre393.component()394.get_export_index(Some(&instance), name)395.ok_or_else(|| {396anyhow::anyhow!(397"instance export `foo:foo/floats` does \398not have export `{name}`"399)400})401};402let _ = &mut lookup;403let f32_param = lookup("f32-param")?;404let f64_param = lookup("f64-param")?;405let f32_result = lookup("f32-result")?;406let f64_result = lookup("f64-result")?;407Ok(GuestIndices {408f32_param,409f64_param,410f32_result,411f64_result,412})413}414pub fn load(415&self,416mut store: impl wasmtime::AsContextMut,417instance: &wasmtime::component::Instance,418) -> wasmtime::Result<Guest> {419let _instance = instance;420let _instance_pre = _instance.instance_pre(&store);421let _instance_type = _instance_pre.instance_type();422let mut store = store.as_context_mut();423let _ = &mut store;424let f32_param = *_instance425.get_typed_func::<(f32,), ()>(&mut store, &self.f32_param)?426.func();427let f64_param = *_instance428.get_typed_func::<(f64,), ()>(&mut store, &self.f64_param)?429.func();430let f32_result = *_instance431.get_typed_func::<(), (f32,)>(&mut store, &self.f32_result)?432.func();433let f64_result = *_instance434.get_typed_func::<(), (f64,)>(&mut store, &self.f64_result)?435.func();436Ok(Guest {437f32_param,438f64_param,439f32_result,440f64_result,441})442}443}444impl Guest {445pub async fn call_f32_param<S: wasmtime::AsContextMut>(446&self,447mut store: S,448arg0: f32,449) -> wasmtime::Result<()>450where451<S as wasmtime::AsContext>::Data: Send,452{453use tracing::Instrument;454let span = tracing::span!(455tracing::Level::TRACE, "wit-bindgen export", module =456"foo:foo/floats", function = "f32-param",457);458let callee = unsafe {459wasmtime::component::TypedFunc::<460(f32,),461(),462>::new_unchecked(self.f32_param)463};464let () = callee465.call_async(store.as_context_mut(), (arg0,))466.instrument(span.clone())467.await?;468callee469.post_return_async(store.as_context_mut())470.instrument(span)471.await?;472Ok(())473}474pub async fn call_f64_param<S: wasmtime::AsContextMut>(475&self,476mut store: S,477arg0: f64,478) -> wasmtime::Result<()>479where480<S as wasmtime::AsContext>::Data: Send,481{482use tracing::Instrument;483let span = tracing::span!(484tracing::Level::TRACE, "wit-bindgen export", module =485"foo:foo/floats", function = "f64-param",486);487let callee = unsafe {488wasmtime::component::TypedFunc::<489(f64,),490(),491>::new_unchecked(self.f64_param)492};493let () = callee494.call_async(store.as_context_mut(), (arg0,))495.instrument(span.clone())496.await?;497callee498.post_return_async(store.as_context_mut())499.instrument(span)500.await?;501Ok(())502}503pub async fn call_f32_result<S: wasmtime::AsContextMut>(504&self,505mut store: S,506) -> wasmtime::Result<f32>507where508<S as wasmtime::AsContext>::Data: Send,509{510use tracing::Instrument;511let span = tracing::span!(512tracing::Level::TRACE, "wit-bindgen export", module =513"foo:foo/floats", function = "f32-result",514);515let callee = unsafe {516wasmtime::component::TypedFunc::<517(),518(f32,),519>::new_unchecked(self.f32_result)520};521let (ret0,) = callee522.call_async(store.as_context_mut(), ())523.instrument(span.clone())524.await?;525callee526.post_return_async(store.as_context_mut())527.instrument(span)528.await?;529Ok(ret0)530}531pub async fn call_f64_result<S: wasmtime::AsContextMut>(532&self,533mut store: S,534) -> wasmtime::Result<f64>535where536<S as wasmtime::AsContext>::Data: Send,537{538use tracing::Instrument;539let span = tracing::span!(540tracing::Level::TRACE, "wit-bindgen export", module =541"foo:foo/floats", function = "f64-result",542);543let callee = unsafe {544wasmtime::component::TypedFunc::<545(),546(f64,),547>::new_unchecked(self.f64_result)548};549let (ret0,) = callee550.call_async(store.as_context_mut(), ())551.instrument(span.clone())552.await?;553callee554.post_return_async(store.as_context_mut())555.instrument(span)556.await?;557Ok(ret0)558}559}560}561}562}563}564565566