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