1 /// Auto-generated bindings for a pre-instantiated version of a
2 /// component which implements the world `imports`.
3 ///
4 /// This structure is created through [`ImportsPre::new`] which
5 /// takes a [`InstancePre`](wasmtime::component::InstancePre) that
6 /// has been created through a [`Linker`](wasmtime::component::Linker).
7 ///
8 /// For more information see [`Imports`] as well.
9 pub struct ImportsPre<T> {
10     instance_pre: wasmtime::component::InstancePre<T>,
11     indices: ImportsIndices,
12 }
13 impl<T> Clone for ImportsPre<T> {
14     fn clone(&self) -> Self {
15         Self {
16             instance_pre: self.instance_pre.clone(),
17             indices: self.indices.clone(),
18         }
19     }
20 }
21 impl<_T> ImportsPre<_T> {
22     /// Creates a new copy of `ImportsPre` bindings which can then
23     /// be used to instantiate into a particular store.
24     ///
25     /// This method may fail if the component behind `instance_pre`
26     /// does not have the required exports.
27     pub fn new(
28         instance_pre: wasmtime::component::InstancePre<_T>,
29     ) -> wasmtime::Result<Self> {
30         let indices = ImportsIndices::new(&instance_pre)?;
31         Ok(Self { instance_pre, indices })
32     }
33     pub fn engine(&self) -> &wasmtime::Engine {
34         self.instance_pre.engine()
35     }
36     pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {
37         &self.instance_pre
38     }
39     /// Instantiates a new instance of [`Imports`] within the
40     /// `store` provided.
41     ///
42     /// This function will use `self` as the pre-instantiated
43     /// instance to perform instantiation. Afterwards the preloaded
44     /// indices in `self` are used to lookup all exports on the
45     /// resulting instance.
46     pub async fn instantiate_async(
47         &self,
48         mut store: impl wasmtime::AsContextMut<Data = _T>,
49     ) -> wasmtime::Result<Imports>
50     where
51         _T: Send + 'static,
52     {
53         let mut store = store.as_context_mut();
54         let instance = self.instance_pre.instantiate_async(&mut store).await?;
55         self.indices.load(&mut store, &instance)
56     }
57 }
58 /// Auto-generated bindings for index of the exports of
59 /// `imports`.
60 ///
61 /// This is an implementation detail of [`ImportsPre`] and can
62 /// be constructed if needed as well.
63 ///
64 /// For more information see [`Imports`] as well.
65 #[derive(Clone)]
66 pub struct ImportsIndices {}
67 /// Auto-generated bindings for an instance a component which
68 /// implements the world `imports`.
69 ///
70 /// This structure can be created through a number of means
71 /// depending on your requirements and what you have on hand:
72 ///
73 /// * The most convenient way is to use
74 ///   [`Imports::instantiate_async`] which only needs a
75 ///   [`Store`], [`Component`], and [`Linker`].
76 ///
77 /// * Alternatively you can create a [`ImportsPre`] ahead of
78 ///   time with a [`Component`] to front-load string lookups
79 ///   of exports once instead of per-instantiation. This
80 ///   method then uses [`ImportsPre::instantiate_async`] to
81 ///   create a [`Imports`].
82 ///
83 /// * If you've instantiated the instance yourself already
84 ///   then you can use [`Imports::new`].
85 ///
86 /// These methods are all equivalent to one another and move
87 /// around the tradeoff of what work is performed when.
88 ///
89 /// [`Store`]: wasmtime::Store
90 /// [`Component`]: wasmtime::component::Component
91 /// [`Linker`]: wasmtime::component::Linker
92 pub struct Imports {}
93 const _: () = {
94     #[allow(unused_imports)]
95     use wasmtime::component::__internal::anyhow;
96     impl ImportsIndices {
97         /// Creates a new copy of `ImportsIndices` bindings which can then
98         /// be used to instantiate into a particular store.
99         ///
100         /// This method may fail if the component does not have the
101         /// required exports.
102         pub fn new<_T>(
103             _instance_pre: &wasmtime::component::InstancePre<_T>,
104         ) -> wasmtime::Result<Self> {
105             let _component = _instance_pre.component();
106             let _instance_type = _instance_pre.instance_type();
107             Ok(ImportsIndices {})
108         }
109         /// Uses the indices stored in `self` to load an instance
110         /// of [`Imports`] from the instance provided.
111         ///
112         /// Note that at this time this method will additionally
113         /// perform type-checks of all exports.
114         pub fn load(
115             &self,
116             mut store: impl wasmtime::AsContextMut,
117             instance: &wasmtime::component::Instance,
118         ) -> wasmtime::Result<Imports> {
119             let _ = &mut store;
120             let _instance = instance;
121             Ok(Imports {})
122         }
123     }
124     impl Imports {
125         /// Convenience wrapper around [`ImportsPre::new`] and
126         /// [`ImportsPre::instantiate_async`].
127         pub async fn instantiate_async<_T>(
128             store: impl wasmtime::AsContextMut<Data = _T>,
129             component: &wasmtime::component::Component,
130             linker: &wasmtime::component::Linker<_T>,
131         ) -> wasmtime::Result<Imports>
132         where
133             _T: Send + 'static,
134         {
135             let pre = linker.instantiate_pre(component)?;
136             ImportsPre::new(pre)?.instantiate_async(store).await
137         }
138         /// Convenience wrapper around [`ImportsIndices::new`] and
139         /// [`ImportsIndices::load`].
140         pub fn new(
141             mut store: impl wasmtime::AsContextMut,
142             instance: &wasmtime::component::Instance,
143         ) -> wasmtime::Result<Imports> {
144             let indices = ImportsIndices::new(&instance.instance_pre(&store))?;
145             indices.load(&mut store, instance)
146         }
147         pub fn add_to_linker<T, U>(
148             linker: &mut wasmtime::component::Linker<T>,
149             get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
150         ) -> wasmtime::Result<()>
151         where
152             T: Send + a::b::interface_with_live_type::Host<Data = T>
153                 + a::b::interface_with_dead_type::Host + 'static,
154             U: Send + a::b::interface_with_live_type::Host<Data = T>
155                 + a::b::interface_with_dead_type::Host,
156         {
157             a::b::interface_with_live_type::add_to_linker(linker, get)?;
158             a::b::interface_with_dead_type::add_to_linker(linker, get)?;
159             Ok(())
160         }
161     }
162 };
163 pub mod a {
164     pub mod b {
165         #[allow(clippy::all)]
166         pub mod interface_with_live_type {
167             #[allow(unused_imports)]
168             use wasmtime::component::__internal::{anyhow, Box};
169             #[derive(wasmtime::component::ComponentType)]
170             #[derive(wasmtime::component::Lift)]
171             #[derive(wasmtime::component::Lower)]
172             #[component(record)]
173             #[derive(Clone, Copy)]
174             pub struct LiveType {
175                 #[component(name = "a")]
176                 pub a: u32,
177             }
178             impl core::fmt::Debug for LiveType {
179                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
180                     f.debug_struct("LiveType").field("a", &self.a).finish()
181                 }
182             }
183             const _: () = {
184                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
185                 assert!(
186                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
187                 );
188             };
189             pub trait Host {
190                 type Data;
191                 fn f(
192                     store: wasmtime::StoreContextMut<'_, Self::Data>,
193                 ) -> impl ::core::future::Future<
194                     Output = impl FnOnce(
195                         wasmtime::StoreContextMut<'_, Self::Data>,
196                     ) -> LiveType + Send + Sync + 'static,
197                 > + Send + Sync + 'static
198                 where
199                     Self: Sized;
200             }
201             pub trait GetHost<
202                 T,
203                 D,
204             >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static {
205                 type Host: Host<Data = D> + Send;
206             }
207             impl<F, T, D, O> GetHost<T, D> for F
208             where
209                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
210                 O: Host<Data = D> + Send,
211             {
212                 type Host = O;
213             }
214             pub fn add_to_linker_get_host<
215                 T,
216                 G: for<'a> GetHost<&'a mut T, T, Host: Host<Data = T> + Send>,
217             >(
218                 linker: &mut wasmtime::component::Linker<T>,
219                 host_getter: G,
220             ) -> wasmtime::Result<()>
221             where
222                 T: Send + 'static,
223             {
224                 let mut inst = linker.instance("a:b/interface-with-live-type")?;
225                 inst.func_wrap_concurrent(
226                     "f",
227                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
228                         let host = caller;
229                         let r = <G::Host as Host>::f(host);
230                         Box::pin(async move {
231                             let fun = r.await;
232                             Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| {
233                                 let r = fun(caller);
234                                 Ok((r,))
235                             })
236                                 as Box<
237                                     dyn FnOnce(
238                                         wasmtime::StoreContextMut<'_, T>,
239                                     ) -> wasmtime::Result<(LiveType,)> + Send + Sync,
240                                 >
241                         })
242                             as ::core::pin::Pin<
243                                 Box<
244                                     dyn ::core::future::Future<
245                                         Output = Box<
246                                             dyn FnOnce(
247                                                 wasmtime::StoreContextMut<'_, T>,
248                                             ) -> wasmtime::Result<(LiveType,)> + Send + Sync,
249                                         >,
250                                     > + Send + Sync + 'static,
251                                 >,
252                             >
253                     },
254                 )?;
255                 Ok(())
256             }
257             pub fn add_to_linker<T, U>(
258                 linker: &mut wasmtime::component::Linker<T>,
259                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
260             ) -> wasmtime::Result<()>
261             where
262                 U: Host<Data = T> + Send,
263                 T: Send + 'static,
264             {
265                 add_to_linker_get_host(linker, get)
266             }
267             impl<_T: Host> Host for &mut _T {
268                 type Data = _T::Data;
269                 fn f(
270                     store: wasmtime::StoreContextMut<'_, Self::Data>,
271                 ) -> impl ::core::future::Future<
272                     Output = impl FnOnce(
273                         wasmtime::StoreContextMut<'_, Self::Data>,
274                     ) -> LiveType + Send + Sync + 'static,
275                 > + Send + Sync + 'static
276                 where
277                     Self: Sized,
278                 {
279                     <_T as Host>::f(store)
280                 }
281             }
282         }
283         #[allow(clippy::all)]
284         pub mod interface_with_dead_type {
285             #[allow(unused_imports)]
286             use wasmtime::component::__internal::{anyhow, Box};
287             pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType;
288             const _: () = {
289                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
290                 assert!(
291                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
292                 );
293             };
294             #[derive(wasmtime::component::ComponentType)]
295             #[derive(wasmtime::component::Lift)]
296             #[derive(wasmtime::component::Lower)]
297             #[component(record)]
298             #[derive(Clone, Copy)]
299             pub struct DeadType {
300                 #[component(name = "a")]
301                 pub a: u32,
302             }
303             impl core::fmt::Debug for DeadType {
304                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
305                     f.debug_struct("DeadType").field("a", &self.a).finish()
306                 }
307             }
308             const _: () = {
309                 assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32);
310                 assert!(
311                     4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32
312                 );
313             };
314             #[derive(wasmtime::component::ComponentType)]
315             #[derive(wasmtime::component::Lift)]
316             #[derive(wasmtime::component::Lower)]
317             #[component(variant)]
318             #[derive(Clone, Copy)]
319             pub enum V {
320                 #[component(name = "a")]
321                 A(LiveType),
322                 #[component(name = "b")]
323                 B(DeadType),
324             }
325             impl core::fmt::Debug for V {
326                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
327                     match self {
328                         V::A(e) => f.debug_tuple("V::A").field(e).finish(),
329                         V::B(e) => f.debug_tuple("V::B").field(e).finish(),
330                     }
331                 }
332             }
333             const _: () = {
334                 assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32);
335                 assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32);
336             };
337             pub trait Host {}
338             pub trait GetHost<
339                 T,
340                 D,
341             >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static {
342                 type Host: Host + Send;
343             }
344             impl<F, T, D, O> GetHost<T, D> for F
345             where
346                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
347                 O: Host + Send,
348             {
349                 type Host = O;
350             }
351             pub fn add_to_linker_get_host<
352                 T,
353                 G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>,
354             >(
355                 linker: &mut wasmtime::component::Linker<T>,
356                 host_getter: G,
357             ) -> wasmtime::Result<()>
358             where
359                 T: Send + 'static,
360             {
361                 let mut inst = linker.instance("a:b/interface-with-dead-type")?;
362                 Ok(())
363             }
364             pub fn add_to_linker<T, U>(
365                 linker: &mut wasmtime::component::Linker<T>,
366                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
367             ) -> wasmtime::Result<()>
368             where
369                 U: Host + Send,
370                 T: Send + 'static,
371             {
372                 add_to_linker_get_host(linker, get)
373             }
374             impl<_T: Host + ?Sized> Host for &mut _T {}
375         }
376     }
377 }
378