Path: blob/main/crates/component-macro/tests/expanded/char_async.rs
3069 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::chars::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::chars::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::chars::GuestIndices::new(_instance_pre)?;117Ok(TheWorldIndices { interface0 })118}119/// Uses the indices stored in `self` to load an instance120/// of [`TheWorld`] from the instance provided.121///122/// Note that at this time this method will additionally123/// perform type-checks of all exports.124pub fn load(125&self,126mut store: impl wasmtime::AsContextMut,127instance: &wasmtime::component::Instance,128) -> wasmtime::Result<TheWorld> {129let _ = &mut store;130let _instance = instance;131let interface0 = self.interface0.load(&mut store, &_instance)?;132Ok(TheWorld { interface0 })133}134}135impl TheWorld {136/// Convenience wrapper around [`TheWorldPre::new`] and137/// [`TheWorldPre::instantiate`].138pub fn instantiate<_T>(139store: impl wasmtime::AsContextMut<Data = _T>,140component: &wasmtime::component::Component,141linker: &wasmtime::component::Linker<_T>,142) -> wasmtime::Result<TheWorld> {143let pre = linker.instantiate_pre(component)?;144TheWorldPre::new(pre)?.instantiate(store)145}146/// Convenience wrapper around [`TheWorldIndices::new`] and147/// [`TheWorldIndices::load`].148pub fn new(149mut store: impl wasmtime::AsContextMut,150instance: &wasmtime::component::Instance,151) -> wasmtime::Result<TheWorld> {152let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;153indices.load(&mut store, instance)154}155/// Convenience wrapper around [`TheWorldPre::new`] and156/// [`TheWorldPre::instantiate_async`].157pub async fn instantiate_async<_T>(158store: impl wasmtime::AsContextMut<Data = _T>,159component: &wasmtime::component::Component,160linker: &wasmtime::component::Linker<_T>,161) -> wasmtime::Result<TheWorld>162where163_T: Send,164{165let pre = linker.instantiate_pre(component)?;166TheWorldPre::new(pre)?.instantiate_async(store).await167}168pub fn add_to_linker<T, D>(169linker: &mut wasmtime::component::Linker<T>,170host_getter: fn(&mut T) -> D::Data<'_>,171) -> wasmtime::Result<()>172where173D: foo::foo::chars::HostWithStore + Send,174for<'a> D::Data<'a>: foo::foo::chars::Host + Send,175T: 'static + Send,176{177foo::foo::chars::add_to_linker::<T, D>(linker, host_getter)?;178Ok(())179}180pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest {181&self.interface0182}183}184};185pub mod foo {186pub mod foo {187#[allow(clippy::all)]188pub mod chars {189#[allow(unused_imports)]190use wasmtime::component::__internal::Box;191pub trait HostWithStore: wasmtime::component::HasData + Send {}192impl<_T: ?Sized> HostWithStore for _T193where194_T: wasmtime::component::HasData + Send,195{}196pub trait Host: Send {197/// A function that accepts a character198fn take_char(199&mut self,200x: char,201) -> impl ::core::future::Future<Output = ()> + Send;202/// A function that returns a character203fn return_char(204&mut self,205) -> impl ::core::future::Future<Output = char> + Send;206}207impl<_T: Host + ?Sized + Send> Host for &mut _T {208/// A function that accepts a character209fn take_char(210&mut self,211x: char,212) -> impl ::core::future::Future<Output = ()> + Send {213async move { Host::take_char(*self, x).await }214}215/// A function that returns a character216fn return_char(217&mut self,218) -> impl ::core::future::Future<Output = char> + Send {219async move { Host::return_char(*self).await }220}221}222pub fn add_to_linker<T, D>(223linker: &mut wasmtime::component::Linker<T>,224host_getter: fn(&mut T) -> D::Data<'_>,225) -> wasmtime::Result<()>226where227D: HostWithStore,228for<'a> D::Data<'a>: Host,229T: 'static + Send,230{231let mut inst = linker.instance("foo:foo/chars")?;232inst.func_wrap_async(233"take-char",234move |235mut caller: wasmtime::StoreContextMut<'_, T>,236(arg0,): (char,)|237{238wasmtime::component::__internal::Box::new(async move {239let host = &mut host_getter(caller.data_mut());240let r = Host::take_char(host, arg0).await;241Ok(r)242})243},244)?;245inst.func_wrap_async(246"return-char",247move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {248wasmtime::component::__internal::Box::new(async move {249let host = &mut host_getter(caller.data_mut());250let r = Host::return_char(host).await;251Ok((r,))252})253},254)?;255Ok(())256}257}258}259}260pub mod exports {261pub mod foo {262pub mod foo {263#[allow(clippy::all)]264pub mod chars {265#[allow(unused_imports)]266use wasmtime::component::__internal::Box;267#[derive(Clone)]268pub struct Guest {269take_char: wasmtime::component::Func,270return_char: wasmtime::component::Func,271}272#[derive(Clone)]273pub struct GuestIndices {274take_char: wasmtime::component::ComponentExportIndex,275return_char: wasmtime::component::ComponentExportIndex,276}277impl GuestIndices {278/// Constructor for [`GuestIndices`] which takes a279/// [`Component`](wasmtime::component::Component) as input and can be executed280/// before instantiation.281///282/// This constructor can be used to front-load string lookups to find exports283/// within a component.284pub fn new<_T>(285_instance_pre: &wasmtime::component::InstancePre<_T>,286) -> wasmtime::Result<GuestIndices> {287let instance = _instance_pre288.component()289.get_export_index(None, "foo:foo/chars")290.ok_or_else(|| {291wasmtime::format_err!(292"no exported instance named `foo:foo/chars`"293)294})?;295let mut lookup = move |name| {296_instance_pre297.component()298.get_export_index(Some(&instance), name)299.ok_or_else(|| {300wasmtime::format_err!(301"instance export `foo:foo/chars` does \302not have export `{name}`"303)304})305};306let _ = &mut lookup;307let take_char = lookup("take-char")?;308let return_char = lookup("return-char")?;309Ok(GuestIndices {310take_char,311return_char,312})313}314pub fn load(315&self,316mut store: impl wasmtime::AsContextMut,317instance: &wasmtime::component::Instance,318) -> wasmtime::Result<Guest> {319let _instance = instance;320let _instance_pre = _instance.instance_pre(&store);321let _instance_type = _instance_pre.instance_type();322let mut store = store.as_context_mut();323let _ = &mut store;324let take_char = *_instance325.get_typed_func::<(char,), ()>(&mut store, &self.take_char)?326.func();327let return_char = *_instance328.get_typed_func::<329(),330(char,),331>(&mut store, &self.return_char)?332.func();333Ok(Guest { take_char, return_char })334}335}336impl Guest {337/// A function that accepts a character338pub async fn call_take_char<S: wasmtime::AsContextMut>(339&self,340mut store: S,341arg0: char,342) -> wasmtime::Result<()>343where344<S as wasmtime::AsContext>::Data: Send,345{346let callee = unsafe {347wasmtime::component::TypedFunc::<348(char,),349(),350>::new_unchecked(self.take_char)351};352let () = callee353.call_async(store.as_context_mut(), (arg0,))354.await?;355Ok(())356}357/// A function that returns a character358pub async fn call_return_char<S: wasmtime::AsContextMut>(359&self,360mut store: S,361) -> wasmtime::Result<char>362where363<S as wasmtime::AsContext>::Data: Send,364{365let callee = unsafe {366wasmtime::component::TypedFunc::<367(),368(char,),369>::new_unchecked(self.return_char)370};371let (ret0,) = callee372.call_async(store.as_context_mut(), ())373.await?;374Ok(ret0)375}376}377}378}379}380}381382383