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