Path: blob/main/crates/component-macro/tests/expanded/multiversion.rs
1692 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 _: () = {107#[allow(unused_imports)]108use wasmtime::component::__internal::anyhow;109impl FooIndices {110/// Creates a new copy of `FooIndices` bindings which can then111/// be used to instantiate into a particular store.112///113/// This method may fail if the component does not have the114/// required exports.115pub fn new<_T>(116_instance_pre: &wasmtime::component::InstancePre<_T>,117) -> wasmtime::Result<Self> {118let _component = _instance_pre.component();119let _instance_type = _instance_pre.instance_type();120let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_instance_pre)?;121let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_instance_pre)?;122Ok(FooIndices {123interface0,124interface1,125})126}127/// Uses the indices stored in `self` to load an instance128/// of [`Foo`] from the instance provided.129///130/// Note that at this time this method will additionally131/// perform type-checks of all exports.132pub fn load(133&self,134mut store: impl wasmtime::AsContextMut,135instance: &wasmtime::component::Instance,136) -> wasmtime::Result<Foo> {137let _ = &mut store;138let _instance = instance;139let interface0 = self.interface0.load(&mut store, &_instance)?;140let interface1 = self.interface1.load(&mut store, &_instance)?;141Ok(Foo { interface0, interface1 })142}143}144impl Foo {145/// Convenience wrapper around [`FooPre::new`] and146/// [`FooPre::instantiate`].147pub fn instantiate<_T>(148store: impl wasmtime::AsContextMut<Data = _T>,149component: &wasmtime::component::Component,150linker: &wasmtime::component::Linker<_T>,151) -> wasmtime::Result<Foo> {152let pre = linker.instantiate_pre(component)?;153FooPre::new(pre)?.instantiate(store)154}155/// Convenience wrapper around [`FooIndices::new`] and156/// [`FooIndices::load`].157pub fn new(158mut store: impl wasmtime::AsContextMut,159instance: &wasmtime::component::Instance,160) -> wasmtime::Result<Foo> {161let indices = FooIndices::new(&instance.instance_pre(&store))?;162indices.load(&mut store, instance)163}164/// Convenience wrapper around [`FooPre::new`] and165/// [`FooPre::instantiate_async`].166pub async fn instantiate_async<_T>(167store: impl wasmtime::AsContextMut<Data = _T>,168component: &wasmtime::component::Component,169linker: &wasmtime::component::Linker<_T>,170) -> wasmtime::Result<Foo>171where172_T: Send,173{174let pre = linker.instantiate_pre(component)?;175FooPre::new(pre)?.instantiate_async(store).await176}177pub fn add_to_linker<T, D>(178linker: &mut wasmtime::component::Linker<T>,179host_getter: fn(&mut T) -> D::Data<'_>,180) -> wasmtime::Result<()>181where182D: my::dep0_1_0::a::HostWithStore + my::dep0_2_0::a::HostWithStore,183for<'a> D::Data<'a>: my::dep0_1_0::a::Host + my::dep0_2_0::a::Host,184T: 'static,185{186my::dep0_1_0::a::add_to_linker::<T, D>(linker, host_getter)?;187my::dep0_2_0::a::add_to_linker::<T, D>(linker, host_getter)?;188Ok(())189}190pub fn my_dep0_1_0_a(&self) -> &exports::my::dep0_1_0::a::Guest {191&self.interface0192}193pub fn my_dep0_2_0_a(&self) -> &exports::my::dep0_2_0::a::Guest {194&self.interface1195}196}197};198pub mod my {199pub mod dep0_1_0 {200#[allow(clippy::all)]201pub mod a {202#[allow(unused_imports)]203use wasmtime::component::__internal::{anyhow, Box};204pub trait HostWithStore: wasmtime::component::HasData {}205impl<_T: ?Sized> HostWithStore for _T206where207_T: wasmtime::component::HasData,208{}209pub trait Host {210fn x(&mut self) -> ();211}212impl<_T: Host + ?Sized> Host for &mut _T {213fn x(&mut self) -> () {214Host::x(*self)215}216}217pub fn add_to_linker<T, D>(218linker: &mut wasmtime::component::Linker<T>,219host_getter: fn(&mut T) -> D::Data<'_>,220) -> wasmtime::Result<()>221where222D: HostWithStore,223for<'a> D::Data<'a>: Host,224T: 'static,225{226let mut inst = linker.instance("my:dep/[email protected]")?;227inst.func_wrap(228"x",229move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {230let host = &mut host_getter(caller.data_mut());231let r = Host::x(host);232Ok(r)233},234)?;235Ok(())236}237}238}239pub mod dep0_2_0 {240#[allow(clippy::all)]241pub mod a {242#[allow(unused_imports)]243use wasmtime::component::__internal::{anyhow, Box};244pub trait HostWithStore: wasmtime::component::HasData {}245impl<_T: ?Sized> HostWithStore for _T246where247_T: wasmtime::component::HasData,248{}249pub trait Host {250fn x(&mut self) -> ();251}252impl<_T: Host + ?Sized> Host for &mut _T {253fn x(&mut self) -> () {254Host::x(*self)255}256}257pub fn add_to_linker<T, D>(258linker: &mut wasmtime::component::Linker<T>,259host_getter: fn(&mut T) -> D::Data<'_>,260) -> wasmtime::Result<()>261where262D: HostWithStore,263for<'a> D::Data<'a>: Host,264T: 'static,265{266let mut inst = linker.instance("my:dep/[email protected]")?;267inst.func_wrap(268"x",269move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {270let host = &mut host_getter(caller.data_mut());271let r = Host::x(host);272Ok(r)273},274)?;275Ok(())276}277}278}279}280pub mod exports {281pub mod my {282pub mod dep0_1_0 {283#[allow(clippy::all)]284pub mod a {285#[allow(unused_imports)]286use wasmtime::component::__internal::{anyhow, Box};287pub struct Guest {288x: wasmtime::component::Func,289}290#[derive(Clone)]291pub struct GuestIndices {292x: wasmtime::component::ComponentExportIndex,293}294impl GuestIndices {295/// Constructor for [`GuestIndices`] which takes a296/// [`Component`](wasmtime::component::Component) as input and can be executed297/// before instantiation.298///299/// This constructor can be used to front-load string lookups to find exports300/// within a component.301pub fn new<_T>(302_instance_pre: &wasmtime::component::InstancePre<_T>,303) -> wasmtime::Result<GuestIndices> {304let instance = _instance_pre305.component()306.get_export_index(None, "my:dep/[email protected]")307.ok_or_else(|| {308anyhow::anyhow!(309"no exported instance named `my:dep/[email protected]`"310)311})?;312let mut lookup = move |name| {313_instance_pre314.component()315.get_export_index(Some(&instance), name)316.ok_or_else(|| {317anyhow::anyhow!(318"instance export `my:dep/[email protected]` does \319not have export `{name}`"320)321})322};323let _ = &mut lookup;324let x = lookup("x")?;325Ok(GuestIndices { x })326}327pub fn load(328&self,329mut store: impl wasmtime::AsContextMut,330instance: &wasmtime::component::Instance,331) -> wasmtime::Result<Guest> {332let _instance = instance;333let _instance_pre = _instance.instance_pre(&store);334let _instance_type = _instance_pre.instance_type();335let mut store = store.as_context_mut();336let _ = &mut store;337let x = *_instance338.get_typed_func::<(), ()>(&mut store, &self.x)?339.func();340Ok(Guest { x })341}342}343impl Guest {344pub fn call_x<S: wasmtime::AsContextMut>(345&self,346mut store: S,347) -> wasmtime::Result<()> {348let callee = unsafe {349wasmtime::component::TypedFunc::<350(),351(),352>::new_unchecked(self.x)353};354let () = callee.call(store.as_context_mut(), ())?;355callee.post_return(store.as_context_mut())?;356Ok(())357}358}359}360}361pub mod dep0_2_0 {362#[allow(clippy::all)]363pub mod a {364#[allow(unused_imports)]365use wasmtime::component::__internal::{anyhow, Box};366pub struct Guest {367x: wasmtime::component::Func,368}369#[derive(Clone)]370pub struct GuestIndices {371x: wasmtime::component::ComponentExportIndex,372}373impl GuestIndices {374/// Constructor for [`GuestIndices`] which takes a375/// [`Component`](wasmtime::component::Component) as input and can be executed376/// before instantiation.377///378/// This constructor can be used to front-load string lookups to find exports379/// within a component.380pub fn new<_T>(381_instance_pre: &wasmtime::component::InstancePre<_T>,382) -> wasmtime::Result<GuestIndices> {383let instance = _instance_pre384.component()385.get_export_index(None, "my:dep/[email protected]")386.ok_or_else(|| {387anyhow::anyhow!(388"no exported instance named `my:dep/[email protected]`"389)390})?;391let mut lookup = move |name| {392_instance_pre393.component()394.get_export_index(Some(&instance), name)395.ok_or_else(|| {396anyhow::anyhow!(397"instance export `my:dep/[email protected]` does \398not have export `{name}`"399)400})401};402let _ = &mut lookup;403let x = lookup("x")?;404Ok(GuestIndices { x })405}406pub fn load(407&self,408mut store: impl wasmtime::AsContextMut,409instance: &wasmtime::component::Instance,410) -> wasmtime::Result<Guest> {411let _instance = instance;412let _instance_pre = _instance.instance_pre(&store);413let _instance_type = _instance_pre.instance_type();414let mut store = store.as_context_mut();415let _ = &mut store;416let x = *_instance417.get_typed_func::<(), ()>(&mut store, &self.x)?418.func();419Ok(Guest { x })420}421}422impl Guest {423pub fn call_x<S: wasmtime::AsContextMut>(424&self,425mut store: S,426) -> wasmtime::Result<()> {427let callee = unsafe {428wasmtime::component::TypedFunc::<429(),430(),431>::new_unchecked(self.x)432};433let () = callee.call(store.as_context_mut(), ())?;434callee.post_return(store.as_context_mut())?;435Ok(())436}437}438}439}440}441}442443444