1 /// Auto-generated bindings for a pre-instantiated version of a
2 /// component which implements the world `the-world`.
3 ///
4 /// This structure is created through [`TheWorldPre::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 [`TheWorld`] as well.
9 pub struct TheWorldPre<T> {
10     instance_pre: wasmtime::component::InstancePre<T>,
11     indices: TheWorldIndices,
12 }
13 impl<T> Clone for TheWorldPre<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> TheWorldPre<_T> {
22     /// Creates a new copy of `TheWorldPre` 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 = TheWorldIndices::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 [`TheWorld`] 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<TheWorld>
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 /// `the-world`.
60 ///
61 /// This is an implementation detail of [`TheWorldPre`] and can
62 /// be constructed if needed as well.
63 ///
64 /// For more information see [`TheWorld`] as well.
65 #[derive(Clone)]
66 pub struct TheWorldIndices {
67     interface0: exports::foo::foo::strings::GuestIndices,
68 }
69 /// Auto-generated bindings for an instance a component which
70 /// implements the world `the-world`.
71 ///
72 /// This structure can be created through a number of means
73 /// depending on your requirements and what you have on hand:
74 ///
75 /// * The most convenient way is to use
76 ///   [`TheWorld::instantiate_async`] which only needs a
77 ///   [`Store`], [`Component`], and [`Linker`].
78 ///
79 /// * Alternatively you can create a [`TheWorldPre`] ahead of
80 ///   time with a [`Component`] to front-load string lookups
81 ///   of exports once instead of per-instantiation. This
82 ///   method then uses [`TheWorldPre::instantiate_async`] to
83 ///   create a [`TheWorld`].
84 ///
85 /// * If you've instantiated the instance yourself already
86 ///   then you can use [`TheWorld::new`].
87 ///
88 /// These methods are all equivalent to one another and move
89 /// around the tradeoff of what work is performed when.
90 ///
91 /// [`Store`]: wasmtime::Store
92 /// [`Component`]: wasmtime::component::Component
93 /// [`Linker`]: wasmtime::component::Linker
94 pub struct TheWorld {
95     interface0: exports::foo::foo::strings::Guest,
96 }
97 const _: () = {
98     #[allow(unused_imports)]
99     use wasmtime::component::__internal::anyhow;
100     impl TheWorldIndices {
101         /// Creates a new copy of `TheWorldIndices` bindings which can then
102         /// be used to instantiate into a particular store.
103         ///
104         /// This method may fail if the component does not have the
105         /// required exports.
106         pub fn new<_T>(
107             _instance_pre: &wasmtime::component::InstancePre<_T>,
108         ) -> wasmtime::Result<Self> {
109             let _component = _instance_pre.component();
110             let _instance_type = _instance_pre.instance_type();
111             let interface0 = exports::foo::foo::strings::GuestIndices::new(
112                 _instance_pre,
113             )?;
114             Ok(TheWorldIndices { interface0 })
115         }
116         /// Uses the indices stored in `self` to load an instance
117         /// of [`TheWorld`] from the instance provided.
118         ///
119         /// Note that at this time this method will additionally
120         /// perform type-checks of all exports.
121         pub fn load(
122             &self,
123             mut store: impl wasmtime::AsContextMut,
124             instance: &wasmtime::component::Instance,
125         ) -> wasmtime::Result<TheWorld> {
126             let _ = &mut store;
127             let _instance = instance;
128             let interface0 = self.interface0.load(&mut store, &_instance)?;
129             Ok(TheWorld { interface0 })
130         }
131     }
132     impl TheWorld {
133         /// Convenience wrapper around [`TheWorldPre::new`] and
134         /// [`TheWorldPre::instantiate_async`].
135         pub async fn instantiate_async<_T>(
136             store: impl wasmtime::AsContextMut<Data = _T>,
137             component: &wasmtime::component::Component,
138             linker: &wasmtime::component::Linker<_T>,
139         ) -> wasmtime::Result<TheWorld>
140         where
141             _T: Send + 'static,
142         {
143             let pre = linker.instantiate_pre(component)?;
144             TheWorldPre::new(pre)?.instantiate_async(store).await
145         }
146         /// Convenience wrapper around [`TheWorldIndices::new`] and
147         /// [`TheWorldIndices::load`].
148         pub fn new(
149             mut store: impl wasmtime::AsContextMut,
150             instance: &wasmtime::component::Instance,
151         ) -> wasmtime::Result<TheWorld> {
152             let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;
153             indices.load(&mut store, instance)
154         }
155         pub fn add_to_linker<T, U>(
156             linker: &mut wasmtime::component::Linker<T>,
157             get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
158         ) -> wasmtime::Result<()>
159         where
160             T: Send + foo::foo::strings::Host<Data = T> + 'static,
161             U: Send + foo::foo::strings::Host<Data = T>,
162         {
163             foo::foo::strings::add_to_linker(linker, get)?;
164             Ok(())
165         }
166         pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest {
167             &self.interface0
168         }
169     }
170 };
171 pub mod foo {
172     pub mod foo {
173         #[allow(clippy::all)]
174         pub mod strings {
175             #[allow(unused_imports)]
176             use wasmtime::component::__internal::{anyhow, Box};
177             pub trait Host {
178                 type Data;
179                 fn a(
180                     store: wasmtime::StoreContextMut<'_, Self::Data>,
181                     x: wasmtime::component::__internal::String,
182                 ) -> impl ::core::future::Future<
183                     Output = impl FnOnce(
184                         wasmtime::StoreContextMut<'_, Self::Data>,
185                     ) -> () + Send + Sync + 'static,
186                 > + Send + Sync + 'static
187                 where
188                     Self: Sized;
189                 fn b(
190                     store: wasmtime::StoreContextMut<'_, Self::Data>,
191                 ) -> impl ::core::future::Future<
192                     Output = impl FnOnce(
193                         wasmtime::StoreContextMut<'_, Self::Data>,
194                     ) -> wasmtime::component::__internal::String + Send + Sync + 'static,
195                 > + Send + Sync + 'static
196                 where
197                     Self: Sized;
198                 fn c(
199                     store: wasmtime::StoreContextMut<'_, Self::Data>,
200                     a: wasmtime::component::__internal::String,
201                     b: wasmtime::component::__internal::String,
202                 ) -> impl ::core::future::Future<
203                     Output = impl FnOnce(
204                         wasmtime::StoreContextMut<'_, Self::Data>,
205                     ) -> wasmtime::component::__internal::String + Send + Sync + 'static,
206                 > + Send + Sync + 'static
207                 where
208                     Self: Sized;
209             }
210             pub trait GetHost<
211                 T,
212                 D,
213             >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static {
214                 type Host: Host<Data = D> + Send;
215             }
216             impl<F, T, D, O> GetHost<T, D> for F
217             where
218                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
219                 O: Host<Data = D> + Send,
220             {
221                 type Host = O;
222             }
223             pub fn add_to_linker_get_host<
224                 T,
225                 G: for<'a> GetHost<&'a mut T, T, Host: Host<Data = T> + Send>,
226             >(
227                 linker: &mut wasmtime::component::Linker<T>,
228                 host_getter: G,
229             ) -> wasmtime::Result<()>
230             where
231                 T: Send + 'static,
232             {
233                 let mut inst = linker.instance("foo:foo/strings")?;
234                 inst.func_wrap_concurrent(
235                     "a",
236                     move |
237                         mut caller: wasmtime::StoreContextMut<'_, T>,
238                         (arg0,): (wasmtime::component::__internal::String,)|
239                     {
240                         let host = caller;
241                         let r = <G::Host as Host>::a(host, arg0);
242                         Box::pin(async move {
243                             let fun = r.await;
244                             Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| {
245                                 let r = fun(caller);
246                                 Ok(r)
247                             })
248                                 as Box<
249                                     dyn FnOnce(
250                                         wasmtime::StoreContextMut<'_, T>,
251                                     ) -> wasmtime::Result<()> + Send + Sync,
252                                 >
253                         })
254                             as ::core::pin::Pin<
255                                 Box<
256                                     dyn ::core::future::Future<
257                                         Output = Box<
258                                             dyn FnOnce(
259                                                 wasmtime::StoreContextMut<'_, T>,
260                                             ) -> wasmtime::Result<()> + Send + Sync,
261                                         >,
262                                     > + Send + Sync + 'static,
263                                 >,
264                             >
265                     },
266                 )?;
267                 inst.func_wrap_concurrent(
268                     "b",
269                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
270                         let host = caller;
271                         let r = <G::Host as Host>::b(host);
272                         Box::pin(async move {
273                             let fun = r.await;
274                             Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| {
275                                 let r = fun(caller);
276                                 Ok((r,))
277                             })
278                                 as Box<
279                                     dyn FnOnce(
280                                         wasmtime::StoreContextMut<'_, T>,
281                                     ) -> wasmtime::Result<
282                                             (wasmtime::component::__internal::String,),
283                                         > + Send + Sync,
284                                 >
285                         })
286                             as ::core::pin::Pin<
287                                 Box<
288                                     dyn ::core::future::Future<
289                                         Output = Box<
290                                             dyn FnOnce(
291                                                 wasmtime::StoreContextMut<'_, T>,
292                                             ) -> wasmtime::Result<
293                                                     (wasmtime::component::__internal::String,),
294                                                 > + Send + Sync,
295                                         >,
296                                     > + Send + Sync + 'static,
297                                 >,
298                             >
299                     },
300                 )?;
301                 inst.func_wrap_concurrent(
302                     "c",
303                     move |
304                         mut caller: wasmtime::StoreContextMut<'_, T>,
305                         (
306                             arg0,
307                             arg1,
308                         ): (
309                             wasmtime::component::__internal::String,
310                             wasmtime::component::__internal::String,
311                         )|
312                     {
313                         let host = caller;
314                         let r = <G::Host as Host>::c(host, arg0, arg1);
315                         Box::pin(async move {
316                             let fun = r.await;
317                             Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| {
318                                 let r = fun(caller);
319                                 Ok((r,))
320                             })
321                                 as Box<
322                                     dyn FnOnce(
323                                         wasmtime::StoreContextMut<'_, T>,
324                                     ) -> wasmtime::Result<
325                                             (wasmtime::component::__internal::String,),
326                                         > + Send + Sync,
327                                 >
328                         })
329                             as ::core::pin::Pin<
330                                 Box<
331                                     dyn ::core::future::Future<
332                                         Output = Box<
333                                             dyn FnOnce(
334                                                 wasmtime::StoreContextMut<'_, T>,
335                                             ) -> wasmtime::Result<
336                                                     (wasmtime::component::__internal::String,),
337                                                 > + Send + Sync,
338                                         >,
339                                     > + Send + Sync + 'static,
340                                 >,
341                             >
342                     },
343                 )?;
344                 Ok(())
345             }
346             pub fn add_to_linker<T, U>(
347                 linker: &mut wasmtime::component::Linker<T>,
348                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
349             ) -> wasmtime::Result<()>
350             where
351                 U: Host<Data = T> + Send,
352                 T: Send + 'static,
353             {
354                 add_to_linker_get_host(linker, get)
355             }
356             impl<_T: Host> Host for &mut _T {
357                 type Data = _T::Data;
358                 fn a(
359                     store: wasmtime::StoreContextMut<'_, Self::Data>,
360                     x: wasmtime::component::__internal::String,
361                 ) -> impl ::core::future::Future<
362                     Output = impl FnOnce(
363                         wasmtime::StoreContextMut<'_, Self::Data>,
364                     ) -> () + Send + Sync + 'static,
365                 > + Send + Sync + 'static
366                 where
367                     Self: Sized,
368                 {
369                     <_T as Host>::a(store, x)
370                 }
371                 fn b(
372                     store: wasmtime::StoreContextMut<'_, Self::Data>,
373                 ) -> impl ::core::future::Future<
374                     Output = impl FnOnce(
375                         wasmtime::StoreContextMut<'_, Self::Data>,
376                     ) -> wasmtime::component::__internal::String + Send + Sync + 'static,
377                 > + Send + Sync + 'static
378                 where
379                     Self: Sized,
380                 {
381                     <_T as Host>::b(store)
382                 }
383                 fn c(
384                     store: wasmtime::StoreContextMut<'_, Self::Data>,
385                     a: wasmtime::component::__internal::String,
386                     b: wasmtime::component::__internal::String,
387                 ) -> impl ::core::future::Future<
388                     Output = impl FnOnce(
389                         wasmtime::StoreContextMut<'_, Self::Data>,
390                     ) -> wasmtime::component::__internal::String + Send + Sync + 'static,
391                 > + Send + Sync + 'static
392                 where
393                     Self: Sized,
394                 {
395                     <_T as Host>::c(store, a, b)
396                 }
397             }
398         }
399     }
400 }
401 pub mod exports {
402     pub mod foo {
403         pub mod foo {
404             #[allow(clippy::all)]
405             pub mod strings {
406                 #[allow(unused_imports)]
407                 use wasmtime::component::__internal::{anyhow, Box};
408                 pub struct Guest {
409                     a: wasmtime::component::Func,
410                     b: wasmtime::component::Func,
411                     c: wasmtime::component::Func,
412                 }
413                 #[derive(Clone)]
414                 pub struct GuestIndices {
415                     a: wasmtime::component::ComponentExportIndex,
416                     b: wasmtime::component::ComponentExportIndex,
417                     c: wasmtime::component::ComponentExportIndex,
418                 }
419                 impl GuestIndices {
420                     /// Constructor for [`GuestIndices`] which takes a
421                     /// [`Component`](wasmtime::component::Component) as input and can be executed
422                     /// before instantiation.
423                     ///
424                     /// This constructor can be used to front-load string lookups to find exports
425                     /// within a component.
426                     pub fn new<_T>(
427                         _instance_pre: &wasmtime::component::InstancePre<_T>,
428                     ) -> wasmtime::Result<GuestIndices> {
429                         let instance = _instance_pre
430                             .component()
431                             .get_export_index(None, "foo:foo/strings")
432                             .ok_or_else(|| {
433                                 anyhow::anyhow!(
434                                     "no exported instance named `foo:foo/strings`"
435                                 )
436                             })?;
437                         let mut lookup = move |name| {
438                             _instance_pre
439                                 .component()
440                                 .get_export_index(Some(&instance), name)
441                                 .ok_or_else(|| {
442                                     anyhow::anyhow!(
443                                         "instance export `foo:foo/strings` does \
444                 not have export `{name}`"
445                                     )
446                                 })
447                         };
448                         let _ = &mut lookup;
449                         let a = lookup("a")?;
450                         let b = lookup("b")?;
451                         let c = lookup("c")?;
452                         Ok(GuestIndices { a, b, c })
453                     }
454                     pub fn load(
455                         &self,
456                         mut store: impl wasmtime::AsContextMut,
457                         instance: &wasmtime::component::Instance,
458                     ) -> wasmtime::Result<Guest> {
459                         let _instance = instance;
460                         let _instance_pre = _instance.instance_pre(&store);
461                         let _instance_type = _instance_pre.instance_type();
462                         let mut store = store.as_context_mut();
463                         let _ = &mut store;
464                         let a = *_instance
465                             .get_typed_func::<(&str,), ()>(&mut store, &self.a)?
466                             .func();
467                         let b = *_instance
468                             .get_typed_func::<
469                                 (),
470                                 (wasmtime::component::__internal::String,),
471                             >(&mut store, &self.b)?
472                             .func();
473                         let c = *_instance
474                             .get_typed_func::<
475                                 (&str, &str),
476                                 (wasmtime::component::__internal::String,),
477                             >(&mut store, &self.c)?
478                             .func();
479                         Ok(Guest { a, b, c })
480                     }
481                 }
482                 impl Guest {
483                     pub async fn call_a<S: wasmtime::AsContextMut>(
484                         &self,
485                         mut store: S,
486                         arg0: wasmtime::component::__internal::String,
487                     ) -> wasmtime::Result<wasmtime::component::Promise<()>>
488                     where
489                         <S as wasmtime::AsContext>::Data: Send + 'static,
490                     {
491                         let callee = unsafe {
492                             wasmtime::component::TypedFunc::<
493                                 (wasmtime::component::__internal::String,),
494                                 (),
495                             >::new_unchecked(self.a)
496                         };
497                         let promise = callee
498                             .call_concurrent(store.as_context_mut(), (arg0,))
499                             .await?;
500                         Ok(promise)
501                     }
502                     pub async fn call_b<S: wasmtime::AsContextMut>(
503                         &self,
504                         mut store: S,
505                     ) -> wasmtime::Result<
506                         wasmtime::component::Promise<
507                             wasmtime::component::__internal::String,
508                         >,
509                     >
510                     where
511                         <S as wasmtime::AsContext>::Data: Send + 'static,
512                     {
513                         let callee = unsafe {
514                             wasmtime::component::TypedFunc::<
515                                 (),
516                                 (wasmtime::component::__internal::String,),
517                             >::new_unchecked(self.b)
518                         };
519                         let promise = callee
520                             .call_concurrent(store.as_context_mut(), ())
521                             .await?;
522                         Ok(promise.map(|(v,)| v))
523                     }
524                     pub async fn call_c<S: wasmtime::AsContextMut>(
525                         &self,
526                         mut store: S,
527                         arg0: wasmtime::component::__internal::String,
528                         arg1: wasmtime::component::__internal::String,
529                     ) -> wasmtime::Result<
530                         wasmtime::component::Promise<
531                             wasmtime::component::__internal::String,
532                         >,
533                     >
534                     where
535                         <S as wasmtime::AsContext>::Data: Send + 'static,
536                     {
537                         let callee = unsafe {
538                             wasmtime::component::TypedFunc::<
539                                 (
540                                     wasmtime::component::__internal::String,
541                                     wasmtime::component::__internal::String,
542                                 ),
543                                 (wasmtime::component::__internal::String,),
544                             >::new_unchecked(self.c)
545                         };
546                         let promise = callee
547                             .call_concurrent(store.as_context_mut(), (arg0, arg1))
548                             .await?;
549                         Ok(promise.map(|(v,)| v))
550                     }
551                 }
552             }
553         }
554     }
555 }
556