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