Path: blob/main/crates/component-macro/tests/expanded/char_tracing_async.rs
3101 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{238use tracing::Instrument;239let span = tracing::span!(240tracing::Level::TRACE, "wit-bindgen import", module =241"chars", function = "take-char",242);243wasmtime::component::__internal::Box::new(244async move {245tracing::event!(246tracing::Level::TRACE, x = tracing::field::debug(& arg0),247"call"248);249let host = &mut host_getter(caller.data_mut());250let r = Host::take_char(host, arg0).await;251tracing::event!(252tracing::Level::TRACE, result = tracing::field::debug(& r),253"return"254);255Ok(r)256}257.instrument(span),258)259},260)?;261inst.func_wrap_async(262"return-char",263move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {264use tracing::Instrument;265let span = tracing::span!(266tracing::Level::TRACE, "wit-bindgen import", module =267"chars", function = "return-char",268);269wasmtime::component::__internal::Box::new(270async move {271tracing::event!(tracing::Level::TRACE, "call");272let host = &mut host_getter(caller.data_mut());273let r = Host::return_char(host).await;274tracing::event!(275tracing::Level::TRACE, result = tracing::field::debug(& r),276"return"277);278Ok((r,))279}280.instrument(span),281)282},283)?;284Ok(())285}286}287}288}289pub mod exports {290pub mod foo {291pub mod foo {292#[allow(clippy::all)]293pub mod chars {294#[allow(unused_imports)]295use wasmtime::component::__internal::Box;296#[derive(Clone)]297pub struct Guest {298take_char: wasmtime::component::Func,299return_char: wasmtime::component::Func,300}301#[derive(Clone)]302pub struct GuestIndices {303take_char: wasmtime::component::ComponentExportIndex,304return_char: wasmtime::component::ComponentExportIndex,305}306impl GuestIndices {307/// Constructor for [`GuestIndices`] which takes a308/// [`Component`](wasmtime::component::Component) as input and can be executed309/// before instantiation.310///311/// This constructor can be used to front-load string lookups to find exports312/// within a component.313pub fn new<_T>(314_instance_pre: &wasmtime::component::InstancePre<_T>,315) -> wasmtime::Result<GuestIndices> {316let instance = _instance_pre317.component()318.get_export_index(None, "foo:foo/chars")319.ok_or_else(|| {320wasmtime::format_err!(321"no exported instance named `foo:foo/chars`"322)323})?;324let mut lookup = move |name| {325_instance_pre326.component()327.get_export_index(Some(&instance), name)328.ok_or_else(|| {329wasmtime::format_err!(330"instance export `foo:foo/chars` does \331not have export `{name}`"332)333})334};335let _ = &mut lookup;336let take_char = lookup("take-char")?;337let return_char = lookup("return-char")?;338Ok(GuestIndices {339take_char,340return_char,341})342}343pub fn load(344&self,345mut store: impl wasmtime::AsContextMut,346instance: &wasmtime::component::Instance,347) -> wasmtime::Result<Guest> {348let _instance = instance;349let _instance_pre = _instance.instance_pre(&store);350let _instance_type = _instance_pre.instance_type();351let mut store = store.as_context_mut();352let _ = &mut store;353let take_char = *_instance354.get_typed_func::<(char,), ()>(&mut store, &self.take_char)?355.func();356let return_char = *_instance357.get_typed_func::<358(),359(char,),360>(&mut store, &self.return_char)?361.func();362Ok(Guest { take_char, return_char })363}364}365impl Guest {366/// A function that accepts a character367pub async fn call_take_char<S: wasmtime::AsContextMut>(368&self,369mut store: S,370arg0: char,371) -> wasmtime::Result<()>372where373<S as wasmtime::AsContext>::Data: Send,374{375use tracing::Instrument;376let span = tracing::span!(377tracing::Level::TRACE, "wit-bindgen export", module =378"foo:foo/chars", function = "take-char",379);380let callee = unsafe {381wasmtime::component::TypedFunc::<382(char,),383(),384>::new_unchecked(self.take_char)385};386let () = callee387.call_async(store.as_context_mut(), (arg0,))388.instrument(span.clone())389.await?;390Ok(())391}392/// A function that returns a character393pub async fn call_return_char<S: wasmtime::AsContextMut>(394&self,395mut store: S,396) -> wasmtime::Result<char>397where398<S as wasmtime::AsContext>::Data: Send,399{400use tracing::Instrument;401let span = tracing::span!(402tracing::Level::TRACE, "wit-bindgen export", module =403"foo:foo/chars", function = "return-char",404);405let callee = unsafe {406wasmtime::component::TypedFunc::<407(),408(char,),409>::new_unchecked(self.return_char)410};411let (ret0,) = callee412.call_async(store.as_context_mut(), ())413.instrument(span.clone())414.await?;415Ok(ret0)416}417}418}419}420}421}422423424