Path: blob/main/crates/component-macro/tests/expanded/conventions.rs
3070 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::conventions::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::conventions::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::conventions::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::conventions::HostWithStore,176for<'a> D::Data<'a>: foo::foo::conventions::Host,177T: 'static,178{179foo::foo::conventions::add_to_linker::<T, D>(linker, host_getter)?;180Ok(())181}182pub fn foo_foo_conventions(&self) -> &exports::foo::foo::conventions::Guest {183&self.interface0184}185}186};187pub mod foo {188pub mod foo {189#[allow(clippy::all)]190pub mod conventions {191#[allow(unused_imports)]192use wasmtime::component::__internal::Box;193#[derive(wasmtime::component::ComponentType)]194#[derive(wasmtime::component::Lift)]195#[derive(wasmtime::component::Lower)]196#[component(record)]197#[derive(Clone, Copy)]198pub struct LudicrousSpeed {199#[component(name = "how-fast-are-you-going")]200pub how_fast_are_you_going: u32,201#[component(name = "i-am-going-extremely-slow")]202pub i_am_going_extremely_slow: u64,203}204impl core::fmt::Debug for LudicrousSpeed {205fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {206f.debug_struct("LudicrousSpeed")207.field("how-fast-are-you-going", &self.how_fast_are_you_going)208.field(209"i-am-going-extremely-slow",210&self.i_am_going_extremely_slow,211)212.finish()213}214}215const _: () = {216assert!(21716 == < LudicrousSpeed as wasmtime::component::ComponentType218>::SIZE32219);220assert!(2218 == < LudicrousSpeed as wasmtime::component::ComponentType222>::ALIGN32223);224};225pub trait HostWithStore: wasmtime::component::HasData {}226impl<_T: ?Sized> HostWithStore for _T227where228_T: wasmtime::component::HasData,229{}230pub trait Host {231fn kebab_case(&mut self) -> ();232fn foo(&mut self, x: LudicrousSpeed) -> ();233fn function_with_dashes(&mut self) -> ();234fn function_with_no_weird_characters(&mut self) -> ();235fn apple(&mut self) -> ();236fn apple_pear(&mut self) -> ();237fn apple_pear_grape(&mut self) -> ();238fn a0(&mut self) -> ();239/// Comment out identifiers that collide when mapped to snake_case, for now; see240/// https://github.com/WebAssembly/component-model/issues/118241/// APPLE: func()242/// APPLE-pear-GRAPE: func()243/// apple-PEAR-grape: func()244fn is_xml(&mut self) -> ();245fn explicit(&mut self) -> ();246fn explicit_kebab(&mut self) -> ();247/// Identifiers with the same name as keywords are quoted.248fn bool(&mut self) -> ();249}250impl<_T: Host + ?Sized> Host for &mut _T {251fn kebab_case(&mut self) -> () {252Host::kebab_case(*self)253}254fn foo(&mut self, x: LudicrousSpeed) -> () {255Host::foo(*self, x)256}257fn function_with_dashes(&mut self) -> () {258Host::function_with_dashes(*self)259}260fn function_with_no_weird_characters(&mut self) -> () {261Host::function_with_no_weird_characters(*self)262}263fn apple(&mut self) -> () {264Host::apple(*self)265}266fn apple_pear(&mut self) -> () {267Host::apple_pear(*self)268}269fn apple_pear_grape(&mut self) -> () {270Host::apple_pear_grape(*self)271}272fn a0(&mut self) -> () {273Host::a0(*self)274}275/// Comment out identifiers that collide when mapped to snake_case, for now; see276/// https://github.com/WebAssembly/component-model/issues/118277/// APPLE: func()278/// APPLE-pear-GRAPE: func()279/// apple-PEAR-grape: func()280fn is_xml(&mut self) -> () {281Host::is_xml(*self)282}283fn explicit(&mut self) -> () {284Host::explicit(*self)285}286fn explicit_kebab(&mut self) -> () {287Host::explicit_kebab(*self)288}289/// Identifiers with the same name as keywords are quoted.290fn bool(&mut self) -> () {291Host::bool(*self)292}293}294pub fn add_to_linker<T, D>(295linker: &mut wasmtime::component::Linker<T>,296host_getter: fn(&mut T) -> D::Data<'_>,297) -> wasmtime::Result<()>298where299D: HostWithStore,300for<'a> D::Data<'a>: Host,301T: 'static,302{303let mut inst = linker.instance("foo:foo/conventions")?;304inst.func_wrap(305"kebab-case",306move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {307let host = &mut host_getter(caller.data_mut());308let r = Host::kebab_case(host);309Ok(r)310},311)?;312inst.func_wrap(313"foo",314move |315mut caller: wasmtime::StoreContextMut<'_, T>,316(arg0,): (LudicrousSpeed,)|317{318let host = &mut host_getter(caller.data_mut());319let r = Host::foo(host, arg0);320Ok(r)321},322)?;323inst.func_wrap(324"function-with-dashes",325move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {326let host = &mut host_getter(caller.data_mut());327let r = Host::function_with_dashes(host);328Ok(r)329},330)?;331inst.func_wrap(332"function-with-no-weird-characters",333move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {334let host = &mut host_getter(caller.data_mut());335let r = Host::function_with_no_weird_characters(host);336Ok(r)337},338)?;339inst.func_wrap(340"apple",341move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {342let host = &mut host_getter(caller.data_mut());343let r = Host::apple(host);344Ok(r)345},346)?;347inst.func_wrap(348"apple-pear",349move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {350let host = &mut host_getter(caller.data_mut());351let r = Host::apple_pear(host);352Ok(r)353},354)?;355inst.func_wrap(356"apple-pear-grape",357move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {358let host = &mut host_getter(caller.data_mut());359let r = Host::apple_pear_grape(host);360Ok(r)361},362)?;363inst.func_wrap(364"a0",365move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {366let host = &mut host_getter(caller.data_mut());367let r = Host::a0(host);368Ok(r)369},370)?;371inst.func_wrap(372"is-XML",373move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {374let host = &mut host_getter(caller.data_mut());375let r = Host::is_xml(host);376Ok(r)377},378)?;379inst.func_wrap(380"explicit",381move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {382let host = &mut host_getter(caller.data_mut());383let r = Host::explicit(host);384Ok(r)385},386)?;387inst.func_wrap(388"explicit-kebab",389move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {390let host = &mut host_getter(caller.data_mut());391let r = Host::explicit_kebab(host);392Ok(r)393},394)?;395inst.func_wrap(396"bool",397move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {398let host = &mut host_getter(caller.data_mut());399let r = Host::bool(host);400Ok(r)401},402)?;403Ok(())404}405}406}407}408pub mod exports {409pub mod foo {410pub mod foo {411#[allow(clippy::all)]412pub mod conventions {413#[allow(unused_imports)]414use wasmtime::component::__internal::Box;415#[derive(wasmtime::component::ComponentType)]416#[derive(wasmtime::component::Lift)]417#[derive(wasmtime::component::Lower)]418#[component(record)]419#[derive(Clone, Copy)]420pub struct LudicrousSpeed {421#[component(name = "how-fast-are-you-going")]422pub how_fast_are_you_going: u32,423#[component(name = "i-am-going-extremely-slow")]424pub i_am_going_extremely_slow: u64,425}426impl core::fmt::Debug for LudicrousSpeed {427fn fmt(428&self,429f: &mut core::fmt::Formatter<'_>,430) -> core::fmt::Result {431f.debug_struct("LudicrousSpeed")432.field(433"how-fast-are-you-going",434&self.how_fast_are_you_going,435)436.field(437"i-am-going-extremely-slow",438&self.i_am_going_extremely_slow,439)440.finish()441}442}443const _: () = {444assert!(44516 == < LudicrousSpeed as wasmtime::component::ComponentType446>::SIZE32447);448assert!(4498 == < LudicrousSpeed as wasmtime::component::ComponentType450>::ALIGN32451);452};453#[derive(Clone)]454pub struct Guest {455kebab_case: wasmtime::component::Func,456foo: wasmtime::component::Func,457function_with_dashes: wasmtime::component::Func,458function_with_no_weird_characters: wasmtime::component::Func,459apple: wasmtime::component::Func,460apple_pear: wasmtime::component::Func,461apple_pear_grape: wasmtime::component::Func,462a0: wasmtime::component::Func,463is_xml: wasmtime::component::Func,464explicit: wasmtime::component::Func,465explicit_kebab: wasmtime::component::Func,466bool: wasmtime::component::Func,467}468#[derive(Clone)]469pub struct GuestIndices {470kebab_case: wasmtime::component::ComponentExportIndex,471foo: wasmtime::component::ComponentExportIndex,472function_with_dashes: wasmtime::component::ComponentExportIndex,473function_with_no_weird_characters: wasmtime::component::ComponentExportIndex,474apple: wasmtime::component::ComponentExportIndex,475apple_pear: wasmtime::component::ComponentExportIndex,476apple_pear_grape: wasmtime::component::ComponentExportIndex,477a0: wasmtime::component::ComponentExportIndex,478is_xml: wasmtime::component::ComponentExportIndex,479explicit: wasmtime::component::ComponentExportIndex,480explicit_kebab: wasmtime::component::ComponentExportIndex,481bool: wasmtime::component::ComponentExportIndex,482}483impl GuestIndices {484/// Constructor for [`GuestIndices`] which takes a485/// [`Component`](wasmtime::component::Component) as input and can be executed486/// before instantiation.487///488/// This constructor can be used to front-load string lookups to find exports489/// within a component.490pub fn new<_T>(491_instance_pre: &wasmtime::component::InstancePre<_T>,492) -> wasmtime::Result<GuestIndices> {493let instance = _instance_pre494.component()495.get_export_index(None, "foo:foo/conventions")496.ok_or_else(|| {497wasmtime::format_err!(498"no exported instance named `foo:foo/conventions`"499)500})?;501let mut lookup = move |name| {502_instance_pre503.component()504.get_export_index(Some(&instance), name)505.ok_or_else(|| {506wasmtime::format_err!(507"instance export `foo:foo/conventions` does \508not have export `{name}`"509)510})511};512let _ = &mut lookup;513let kebab_case = lookup("kebab-case")?;514let foo = lookup("foo")?;515let function_with_dashes = lookup("function-with-dashes")?;516let function_with_no_weird_characters = lookup(517"function-with-no-weird-characters",518)?;519let apple = lookup("apple")?;520let apple_pear = lookup("apple-pear")?;521let apple_pear_grape = lookup("apple-pear-grape")?;522let a0 = lookup("a0")?;523let is_xml = lookup("is-XML")?;524let explicit = lookup("explicit")?;525let explicit_kebab = lookup("explicit-kebab")?;526let bool = lookup("bool")?;527Ok(GuestIndices {528kebab_case,529foo,530function_with_dashes,531function_with_no_weird_characters,532apple,533apple_pear,534apple_pear_grape,535a0,536is_xml,537explicit,538explicit_kebab,539bool,540})541}542pub fn load(543&self,544mut store: impl wasmtime::AsContextMut,545instance: &wasmtime::component::Instance,546) -> wasmtime::Result<Guest> {547let _instance = instance;548let _instance_pre = _instance.instance_pre(&store);549let _instance_type = _instance_pre.instance_type();550let mut store = store.as_context_mut();551let _ = &mut store;552let kebab_case = *_instance553.get_typed_func::<(), ()>(&mut store, &self.kebab_case)?554.func();555let foo = *_instance556.get_typed_func::<557(LudicrousSpeed,),558(),559>(&mut store, &self.foo)?560.func();561let function_with_dashes = *_instance562.get_typed_func::<563(),564(),565>(&mut store, &self.function_with_dashes)?566.func();567let function_with_no_weird_characters = *_instance568.get_typed_func::<569(),570(),571>(&mut store, &self.function_with_no_weird_characters)?572.func();573let apple = *_instance574.get_typed_func::<(), ()>(&mut store, &self.apple)?575.func();576let apple_pear = *_instance577.get_typed_func::<(), ()>(&mut store, &self.apple_pear)?578.func();579let apple_pear_grape = *_instance580.get_typed_func::<581(),582(),583>(&mut store, &self.apple_pear_grape)?584.func();585let a0 = *_instance586.get_typed_func::<(), ()>(&mut store, &self.a0)?587.func();588let is_xml = *_instance589.get_typed_func::<(), ()>(&mut store, &self.is_xml)?590.func();591let explicit = *_instance592.get_typed_func::<(), ()>(&mut store, &self.explicit)?593.func();594let explicit_kebab = *_instance595.get_typed_func::<(), ()>(&mut store, &self.explicit_kebab)?596.func();597let bool = *_instance598.get_typed_func::<(), ()>(&mut store, &self.bool)?599.func();600Ok(Guest {601kebab_case,602foo,603function_with_dashes,604function_with_no_weird_characters,605apple,606apple_pear,607apple_pear_grape,608a0,609is_xml,610explicit,611explicit_kebab,612bool,613})614}615}616impl Guest {617pub fn call_kebab_case<S: wasmtime::AsContextMut>(618&self,619mut store: S,620) -> wasmtime::Result<()> {621let callee = unsafe {622wasmtime::component::TypedFunc::<623(),624(),625>::new_unchecked(self.kebab_case)626};627let () = callee.call(store.as_context_mut(), ())?;628Ok(())629}630pub fn call_foo<S: wasmtime::AsContextMut>(631&self,632mut store: S,633arg0: LudicrousSpeed,634) -> wasmtime::Result<()> {635let callee = unsafe {636wasmtime::component::TypedFunc::<637(LudicrousSpeed,),638(),639>::new_unchecked(self.foo)640};641let () = callee.call(store.as_context_mut(), (arg0,))?;642Ok(())643}644pub fn call_function_with_dashes<S: wasmtime::AsContextMut>(645&self,646mut store: S,647) -> wasmtime::Result<()> {648let callee = unsafe {649wasmtime::component::TypedFunc::<650(),651(),652>::new_unchecked(self.function_with_dashes)653};654let () = callee.call(store.as_context_mut(), ())?;655Ok(())656}657pub fn call_function_with_no_weird_characters<658S: wasmtime::AsContextMut,659>(&self, mut store: S) -> wasmtime::Result<()> {660let callee = unsafe {661wasmtime::component::TypedFunc::<662(),663(),664>::new_unchecked(self.function_with_no_weird_characters)665};666let () = callee.call(store.as_context_mut(), ())?;667Ok(())668}669pub fn call_apple<S: wasmtime::AsContextMut>(670&self,671mut store: S,672) -> wasmtime::Result<()> {673let callee = unsafe {674wasmtime::component::TypedFunc::<675(),676(),677>::new_unchecked(self.apple)678};679let () = callee.call(store.as_context_mut(), ())?;680Ok(())681}682pub fn call_apple_pear<S: wasmtime::AsContextMut>(683&self,684mut store: S,685) -> wasmtime::Result<()> {686let callee = unsafe {687wasmtime::component::TypedFunc::<688(),689(),690>::new_unchecked(self.apple_pear)691};692let () = callee.call(store.as_context_mut(), ())?;693Ok(())694}695pub fn call_apple_pear_grape<S: wasmtime::AsContextMut>(696&self,697mut store: S,698) -> wasmtime::Result<()> {699let callee = unsafe {700wasmtime::component::TypedFunc::<701(),702(),703>::new_unchecked(self.apple_pear_grape)704};705let () = callee.call(store.as_context_mut(), ())?;706Ok(())707}708pub fn call_a0<S: wasmtime::AsContextMut>(709&self,710mut store: S,711) -> wasmtime::Result<()> {712let callee = unsafe {713wasmtime::component::TypedFunc::<714(),715(),716>::new_unchecked(self.a0)717};718let () = callee.call(store.as_context_mut(), ())?;719Ok(())720}721/// Comment out identifiers that collide when mapped to snake_case, for now; see722/// https://github.com/WebAssembly/component-model/issues/118723/// APPLE: func()724/// APPLE-pear-GRAPE: func()725/// apple-PEAR-grape: func()726pub fn call_is_xml<S: wasmtime::AsContextMut>(727&self,728mut store: S,729) -> wasmtime::Result<()> {730let callee = unsafe {731wasmtime::component::TypedFunc::<732(),733(),734>::new_unchecked(self.is_xml)735};736let () = callee.call(store.as_context_mut(), ())?;737Ok(())738}739pub fn call_explicit<S: wasmtime::AsContextMut>(740&self,741mut store: S,742) -> wasmtime::Result<()> {743let callee = unsafe {744wasmtime::component::TypedFunc::<745(),746(),747>::new_unchecked(self.explicit)748};749let () = callee.call(store.as_context_mut(), ())?;750Ok(())751}752pub fn call_explicit_kebab<S: wasmtime::AsContextMut>(753&self,754mut store: S,755) -> wasmtime::Result<()> {756let callee = unsafe {757wasmtime::component::TypedFunc::<758(),759(),760>::new_unchecked(self.explicit_kebab)761};762let () = callee.call(store.as_context_mut(), ())?;763Ok(())764}765/// Identifiers with the same name as keywords are quoted.766pub fn call_bool<S: wasmtime::AsContextMut>(767&self,768mut store: S,769) -> wasmtime::Result<()> {770let callee = unsafe {771wasmtime::component::TypedFunc::<772(),773(),774>::new_unchecked(self.bool)775};776let () = callee.call(store.as_context_mut(), ())?;777Ok(())778}779}780}781}782}783}784785786