13171ef6dSAlex Crichton /// Auto-generated bindings for a pre-instantiated version of a
25393c2bfSBruce Mitchener /// component which implements the world `imports`.
33171ef6dSAlex Crichton ///
43171ef6dSAlex Crichton /// This structure is created through [`ImportsPre::new`] which
53171ef6dSAlex Crichton /// takes a [`InstancePre`](wasmtime::component::InstancePre) that
63171ef6dSAlex Crichton /// has been created through a [`Linker`](wasmtime::component::Linker).
799d861cbSAlex Crichton ///
899d861cbSAlex Crichton /// For more information see [`Imports`] as well.
9f81c0dc0SAlex Crichton pub struct ImportsPre<T: 'static> {
103171ef6dSAlex Crichton     instance_pre: wasmtime::component::InstancePre<T>,
1199d861cbSAlex Crichton     indices: ImportsIndices,
123171ef6dSAlex Crichton }
13f81c0dc0SAlex Crichton impl<T: 'static> Clone for ImportsPre<T> {
clone(&self) -> Self14f471b4dcSAlex Crichton     fn clone(&self) -> Self {
15f471b4dcSAlex Crichton         Self {
16f471b4dcSAlex Crichton             instance_pre: self.instance_pre.clone(),
1799d861cbSAlex Crichton             indices: self.indices.clone(),
18f471b4dcSAlex Crichton         }
19f471b4dcSAlex Crichton     }
20f471b4dcSAlex Crichton }
21f81c0dc0SAlex Crichton impl<_T: 'static> ImportsPre<_T> {
223171ef6dSAlex Crichton     /// Creates a new copy of `ImportsPre` bindings which can then
233171ef6dSAlex Crichton     /// be used to instantiate into a particular store.
243171ef6dSAlex Crichton     ///
255393c2bfSBruce Mitchener     /// This method may fail if the component behind `instance_pre`
263171ef6dSAlex Crichton     /// does not have the required exports.
new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>273171ef6dSAlex Crichton     pub fn new(
283171ef6dSAlex Crichton         instance_pre: wasmtime::component::InstancePre<_T>,
293171ef6dSAlex Crichton     ) -> wasmtime::Result<Self> {
30bb77f602SPat Hickey         let indices = ImportsIndices::new(&instance_pre)?;
3199d861cbSAlex Crichton         Ok(Self { instance_pre, indices })
3299d861cbSAlex Crichton     }
engine(&self) -> &wasmtime::Engine3399d861cbSAlex Crichton     pub fn engine(&self) -> &wasmtime::Engine {
3499d861cbSAlex Crichton         self.instance_pre.engine()
3599d861cbSAlex Crichton     }
instance_pre(&self) -> &wasmtime::component::InstancePre<_T>3699d861cbSAlex Crichton     pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {
3799d861cbSAlex Crichton         &self.instance_pre
383171ef6dSAlex Crichton     }
393171ef6dSAlex Crichton     /// Instantiates a new instance of [`Imports`] within the
403171ef6dSAlex Crichton     /// `store` provided.
413171ef6dSAlex Crichton     ///
423171ef6dSAlex Crichton     /// This function will use `self` as the pre-instantiated
433171ef6dSAlex Crichton     /// instance to perform instantiation. Afterwards the preloaded
443171ef6dSAlex Crichton     /// indices in `self` are used to lookup all exports on the
453171ef6dSAlex Crichton     /// resulting instance.
instantiate( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Imports>463171ef6dSAlex Crichton     pub fn instantiate(
473171ef6dSAlex Crichton         &self,
483171ef6dSAlex Crichton         mut store: impl wasmtime::AsContextMut<Data = _T>,
493171ef6dSAlex Crichton     ) -> wasmtime::Result<Imports> {
503171ef6dSAlex Crichton         let mut store = store.as_context_mut();
5199d861cbSAlex Crichton         let instance = self.instance_pre.instantiate(&mut store)?;
5299d861cbSAlex Crichton         self.indices.load(&mut store, &instance)
5399d861cbSAlex Crichton     }
5499d861cbSAlex Crichton }
551155d6dfSAlex Crichton impl<_T: Send + 'static> ImportsPre<_T> {
561155d6dfSAlex Crichton     /// Same as [`Self::instantiate`], except with `async`.
instantiate_async( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Imports>571155d6dfSAlex Crichton     pub async fn instantiate_async(
581155d6dfSAlex Crichton         &self,
591155d6dfSAlex Crichton         mut store: impl wasmtime::AsContextMut<Data = _T>,
601155d6dfSAlex Crichton     ) -> wasmtime::Result<Imports> {
611155d6dfSAlex Crichton         let mut store = store.as_context_mut();
621155d6dfSAlex Crichton         let instance = self.instance_pre.instantiate_async(&mut store).await?;
631155d6dfSAlex Crichton         self.indices.load(&mut store, &instance)
641155d6dfSAlex Crichton     }
651155d6dfSAlex Crichton }
6699d861cbSAlex Crichton /// Auto-generated bindings for index of the exports of
6799d861cbSAlex Crichton /// `imports`.
6899d861cbSAlex Crichton ///
6999d861cbSAlex Crichton /// This is an implementation detail of [`ImportsPre`] and can
7099d861cbSAlex Crichton /// be constructed if needed as well.
7199d861cbSAlex Crichton ///
7299d861cbSAlex Crichton /// For more information see [`Imports`] as well.
7399d861cbSAlex Crichton #[derive(Clone)]
7499d861cbSAlex Crichton pub struct ImportsIndices {}
7599d861cbSAlex Crichton /// Auto-generated bindings for an instance a component which
7699d861cbSAlex Crichton /// implements the world `imports`.
7799d861cbSAlex Crichton ///
7899d861cbSAlex Crichton /// This structure can be created through a number of means
7999d861cbSAlex Crichton /// depending on your requirements and what you have on hand:
8099d861cbSAlex Crichton ///
8199d861cbSAlex Crichton /// * The most convenient way is to use
8299d861cbSAlex Crichton ///   [`Imports::instantiate`] which only needs a
8399d861cbSAlex Crichton ///   [`Store`], [`Component`], and [`Linker`].
8499d861cbSAlex Crichton ///
8599d861cbSAlex Crichton /// * Alternatively you can create a [`ImportsPre`] ahead of
8699d861cbSAlex Crichton ///   time with a [`Component`] to front-load string lookups
8799d861cbSAlex Crichton ///   of exports once instead of per-instantiation. This
8899d861cbSAlex Crichton ///   method then uses [`ImportsPre::instantiate`] to
8999d861cbSAlex Crichton ///   create a [`Imports`].
9099d861cbSAlex Crichton ///
9199d861cbSAlex Crichton /// * If you've instantiated the instance yourself already
9299d861cbSAlex Crichton ///   then you can use [`Imports::new`].
9399d861cbSAlex Crichton ///
9499d861cbSAlex Crichton /// These methods are all equivalent to one another and move
9599d861cbSAlex Crichton /// around the tradeoff of what work is performed when.
9699d861cbSAlex Crichton ///
9799d861cbSAlex Crichton /// [`Store`]: wasmtime::Store
9899d861cbSAlex Crichton /// [`Component`]: wasmtime::component::Component
9999d861cbSAlex Crichton /// [`Linker`]: wasmtime::component::Linker
10099d861cbSAlex Crichton pub struct Imports {}
10199d861cbSAlex Crichton const _: () = {
10299d861cbSAlex Crichton     impl ImportsIndices {
10399d861cbSAlex Crichton         /// Creates a new copy of `ImportsIndices` bindings which can then
10499d861cbSAlex Crichton         /// be used to instantiate into a particular store.
10599d861cbSAlex Crichton         ///
10699d861cbSAlex Crichton         /// This method may fail if the component does not have the
10799d861cbSAlex Crichton         /// required exports.
new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>108bb77f602SPat Hickey         pub fn new<_T>(
109bb77f602SPat Hickey             _instance_pre: &wasmtime::component::InstancePre<_T>,
11099d861cbSAlex Crichton         ) -> wasmtime::Result<Self> {
111bb77f602SPat Hickey             let _component = _instance_pre.component();
112bb77f602SPat Hickey             let _instance_type = _instance_pre.instance_type();
11399d861cbSAlex Crichton             Ok(ImportsIndices {})
11499d861cbSAlex Crichton         }
11599d861cbSAlex Crichton         /// Uses the indices stored in `self` to load an instance
11699d861cbSAlex Crichton         /// of [`Imports`] from the instance provided.
11799d861cbSAlex Crichton         ///
11899d861cbSAlex Crichton         /// Note that at this time this method will additionally
11999d861cbSAlex Crichton         /// perform type-checks of all exports.
load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Imports>12099d861cbSAlex Crichton         pub fn load(
12199d861cbSAlex Crichton             &self,
12299d861cbSAlex Crichton             mut store: impl wasmtime::AsContextMut,
12399d861cbSAlex Crichton             instance: &wasmtime::component::Instance,
12499d861cbSAlex Crichton         ) -> wasmtime::Result<Imports> {
125bb77f602SPat Hickey             let _ = &mut store;
12699d861cbSAlex Crichton             let _instance = instance;
1273171ef6dSAlex Crichton             Ok(Imports {})
1283171ef6dSAlex Crichton         }
1293171ef6dSAlex Crichton     }
130eea68f59SLann     impl Imports {
1313171ef6dSAlex Crichton         /// Convenience wrapper around [`ImportsPre::new`] and
1323171ef6dSAlex Crichton         /// [`ImportsPre::instantiate`].
instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Imports>1333171ef6dSAlex Crichton         pub fn instantiate<_T>(
134bb77f602SPat Hickey             store: impl wasmtime::AsContextMut<Data = _T>,
1353171ef6dSAlex Crichton             component: &wasmtime::component::Component,
1363171ef6dSAlex Crichton             linker: &wasmtime::component::Linker<_T>,
1373171ef6dSAlex Crichton         ) -> wasmtime::Result<Imports> {
1383171ef6dSAlex Crichton             let pre = linker.instantiate_pre(component)?;
1393171ef6dSAlex Crichton             ImportsPre::new(pre)?.instantiate(store)
1403171ef6dSAlex Crichton         }
141bb77f602SPat Hickey         /// Convenience wrapper around [`ImportsIndices::new`] and
14299d861cbSAlex Crichton         /// [`ImportsIndices::load`].
new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Imports>14399d861cbSAlex Crichton         pub fn new(
14499d861cbSAlex Crichton             mut store: impl wasmtime::AsContextMut,
14599d861cbSAlex Crichton             instance: &wasmtime::component::Instance,
14699d861cbSAlex Crichton         ) -> wasmtime::Result<Imports> {
147bb77f602SPat Hickey             let indices = ImportsIndices::new(&instance.instance_pre(&store))?;
148bb77f602SPat Hickey             indices.load(&mut store, instance)
14999d861cbSAlex Crichton         }
1501155d6dfSAlex Crichton         /// Convenience wrapper around [`ImportsPre::new`] and
1511155d6dfSAlex Crichton         /// [`ImportsPre::instantiate_async`].
instantiate_async<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Imports> where _T: Send,1521155d6dfSAlex Crichton         pub async fn instantiate_async<_T>(
1531155d6dfSAlex Crichton             store: impl wasmtime::AsContextMut<Data = _T>,
1541155d6dfSAlex Crichton             component: &wasmtime::component::Component,
1551155d6dfSAlex Crichton             linker: &wasmtime::component::Linker<_T>,
1561155d6dfSAlex Crichton         ) -> wasmtime::Result<Imports>
1571155d6dfSAlex Crichton         where
1581155d6dfSAlex Crichton             _T: Send,
1591155d6dfSAlex Crichton         {
1601155d6dfSAlex Crichton             let pre = linker.instantiate_pre(component)?;
1611155d6dfSAlex Crichton             ImportsPre::new(pre)?.instantiate_async(store).await
1621155d6dfSAlex Crichton         }
add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: a::b::interface_with_live_type::HostWithStore + a::b::interface_with_dead_type::HostWithStore, for<'a> D::Data< 'a, >: a::b::interface_with_live_type::Host + a::b::interface_with_dead_type::Host, T: 'static,163f6775a33SAlex Crichton         pub fn add_to_linker<T, D>(
164eea68f59SLann             linker: &mut wasmtime::component::Linker<T>,
1657c790607SAlex Crichton             host_getter: fn(&mut T) -> D::Data<'_>,
166eea68f59SLann         ) -> wasmtime::Result<()>
167eea68f59SLann         where
1681155d6dfSAlex Crichton             D: a::b::interface_with_live_type::HostWithStore
1691155d6dfSAlex Crichton                 + a::b::interface_with_dead_type::HostWithStore,
170f6775a33SAlex Crichton             for<'a> D::Data<
171f6775a33SAlex Crichton                 'a,
172f6775a33SAlex Crichton             >: a::b::interface_with_live_type::Host
173eea68f59SLann                 + a::b::interface_with_dead_type::Host,
174f6775a33SAlex Crichton             T: 'static,
175eea68f59SLann         {
1767c790607SAlex Crichton             a::b::interface_with_live_type::add_to_linker::<T, D>(linker, host_getter)?;
1777c790607SAlex Crichton             a::b::interface_with_dead_type::add_to_linker::<T, D>(linker, host_getter)?;
178eea68f59SLann             Ok(())
179eea68f59SLann         }
180eea68f59SLann     }
181eea68f59SLann };
182eea68f59SLann pub mod a {
183eea68f59SLann     pub mod b {
184eea68f59SLann         #[allow(clippy::all)]
185eea68f59SLann         pub mod interface_with_live_type {
186eea68f59SLann             #[allow(unused_imports)]
187*96e19700SNick Fitzgerald             use wasmtime::component::__internal::Box;
188eea68f59SLann             #[derive(wasmtime::component::ComponentType)]
189eea68f59SLann             #[derive(wasmtime::component::Lift)]
190eea68f59SLann             #[derive(wasmtime::component::Lower)]
191eea68f59SLann             #[component(record)]
192eea68f59SLann             #[derive(Clone, Copy)]
193eea68f59SLann             pub struct LiveType {
194eea68f59SLann                 #[component(name = "a")]
195eea68f59SLann                 pub a: u32,
196eea68f59SLann             }
197eea68f59SLann             impl core::fmt::Debug for LiveType {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result198eea68f59SLann                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
199eea68f59SLann                     f.debug_struct("LiveType").field("a", &self.a).finish()
200eea68f59SLann                 }
201eea68f59SLann             }
202eea68f59SLann             const _: () = {
203eea68f59SLann                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
204eea68f59SLann                 assert!(
205eea68f59SLann                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
206eea68f59SLann                 );
207eea68f59SLann             };
2081155d6dfSAlex Crichton             pub trait HostWithStore: wasmtime::component::HasData {}
2091155d6dfSAlex Crichton             impl<_T: ?Sized> HostWithStore for _T
2101155d6dfSAlex Crichton             where
2111155d6dfSAlex Crichton                 _T: wasmtime::component::HasData,
2121155d6dfSAlex Crichton             {}
213eea68f59SLann             pub trait Host {
f(&mut self) -> LiveType214eea68f59SLann                 fn f(&mut self) -> LiveType;
215eea68f59SLann             }
2167c790607SAlex Crichton             impl<_T: Host + ?Sized> Host for &mut _T {
f(&mut self) -> LiveType2177c790607SAlex Crichton                 fn f(&mut self) -> LiveType {
2187c790607SAlex Crichton                     Host::f(*self)
2197c790607SAlex Crichton                 }
2207c790607SAlex Crichton             }
add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static,221f6775a33SAlex Crichton             pub fn add_to_linker<T, D>(
2223cd96e17SLann                 linker: &mut wasmtime::component::Linker<T>,
223f6775a33SAlex Crichton                 host_getter: fn(&mut T) -> D::Data<'_>,
22429d04b15SAlex Crichton             ) -> wasmtime::Result<()>
22529d04b15SAlex Crichton             where
2261155d6dfSAlex Crichton                 D: HostWithStore,
227f6775a33SAlex Crichton                 for<'a> D::Data<'a>: Host,
228f81c0dc0SAlex Crichton                 T: 'static,
22929d04b15SAlex Crichton             {
2303cd96e17SLann                 let mut inst = linker.instance("a:b/interface-with-live-type")?;
2313cd96e17SLann                 inst.func_wrap(
2323cd96e17SLann                     "f",
2333cd96e17SLann                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
2343cd96e17SLann                         let host = &mut host_getter(caller.data_mut());
2353cd96e17SLann                         let r = Host::f(host);
2363cd96e17SLann                         Ok((r,))
2373cd96e17SLann                     },
2383cd96e17SLann                 )?;
2393cd96e17SLann                 Ok(())
2403cd96e17SLann             }
241eea68f59SLann         }
242eea68f59SLann         #[allow(clippy::all)]
243eea68f59SLann         pub mod interface_with_dead_type {
244eea68f59SLann             #[allow(unused_imports)]
245*96e19700SNick Fitzgerald             use wasmtime::component::__internal::Box;
246af31e80dSPat Hickey             pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType;
247af31e80dSPat Hickey             const _: () = {
248af31e80dSPat Hickey                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
249af31e80dSPat Hickey                 assert!(
250af31e80dSPat Hickey                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
251af31e80dSPat Hickey                 );
252af31e80dSPat Hickey             };
253af31e80dSPat Hickey             #[derive(wasmtime::component::ComponentType)]
254af31e80dSPat Hickey             #[derive(wasmtime::component::Lift)]
255af31e80dSPat Hickey             #[derive(wasmtime::component::Lower)]
256af31e80dSPat Hickey             #[component(record)]
257af31e80dSPat Hickey             #[derive(Clone, Copy)]
258af31e80dSPat Hickey             pub struct DeadType {
259af31e80dSPat Hickey                 #[component(name = "a")]
260af31e80dSPat Hickey                 pub a: u32,
261af31e80dSPat Hickey             }
262af31e80dSPat Hickey             impl core::fmt::Debug for DeadType {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result263af31e80dSPat Hickey                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
264af31e80dSPat Hickey                     f.debug_struct("DeadType").field("a", &self.a).finish()
265af31e80dSPat Hickey                 }
266af31e80dSPat Hickey             }
267af31e80dSPat Hickey             const _: () = {
268af31e80dSPat Hickey                 assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32);
269af31e80dSPat Hickey                 assert!(
270af31e80dSPat Hickey                     4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32
271af31e80dSPat Hickey                 );
272af31e80dSPat Hickey             };
273af31e80dSPat Hickey             #[derive(wasmtime::component::ComponentType)]
274af31e80dSPat Hickey             #[derive(wasmtime::component::Lift)]
275af31e80dSPat Hickey             #[derive(wasmtime::component::Lower)]
276af31e80dSPat Hickey             #[component(variant)]
277af31e80dSPat Hickey             #[derive(Clone, Copy)]
278af31e80dSPat Hickey             pub enum V {
279af31e80dSPat Hickey                 #[component(name = "a")]
280af31e80dSPat Hickey                 A(LiveType),
281af31e80dSPat Hickey                 #[component(name = "b")]
282af31e80dSPat Hickey                 B(DeadType),
283af31e80dSPat Hickey             }
284af31e80dSPat Hickey             impl core::fmt::Debug for V {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result285af31e80dSPat Hickey                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
286af31e80dSPat Hickey                     match self {
287af31e80dSPat Hickey                         V::A(e) => f.debug_tuple("V::A").field(e).finish(),
288af31e80dSPat Hickey                         V::B(e) => f.debug_tuple("V::B").field(e).finish(),
289af31e80dSPat Hickey                     }
290af31e80dSPat Hickey                 }
291af31e80dSPat Hickey             }
292af31e80dSPat Hickey             const _: () = {
293af31e80dSPat Hickey                 assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32);
294af31e80dSPat Hickey                 assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32);
295af31e80dSPat Hickey             };
2961155d6dfSAlex Crichton             pub trait HostWithStore: wasmtime::component::HasData {}
2971155d6dfSAlex Crichton             impl<_T: ?Sized> HostWithStore for _T
2981155d6dfSAlex Crichton             where
2991155d6dfSAlex Crichton                 _T: wasmtime::component::HasData,
3001155d6dfSAlex Crichton             {}
301eea68f59SLann             pub trait Host {}
3027c790607SAlex Crichton             impl<_T: Host + ?Sized> Host for &mut _T {}
add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static,303f6775a33SAlex Crichton             pub fn add_to_linker<T, D>(
3043cd96e17SLann                 linker: &mut wasmtime::component::Linker<T>,
305f6775a33SAlex Crichton                 host_getter: fn(&mut T) -> D::Data<'_>,
30629d04b15SAlex Crichton             ) -> wasmtime::Result<()>
30729d04b15SAlex Crichton             where
3081155d6dfSAlex Crichton                 D: HostWithStore,
309f6775a33SAlex Crichton                 for<'a> D::Data<'a>: Host,
310f81c0dc0SAlex Crichton                 T: 'static,
31129d04b15SAlex Crichton             {
3123cd96e17SLann                 let mut inst = linker.instance("a:b/interface-with-dead-type")?;
3133cd96e17SLann                 Ok(())
3143cd96e17SLann             }
315eea68f59SLann         }
316eea68f59SLann     }
317eea68f59SLann }
318