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::strings::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::strings::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::strings::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::strings::HostWithStore + Send,
179             for<'a> D::Data<'a>: foo::foo::strings::Host + Send,
180             T: 'static + Send,
181         {
182             foo::foo::strings::add_to_linker::<T, D>(linker, host_getter)?;
183             Ok(())
184         }
185         pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest {
186             &self.interface0
187         }
188     }
189 };
190 pub mod foo {
191     pub mod foo {
192         #[allow(clippy::all)]
193         pub mod strings {
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 a(
203                     &mut self,
204                     x: wasmtime::component::__internal::String,
205                 ) -> impl ::core::future::Future<Output = ()> + Send;
206                 fn b(
207                     &mut self,
208                 ) -> impl ::core::future::Future<
209                     Output = wasmtime::component::__internal::String,
210                 > + Send;
211                 fn c(
212                     &mut self,
213                     a: wasmtime::component::__internal::String,
214                     b: wasmtime::component::__internal::String,
215                 ) -> impl ::core::future::Future<
216                     Output = wasmtime::component::__internal::String,
217                 > + Send;
218             }
219             impl<_T: Host + ?Sized + Send> Host for &mut _T {
220                 fn a(
221                     &mut self,
222                     x: wasmtime::component::__internal::String,
223                 ) -> impl ::core::future::Future<Output = ()> + Send {
224                     async move { Host::a(*self, x).await }
225                 }
226                 fn b(
227                     &mut self,
228                 ) -> impl ::core::future::Future<
229                     Output = wasmtime::component::__internal::String,
230                 > + Send {
231                     async move { Host::b(*self).await }
232                 }
233                 fn c(
234                     &mut self,
235                     a: wasmtime::component::__internal::String,
236                     b: wasmtime::component::__internal::String,
237                 ) -> impl ::core::future::Future<
238                     Output = wasmtime::component::__internal::String,
239                 > + Send {
240                     async move { Host::c(*self, a, b).await }
241                 }
242             }
243             pub fn add_to_linker<T, D>(
244                 linker: &mut wasmtime::component::Linker<T>,
245                 host_getter: fn(&mut T) -> D::Data<'_>,
246             ) -> wasmtime::Result<()>
247             where
248                 D: HostWithStore,
249                 for<'a> D::Data<'a>: Host,
250                 T: 'static + Send,
251             {
252                 let mut inst = linker.instance("foo:foo/strings")?;
253                 inst.func_wrap_async(
254                     "a",
255                     move |
256                         mut caller: wasmtime::StoreContextMut<'_, T>,
257                         (arg0,): (wasmtime::component::__internal::String,)|
258                     {
259                         use tracing::Instrument;
260                         let span = tracing::span!(
261                             tracing::Level::TRACE, "wit-bindgen import", module =
262                             "strings", function = "a",
263                         );
264                         wasmtime::component::__internal::Box::new(
265                             async move {
266                                 tracing::event!(
267                                     tracing::Level::TRACE, x = tracing::field::debug(& arg0),
268                                     "call"
269                                 );
270                                 let host = &mut host_getter(caller.data_mut());
271                                 let r = Host::a(host, arg0).await;
272                                 tracing::event!(
273                                     tracing::Level::TRACE, result = tracing::field::debug(& r),
274                                     "return"
275                                 );
276                                 Ok(r)
277                             }
278                                 .instrument(span),
279                         )
280                     },
281                 )?;
282                 inst.func_wrap_async(
283                     "b",
284                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
285                         use tracing::Instrument;
286                         let span = tracing::span!(
287                             tracing::Level::TRACE, "wit-bindgen import", module =
288                             "strings", function = "b",
289                         );
290                         wasmtime::component::__internal::Box::new(
291                             async move {
292                                 tracing::event!(tracing::Level::TRACE, "call");
293                                 let host = &mut host_getter(caller.data_mut());
294                                 let r = Host::b(host).await;
295                                 tracing::event!(
296                                     tracing::Level::TRACE, result = tracing::field::debug(& r),
297                                     "return"
298                                 );
299                                 Ok((r,))
300                             }
301                                 .instrument(span),
302                         )
303                     },
304                 )?;
305                 inst.func_wrap_async(
306                     "c",
307                     move |
308                         mut caller: wasmtime::StoreContextMut<'_, T>,
309                         (
310                             arg0,
311                             arg1,
312                         ): (
313                             wasmtime::component::__internal::String,
314                             wasmtime::component::__internal::String,
315                         )|
316                     {
317                         use tracing::Instrument;
318                         let span = tracing::span!(
319                             tracing::Level::TRACE, "wit-bindgen import", module =
320                             "strings", function = "c",
321                         );
322                         wasmtime::component::__internal::Box::new(
323                             async move {
324                                 tracing::event!(
325                                     tracing::Level::TRACE, a = tracing::field::debug(& arg0), b
326                                     = tracing::field::debug(& arg1), "call"
327                                 );
328                                 let host = &mut host_getter(caller.data_mut());
329                                 let r = Host::c(host, arg0, arg1).await;
330                                 tracing::event!(
331                                     tracing::Level::TRACE, result = tracing::field::debug(& r),
332                                     "return"
333                                 );
334                                 Ok((r,))
335                             }
336                                 .instrument(span),
337                         )
338                     },
339                 )?;
340                 Ok(())
341             }
342         }
343     }
344 }
345 pub mod exports {
346     pub mod foo {
347         pub mod foo {
348             #[allow(clippy::all)]
349             pub mod strings {
350                 #[allow(unused_imports)]
351                 use wasmtime::component::__internal::{anyhow, Box};
352                 #[derive(Clone)]
353                 pub struct Guest {
354                     a: wasmtime::component::Func,
355                     b: wasmtime::component::Func,
356                     c: wasmtime::component::Func,
357                 }
358                 #[derive(Clone)]
359                 pub struct GuestIndices {
360                     a: wasmtime::component::ComponentExportIndex,
361                     b: wasmtime::component::ComponentExportIndex,
362                     c: wasmtime::component::ComponentExportIndex,
363                 }
364                 impl GuestIndices {
365                     /// Constructor for [`GuestIndices`] which takes a
366                     /// [`Component`](wasmtime::component::Component) as input and can be executed
367                     /// before instantiation.
368                     ///
369                     /// This constructor can be used to front-load string lookups to find exports
370                     /// within a component.
371                     pub fn new<_T>(
372                         _instance_pre: &wasmtime::component::InstancePre<_T>,
373                     ) -> wasmtime::Result<GuestIndices> {
374                         let instance = _instance_pre
375                             .component()
376                             .get_export_index(None, "foo:foo/strings")
377                             .ok_or_else(|| {
378                                 anyhow::anyhow!(
379                                     "no exported instance named `foo:foo/strings`"
380                                 )
381                             })?;
382                         let mut lookup = move |name| {
383                             _instance_pre
384                                 .component()
385                                 .get_export_index(Some(&instance), name)
386                                 .ok_or_else(|| {
387                                     anyhow::anyhow!(
388                                         "instance export `foo:foo/strings` does \
389                       not have export `{name}`"
390                                     )
391                                 })
392                         };
393                         let _ = &mut lookup;
394                         let a = lookup("a")?;
395                         let b = lookup("b")?;
396                         let c = lookup("c")?;
397                         Ok(GuestIndices { a, b, c })
398                     }
399                     pub fn load(
400                         &self,
401                         mut store: impl wasmtime::AsContextMut,
402                         instance: &wasmtime::component::Instance,
403                     ) -> wasmtime::Result<Guest> {
404                         let _instance = instance;
405                         let _instance_pre = _instance.instance_pre(&store);
406                         let _instance_type = _instance_pre.instance_type();
407                         let mut store = store.as_context_mut();
408                         let _ = &mut store;
409                         let a = *_instance
410                             .get_typed_func::<(&str,), ()>(&mut store, &self.a)?
411                             .func();
412                         let b = *_instance
413                             .get_typed_func::<
414                                 (),
415                                 (wasmtime::component::__internal::String,),
416                             >(&mut store, &self.b)?
417                             .func();
418                         let c = *_instance
419                             .get_typed_func::<
420                                 (&str, &str),
421                                 (wasmtime::component::__internal::String,),
422                             >(&mut store, &self.c)?
423                             .func();
424                         Ok(Guest { a, b, c })
425                     }
426                 }
427                 impl Guest {
428                     pub async fn call_a<S: wasmtime::AsContextMut>(
429                         &self,
430                         mut store: S,
431                         arg0: &str,
432                     ) -> wasmtime::Result<()>
433                     where
434                         <S as wasmtime::AsContext>::Data: Send,
435                     {
436                         use tracing::Instrument;
437                         let span = tracing::span!(
438                             tracing::Level::TRACE, "wit-bindgen export", module =
439                             "foo:foo/strings", function = "a",
440                         );
441                         let callee = unsafe {
442                             wasmtime::component::TypedFunc::<
443                                 (&str,),
444                                 (),
445                             >::new_unchecked(self.a)
446                         };
447                         let () = callee
448                             .call_async(store.as_context_mut(), (arg0,))
449                             .instrument(span.clone())
450                             .await?;
451                         callee
452                             .post_return_async(store.as_context_mut())
453                             .instrument(span)
454                             .await?;
455                         Ok(())
456                     }
457                     pub async fn call_b<S: wasmtime::AsContextMut>(
458                         &self,
459                         mut store: S,
460                     ) -> wasmtime::Result<wasmtime::component::__internal::String>
461                     where
462                         <S as wasmtime::AsContext>::Data: Send,
463                     {
464                         use tracing::Instrument;
465                         let span = tracing::span!(
466                             tracing::Level::TRACE, "wit-bindgen export", module =
467                             "foo:foo/strings", function = "b",
468                         );
469                         let callee = unsafe {
470                             wasmtime::component::TypedFunc::<
471                                 (),
472                                 (wasmtime::component::__internal::String,),
473                             >::new_unchecked(self.b)
474                         };
475                         let (ret0,) = callee
476                             .call_async(store.as_context_mut(), ())
477                             .instrument(span.clone())
478                             .await?;
479                         callee
480                             .post_return_async(store.as_context_mut())
481                             .instrument(span)
482                             .await?;
483                         Ok(ret0)
484                     }
485                     pub async fn call_c<S: wasmtime::AsContextMut>(
486                         &self,
487                         mut store: S,
488                         arg0: &str,
489                         arg1: &str,
490                     ) -> wasmtime::Result<wasmtime::component::__internal::String>
491                     where
492                         <S as wasmtime::AsContext>::Data: Send,
493                     {
494                         use tracing::Instrument;
495                         let span = tracing::span!(
496                             tracing::Level::TRACE, "wit-bindgen export", module =
497                             "foo:foo/strings", function = "c",
498                         );
499                         let callee = unsafe {
500                             wasmtime::component::TypedFunc::<
501                                 (&str, &str),
502                                 (wasmtime::component::__internal::String,),
503                             >::new_unchecked(self.c)
504                         };
505                         let (ret0,) = callee
506                             .call_async(store.as_context_mut(), (arg0, arg1))
507                             .instrument(span.clone())
508                             .await?;
509                         callee
510                             .post_return_async(store.as_context_mut())
511                             .instrument(span)
512                             .await?;
513                         Ok(ret0)
514                     }
515                 }
516             }
517         }
518     }
519 }
520