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