Path: blob/main/crates/component-macro/tests/expanded/multiversion.rs
3073 views
/// Auto-generated bindings for a pre-instantiated version of a1/// component which implements the world `foo`.2///3/// This structure is created through [`FooPre::new`] which4/// takes a [`InstancePre`](wasmtime::component::InstancePre) that5/// has been created through a [`Linker`](wasmtime::component::Linker).6///7/// For more information see [`Foo`] as well.8pub struct FooPre<T: 'static> {9instance_pre: wasmtime::component::InstancePre<T>,10indices: FooIndices,11}12impl<T: 'static> Clone for FooPre<T> {13fn clone(&self) -> Self {14Self {15instance_pre: self.instance_pre.clone(),16indices: self.indices.clone(),17}18}19}20impl<_T: 'static> FooPre<_T> {21/// Creates a new copy of `FooPre` 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 = FooIndices::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 [`Foo`] 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<Foo> {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> FooPre<_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<Foo> {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/// `foo`.67///68/// This is an implementation detail of [`FooPre`] and can69/// be constructed if needed as well.70///71/// For more information see [`Foo`] as well.72#[derive(Clone)]73pub struct FooIndices {74interface0: exports::my::dep0_1_0::a::GuestIndices,75interface1: exports::my::dep0_2_0::a::GuestIndices,76}77/// Auto-generated bindings for an instance a component which78/// implements the world `foo`.79///80/// This structure can be created through a number of means81/// depending on your requirements and what you have on hand:82///83/// * The most convenient way is to use84/// [`Foo::instantiate`] which only needs a85/// [`Store`], [`Component`], and [`Linker`].86///87/// * Alternatively you can create a [`FooPre`] ahead of88/// time with a [`Component`] to front-load string lookups89/// of exports once instead of per-instantiation. This90/// method then uses [`FooPre::instantiate`] to91/// create a [`Foo`].92///93/// * If you've instantiated the instance yourself already94/// then you can use [`Foo::new`].95///96/// These methods are all equivalent to one another and move97/// around the tradeoff of what work is performed when.98///99/// [`Store`]: wasmtime::Store100/// [`Component`]: wasmtime::component::Component101/// [`Linker`]: wasmtime::component::Linker102pub struct Foo {103interface0: exports::my::dep0_1_0::a::Guest,104interface1: exports::my::dep0_2_0::a::Guest,105}106const _: () = {107impl FooIndices {108/// Creates a new copy of `FooIndices` 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::my::dep0_1_0::a::GuestIndices::new(_instance_pre)?;119let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_instance_pre)?;120Ok(FooIndices {121interface0,122interface1,123})124}125/// Uses the indices stored in `self` to load an instance126/// of [`Foo`] from the instance provided.127///128/// Note that at this time this method will additionally129/// perform type-checks of all exports.130pub fn load(131&self,132mut store: impl wasmtime::AsContextMut,133instance: &wasmtime::component::Instance,134) -> wasmtime::Result<Foo> {135let _ = &mut store;136let _instance = instance;137let interface0 = self.interface0.load(&mut store, &_instance)?;138let interface1 = self.interface1.load(&mut store, &_instance)?;139Ok(Foo { interface0, interface1 })140}141}142impl Foo {143/// Convenience wrapper around [`FooPre::new`] and144/// [`FooPre::instantiate`].145pub fn instantiate<_T>(146store: impl wasmtime::AsContextMut<Data = _T>,147component: &wasmtime::component::Component,148linker: &wasmtime::component::Linker<_T>,149) -> wasmtime::Result<Foo> {150let pre = linker.instantiate_pre(component)?;151FooPre::new(pre)?.instantiate(store)152}153/// Convenience wrapper around [`FooIndices::new`] and154/// [`FooIndices::load`].155pub fn new(156mut store: impl wasmtime::AsContextMut,157instance: &wasmtime::component::Instance,158) -> wasmtime::Result<Foo> {159let indices = FooIndices::new(&instance.instance_pre(&store))?;160indices.load(&mut store, instance)161}162/// Convenience wrapper around [`FooPre::new`] and163/// [`FooPre::instantiate_async`].164pub async fn instantiate_async<_T>(165store: impl wasmtime::AsContextMut<Data = _T>,166component: &wasmtime::component::Component,167linker: &wasmtime::component::Linker<_T>,168) -> wasmtime::Result<Foo>169where170_T: Send,171{172let pre = linker.instantiate_pre(component)?;173FooPre::new(pre)?.instantiate_async(store).await174}175pub fn add_to_linker<T, D>(176linker: &mut wasmtime::component::Linker<T>,177host_getter: fn(&mut T) -> D::Data<'_>,178) -> wasmtime::Result<()>179where180D: my::dep0_1_0::a::HostWithStore + my::dep0_2_0::a::HostWithStore,181for<'a> D::Data<'a>: my::dep0_1_0::a::Host + my::dep0_2_0::a::Host,182T: 'static,183{184my::dep0_1_0::a::add_to_linker::<T, D>(linker, host_getter)?;185my::dep0_2_0::a::add_to_linker::<T, D>(linker, host_getter)?;186Ok(())187}188pub fn my_dep0_1_0_a(&self) -> &exports::my::dep0_1_0::a::Guest {189&self.interface0190}191pub fn my_dep0_2_0_a(&self) -> &exports::my::dep0_2_0::a::Guest {192&self.interface1193}194}195};196pub mod my {197pub mod dep0_1_0 {198#[allow(clippy::all)]199pub mod a {200#[allow(unused_imports)]201use wasmtime::component::__internal::Box;202pub trait HostWithStore: wasmtime::component::HasData {}203impl<_T: ?Sized> HostWithStore for _T204where205_T: wasmtime::component::HasData,206{}207pub trait Host {208fn x(&mut self) -> ();209}210impl<_T: Host + ?Sized> Host for &mut _T {211fn x(&mut self) -> () {212Host::x(*self)213}214}215pub fn add_to_linker<T, D>(216linker: &mut wasmtime::component::Linker<T>,217host_getter: fn(&mut T) -> D::Data<'_>,218) -> wasmtime::Result<()>219where220D: HostWithStore,221for<'a> D::Data<'a>: Host,222T: 'static,223{224let mut inst = linker.instance("my:dep/[email protected]")?;225inst.func_wrap(226"x",227move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {228let host = &mut host_getter(caller.data_mut());229let r = Host::x(host);230Ok(r)231},232)?;233Ok(())234}235}236}237pub mod dep0_2_0 {238#[allow(clippy::all)]239pub mod a {240#[allow(unused_imports)]241use wasmtime::component::__internal::Box;242pub trait HostWithStore: wasmtime::component::HasData {}243impl<_T: ?Sized> HostWithStore for _T244where245_T: wasmtime::component::HasData,246{}247pub trait Host {248fn x(&mut self) -> ();249}250impl<_T: Host + ?Sized> Host for &mut _T {251fn x(&mut self) -> () {252Host::x(*self)253}254}255pub fn add_to_linker<T, D>(256linker: &mut wasmtime::component::Linker<T>,257host_getter: fn(&mut T) -> D::Data<'_>,258) -> wasmtime::Result<()>259where260D: HostWithStore,261for<'a> D::Data<'a>: Host,262T: 'static,263{264let mut inst = linker.instance("my:dep/[email protected]")?;265inst.func_wrap(266"x",267move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {268let host = &mut host_getter(caller.data_mut());269let r = Host::x(host);270Ok(r)271},272)?;273Ok(())274}275}276}277}278pub mod exports {279pub mod my {280pub mod dep0_1_0 {281#[allow(clippy::all)]282pub mod a {283#[allow(unused_imports)]284use wasmtime::component::__internal::Box;285#[derive(Clone)]286pub struct Guest {287x: wasmtime::component::Func,288}289#[derive(Clone)]290pub struct GuestIndices {291x: wasmtime::component::ComponentExportIndex,292}293impl GuestIndices {294/// Constructor for [`GuestIndices`] which takes a295/// [`Component`](wasmtime::component::Component) as input and can be executed296/// before instantiation.297///298/// This constructor can be used to front-load string lookups to find exports299/// within a component.300pub fn new<_T>(301_instance_pre: &wasmtime::component::InstancePre<_T>,302) -> wasmtime::Result<GuestIndices> {303let instance = _instance_pre304.component()305.get_export_index(None, "my:dep/[email protected]")306.ok_or_else(|| {307wasmtime::format_err!(308"no exported instance named `my:dep/[email protected]`"309)310})?;311let mut lookup = move |name| {312_instance_pre313.component()314.get_export_index(Some(&instance), name)315.ok_or_else(|| {316wasmtime::format_err!(317"instance export `my:dep/[email protected]` does \318not have export `{name}`"319)320})321};322let _ = &mut lookup;323let x = lookup("x")?;324Ok(GuestIndices { x })325}326pub fn load(327&self,328mut store: impl wasmtime::AsContextMut,329instance: &wasmtime::component::Instance,330) -> wasmtime::Result<Guest> {331let _instance = instance;332let _instance_pre = _instance.instance_pre(&store);333let _instance_type = _instance_pre.instance_type();334let mut store = store.as_context_mut();335let _ = &mut store;336let x = *_instance337.get_typed_func::<(), ()>(&mut store, &self.x)?338.func();339Ok(Guest { x })340}341}342impl Guest {343pub fn call_x<S: wasmtime::AsContextMut>(344&self,345mut store: S,346) -> wasmtime::Result<()> {347let callee = unsafe {348wasmtime::component::TypedFunc::<349(),350(),351>::new_unchecked(self.x)352};353let () = callee.call(store.as_context_mut(), ())?;354Ok(())355}356}357}358}359pub mod dep0_2_0 {360#[allow(clippy::all)]361pub mod a {362#[allow(unused_imports)]363use wasmtime::component::__internal::Box;364#[derive(Clone)]365pub struct Guest {366x: wasmtime::component::Func,367}368#[derive(Clone)]369pub struct GuestIndices {370x: wasmtime::component::ComponentExportIndex,371}372impl GuestIndices {373/// Constructor for [`GuestIndices`] which takes a374/// [`Component`](wasmtime::component::Component) as input and can be executed375/// before instantiation.376///377/// This constructor can be used to front-load string lookups to find exports378/// within a component.379pub fn new<_T>(380_instance_pre: &wasmtime::component::InstancePre<_T>,381) -> wasmtime::Result<GuestIndices> {382let instance = _instance_pre383.component()384.get_export_index(None, "my:dep/[email protected]")385.ok_or_else(|| {386wasmtime::format_err!(387"no exported instance named `my:dep/[email protected]`"388)389})?;390let mut lookup = move |name| {391_instance_pre392.component()393.get_export_index(Some(&instance), name)394.ok_or_else(|| {395wasmtime::format_err!(396"instance export `my:dep/[email protected]` does \397not have export `{name}`"398)399})400};401let _ = &mut lookup;402let x = lookup("x")?;403Ok(GuestIndices { x })404}405pub fn load(406&self,407mut store: impl wasmtime::AsContextMut,408instance: &wasmtime::component::Instance,409) -> wasmtime::Result<Guest> {410let _instance = instance;411let _instance_pre = _instance.instance_pre(&store);412let _instance_type = _instance_pre.instance_type();413let mut store = store.as_context_mut();414let _ = &mut store;415let x = *_instance416.get_typed_func::<(), ()>(&mut store, &self.x)?417.func();418Ok(Guest { x })419}420}421impl Guest {422pub fn call_x<S: wasmtime::AsContextMut>(423&self,424mut store: S,425) -> wasmtime::Result<()> {426let callee = unsafe {427wasmtime::component::TypedFunc::<428(),429(),430>::new_unchecked(self.x)431};432let () = callee.call(store.as_context_mut(), ())?;433Ok(())434}435}436}437}438}439}440441442