Path: blob/main/crates/component-macro/tests/expanded/char_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::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 _: () = {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::chars::GuestIndices::new(_instance_pre)?;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::chars::HostWithStore + Send,176for<'a> D::Data<'a>: foo::foo::chars::Host + Send,177T: 'static + Send,178{179foo::foo::chars::add_to_linker::<T, D>(linker, host_getter)?;180Ok(())181}182pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest {183&self.interface0184}185}186};187pub mod foo {188pub mod foo {189#[allow(clippy::all)]190pub mod chars {191#[allow(unused_imports)]192use wasmtime::component::__internal::{anyhow, Box};193pub trait HostWithStore: wasmtime::component::HasData + Send {}194impl<_T: ?Sized> HostWithStore for _T195where196_T: wasmtime::component::HasData + Send,197{}198pub trait Host: Send {199/// A function that accepts a character200fn take_char(201&mut self,202x: char,203) -> impl ::core::future::Future<Output = ()> + Send;204/// A function that returns a character205fn return_char(206&mut self,207) -> impl ::core::future::Future<Output = char> + Send;208}209impl<_T: Host + ?Sized + Send> Host for &mut _T {210/// A function that accepts a character211fn take_char(212&mut self,213x: char,214) -> impl ::core::future::Future<Output = ()> + Send {215async move { Host::take_char(*self, x).await }216}217/// A function that returns a character218fn return_char(219&mut self,220) -> impl ::core::future::Future<Output = char> + Send {221async move { Host::return_char(*self).await }222}223}224pub fn add_to_linker<T, D>(225linker: &mut wasmtime::component::Linker<T>,226host_getter: fn(&mut T) -> D::Data<'_>,227) -> wasmtime::Result<()>228where229D: HostWithStore,230for<'a> D::Data<'a>: Host,231T: 'static + Send,232{233let mut inst = linker.instance("foo:foo/chars")?;234inst.func_wrap_async(235"take-char",236move |237mut caller: wasmtime::StoreContextMut<'_, T>,238(arg0,): (char,)|239{240use tracing::Instrument;241let span = tracing::span!(242tracing::Level::TRACE, "wit-bindgen import", module =243"chars", function = "take-char",244);245wasmtime::component::__internal::Box::new(246async move {247tracing::event!(248tracing::Level::TRACE, x = tracing::field::debug(& arg0),249"call"250);251let host = &mut host_getter(caller.data_mut());252let r = Host::take_char(host, arg0).await;253tracing::event!(254tracing::Level::TRACE, result = tracing::field::debug(& r),255"return"256);257Ok(r)258}259.instrument(span),260)261},262)?;263inst.func_wrap_async(264"return-char",265move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {266use tracing::Instrument;267let span = tracing::span!(268tracing::Level::TRACE, "wit-bindgen import", module =269"chars", function = "return-char",270);271wasmtime::component::__internal::Box::new(272async move {273tracing::event!(tracing::Level::TRACE, "call");274let host = &mut host_getter(caller.data_mut());275let r = Host::return_char(host).await;276tracing::event!(277tracing::Level::TRACE, result = tracing::field::debug(& r),278"return"279);280Ok((r,))281}282.instrument(span),283)284},285)?;286Ok(())287}288}289}290}291pub mod exports {292pub mod foo {293pub mod foo {294#[allow(clippy::all)]295pub mod chars {296#[allow(unused_imports)]297use wasmtime::component::__internal::{anyhow, Box};298pub struct Guest {299take_char: wasmtime::component::Func,300return_char: wasmtime::component::Func,301}302#[derive(Clone)]303pub struct GuestIndices {304take_char: wasmtime::component::ComponentExportIndex,305return_char: wasmtime::component::ComponentExportIndex,306}307impl GuestIndices {308/// Constructor for [`GuestIndices`] which takes a309/// [`Component`](wasmtime::component::Component) as input and can be executed310/// before instantiation.311///312/// This constructor can be used to front-load string lookups to find exports313/// within a component.314pub fn new<_T>(315_instance_pre: &wasmtime::component::InstancePre<_T>,316) -> wasmtime::Result<GuestIndices> {317let instance = _instance_pre318.component()319.get_export_index(None, "foo:foo/chars")320.ok_or_else(|| {321anyhow::anyhow!(322"no exported instance named `foo:foo/chars`"323)324})?;325let mut lookup = move |name| {326_instance_pre327.component()328.get_export_index(Some(&instance), name)329.ok_or_else(|| {330anyhow::anyhow!(331"instance export `foo:foo/chars` does \332not have export `{name}`"333)334})335};336let _ = &mut lookup;337let take_char = lookup("take-char")?;338let return_char = lookup("return-char")?;339Ok(GuestIndices {340take_char,341return_char,342})343}344pub fn load(345&self,346mut store: impl wasmtime::AsContextMut,347instance: &wasmtime::component::Instance,348) -> wasmtime::Result<Guest> {349let _instance = instance;350let _instance_pre = _instance.instance_pre(&store);351let _instance_type = _instance_pre.instance_type();352let mut store = store.as_context_mut();353let _ = &mut store;354let take_char = *_instance355.get_typed_func::<(char,), ()>(&mut store, &self.take_char)?356.func();357let return_char = *_instance358.get_typed_func::<359(),360(char,),361>(&mut store, &self.return_char)?362.func();363Ok(Guest { take_char, return_char })364}365}366impl Guest {367/// A function that accepts a character368pub async fn call_take_char<S: wasmtime::AsContextMut>(369&self,370mut store: S,371arg0: char,372) -> wasmtime::Result<()>373where374<S as wasmtime::AsContext>::Data: Send,375{376use tracing::Instrument;377let span = tracing::span!(378tracing::Level::TRACE, "wit-bindgen export", module =379"foo:foo/chars", function = "take-char",380);381let callee = unsafe {382wasmtime::component::TypedFunc::<383(char,),384(),385>::new_unchecked(self.take_char)386};387let () = callee388.call_async(store.as_context_mut(), (arg0,))389.instrument(span.clone())390.await?;391callee392.post_return_async(store.as_context_mut())393.instrument(span)394.await?;395Ok(())396}397/// A function that returns a character398pub async fn call_return_char<S: wasmtime::AsContextMut>(399&self,400mut store: S,401) -> wasmtime::Result<char>402where403<S as wasmtime::AsContext>::Data: Send,404{405use tracing::Instrument;406let span = tracing::span!(407tracing::Level::TRACE, "wit-bindgen export", module =408"foo:foo/chars", function = "return-char",409);410let callee = unsafe {411wasmtime::component::TypedFunc::<412(),413(char,),414>::new_unchecked(self.return_char)415};416let (ret0,) = callee417.call_async(store.as_context_mut(), ())418.instrument(span.clone())419.await?;420callee421.post_return_async(store.as_context_mut())422.instrument(span)423.await?;424Ok(ret0)425}426}427}428}429}430}431432433