Path: blob/main/crates/component-macro/tests/expanded/floats_concurrent.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 {196fn f32_param<T>(197accessor: &wasmtime::component::Accessor<T, Self>,198x: f32,199) -> impl ::core::future::Future<Output = ()> + Send;200fn f64_param<T>(201accessor: &wasmtime::component::Accessor<T, Self>,202x: f64,203) -> impl ::core::future::Future<Output = ()> + Send;204fn f32_result<T>(205accessor: &wasmtime::component::Accessor<T, Self>,206) -> impl ::core::future::Future<Output = f32> + Send;207fn f64_result<T>(208accessor: &wasmtime::component::Accessor<T, Self>,209) -> impl ::core::future::Future<Output = f64> + Send;210}211pub trait Host: Send {}212impl<_T: Host + ?Sized + Send> Host for &mut _T {}213pub fn add_to_linker<T, D>(214linker: &mut wasmtime::component::Linker<T>,215host_getter: fn(&mut T) -> D::Data<'_>,216) -> wasmtime::Result<()>217where218D: HostWithStore,219for<'a> D::Data<'a>: Host,220T: 'static + Send,221{222let mut inst = linker.instance("foo:foo/floats")?;223inst.func_wrap_concurrent(224"f32-param",225move |caller: &wasmtime::component::Accessor<T>, (arg0,): (f32,)| {226wasmtime::component::__internal::Box::pin(async move {227let host = &caller.with_getter(host_getter);228let r = <D as HostWithStore>::f32_param(host, arg0).await;229Ok(r)230})231},232)?;233inst.func_wrap_concurrent(234"f64-param",235move |caller: &wasmtime::component::Accessor<T>, (arg0,): (f64,)| {236wasmtime::component::__internal::Box::pin(async move {237let host = &caller.with_getter(host_getter);238let r = <D as HostWithStore>::f64_param(host, arg0).await;239Ok(r)240})241},242)?;243inst.func_wrap_concurrent(244"f32-result",245move |caller: &wasmtime::component::Accessor<T>, (): ()| {246wasmtime::component::__internal::Box::pin(async move {247let host = &caller.with_getter(host_getter);248let r = <D as HostWithStore>::f32_result(host).await;249Ok((r,))250})251},252)?;253inst.func_wrap_concurrent(254"f64-result",255move |caller: &wasmtime::component::Accessor<T>, (): ()| {256wasmtime::component::__internal::Box::pin(async move {257let host = &caller.with_getter(host_getter);258let r = <D as HostWithStore>::f64_result(host).await;259Ok((r,))260})261},262)?;263Ok(())264}265}266}267}268pub mod exports {269pub mod foo {270pub mod foo {271#[allow(clippy::all)]272pub mod floats {273#[allow(unused_imports)]274use wasmtime::component::__internal::{anyhow, Box};275pub struct Guest {276f32_param: wasmtime::component::Func,277f64_param: wasmtime::component::Func,278f32_result: wasmtime::component::Func,279f64_result: wasmtime::component::Func,280}281#[derive(Clone)]282pub struct GuestIndices {283f32_param: wasmtime::component::ComponentExportIndex,284f64_param: wasmtime::component::ComponentExportIndex,285f32_result: wasmtime::component::ComponentExportIndex,286f64_result: wasmtime::component::ComponentExportIndex,287}288impl GuestIndices {289/// Constructor for [`GuestIndices`] which takes a290/// [`Component`](wasmtime::component::Component) as input and can be executed291/// before instantiation.292///293/// This constructor can be used to front-load string lookups to find exports294/// within a component.295pub fn new<_T>(296_instance_pre: &wasmtime::component::InstancePre<_T>,297) -> wasmtime::Result<GuestIndices> {298let instance = _instance_pre299.component()300.get_export_index(None, "foo:foo/floats")301.ok_or_else(|| {302anyhow::anyhow!(303"no exported instance named `foo:foo/floats`"304)305})?;306let mut lookup = move |name| {307_instance_pre308.component()309.get_export_index(Some(&instance), name)310.ok_or_else(|| {311anyhow::anyhow!(312"instance export `foo:foo/floats` does \313not have export `{name}`"314)315})316};317let _ = &mut lookup;318let f32_param = lookup("f32-param")?;319let f64_param = lookup("f64-param")?;320let f32_result = lookup("f32-result")?;321let f64_result = lookup("f64-result")?;322Ok(GuestIndices {323f32_param,324f64_param,325f32_result,326f64_result,327})328}329pub fn load(330&self,331mut store: impl wasmtime::AsContextMut,332instance: &wasmtime::component::Instance,333) -> wasmtime::Result<Guest> {334let _instance = instance;335let _instance_pre = _instance.instance_pre(&store);336let _instance_type = _instance_pre.instance_type();337let mut store = store.as_context_mut();338let _ = &mut store;339let f32_param = *_instance340.get_typed_func::<(f32,), ()>(&mut store, &self.f32_param)?341.func();342let f64_param = *_instance343.get_typed_func::<(f64,), ()>(&mut store, &self.f64_param)?344.func();345let f32_result = *_instance346.get_typed_func::<(), (f32,)>(&mut store, &self.f32_result)?347.func();348let f64_result = *_instance349.get_typed_func::<(), (f64,)>(&mut store, &self.f64_result)?350.func();351Ok(Guest {352f32_param,353f64_param,354f32_result,355f64_result,356})357}358}359impl Guest {360pub async fn call_f32_param<_T, _D>(361&self,362accessor: &wasmtime::component::Accessor<_T, _D>,363arg0: f32,364) -> wasmtime::Result<()>365where366_T: Send,367_D: wasmtime::component::HasData,368{369let callee = unsafe {370wasmtime::component::TypedFunc::<371(f32,),372(),373>::new_unchecked(self.f32_param)374};375let () = callee.call_concurrent(accessor, (arg0,)).await?;376Ok(())377}378pub async fn call_f64_param<_T, _D>(379&self,380accessor: &wasmtime::component::Accessor<_T, _D>,381arg0: f64,382) -> wasmtime::Result<()>383where384_T: Send,385_D: wasmtime::component::HasData,386{387let callee = unsafe {388wasmtime::component::TypedFunc::<389(f64,),390(),391>::new_unchecked(self.f64_param)392};393let () = callee.call_concurrent(accessor, (arg0,)).await?;394Ok(())395}396pub async fn call_f32_result<_T, _D>(397&self,398accessor: &wasmtime::component::Accessor<_T, _D>,399) -> wasmtime::Result<f32>400where401_T: Send,402_D: wasmtime::component::HasData,403{404let callee = unsafe {405wasmtime::component::TypedFunc::<406(),407(f32,),408>::new_unchecked(self.f32_result)409};410let (ret0,) = callee.call_concurrent(accessor, ()).await?;411Ok(ret0)412}413pub async fn call_f64_result<_T, _D>(414&self,415accessor: &wasmtime::component::Accessor<_T, _D>,416) -> wasmtime::Result<f64>417where418_T: Send,419_D: wasmtime::component::HasData,420{421let callee = unsafe {422wasmtime::component::TypedFunc::<423(),424(f64,),425>::new_unchecked(self.f64_result)426};427let (ret0,) = callee.call_concurrent(accessor, ()).await?;428Ok(ret0)429}430}431}432}433}434}435436437