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.component())?;
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 /// * You can also access the guts of instantiation through
89 ///   [`TheWorldIndices::new_instance`] followed
90 ///   by [`TheWorldIndices::load`] to crate an instance of this
91 ///   type.
92 ///
93 /// These methods are all equivalent to one another and move
94 /// around the tradeoff of what work is performed when.
95 ///
96 /// [`Store`]: wasmtime::Store
97 /// [`Component`]: wasmtime::component::Component
98 /// [`Linker`]: wasmtime::component::Linker
99 pub struct TheWorld {
100     interface0: exports::foo::foo::strings::Guest,
101 }
102 const _: () = {
103     #[allow(unused_imports)]
104     use wasmtime::component::__internal::anyhow;
105     impl TheWorldIndices {
106         /// Creates a new copy of `TheWorldIndices` bindings which can then
107         /// be used to instantiate into a particular store.
108         ///
109         /// This method may fail if the component does not have the
110         /// required exports.
111         pub fn new(
112             component: &wasmtime::component::Component,
113         ) -> wasmtime::Result<Self> {
114             let _component = component;
115             let interface0 = exports::foo::foo::strings::GuestIndices::new(_component)?;
116             Ok(TheWorldIndices { interface0 })
117         }
118         /// Creates a new instance of [`TheWorldIndices`] from an
119         /// instantiated component.
120         ///
121         /// This method of creating a [`TheWorld`] will perform string
122         /// lookups for all exports when this method is called. This
123         /// will only succeed if the provided instance matches the
124         /// requirements of [`TheWorld`].
125         pub fn new_instance(
126             mut store: impl wasmtime::AsContextMut,
127             instance: &wasmtime::component::Instance,
128         ) -> wasmtime::Result<Self> {
129             let _instance = instance;
130             let interface0 = exports::foo::foo::strings::GuestIndices::new_instance(
131                 &mut store,
132                 _instance,
133             )?;
134             Ok(TheWorldIndices { interface0 })
135         }
136         /// Uses the indices stored in `self` to load an instance
137         /// of [`TheWorld`] from the instance provided.
138         ///
139         /// Note that at this time this method will additionally
140         /// perform type-checks of all exports.
141         pub fn load(
142             &self,
143             mut store: impl wasmtime::AsContextMut,
144             instance: &wasmtime::component::Instance,
145         ) -> wasmtime::Result<TheWorld> {
146             let _instance = instance;
147             let interface0 = self.interface0.load(&mut store, &_instance)?;
148             Ok(TheWorld { interface0 })
149         }
150     }
151     impl TheWorld {
152         /// Convenience wrapper around [`TheWorldPre::new`] and
153         /// [`TheWorldPre::instantiate_async`].
154         pub async fn instantiate_async<_T>(
155             mut store: impl wasmtime::AsContextMut<Data = _T>,
156             component: &wasmtime::component::Component,
157             linker: &wasmtime::component::Linker<_T>,
158         ) -> wasmtime::Result<TheWorld>
159         where
160             _T: Send,
161         {
162             let pre = linker.instantiate_pre(component)?;
163             TheWorldPre::new(pre)?.instantiate_async(store).await
164         }
165         /// Convenience wrapper around [`TheWorldIndices::new_instance`] and
166         /// [`TheWorldIndices::load`].
167         pub fn new(
168             mut store: impl wasmtime::AsContextMut,
169             instance: &wasmtime::component::Instance,
170         ) -> wasmtime::Result<TheWorld> {
171             let indices = TheWorldIndices::new_instance(&mut store, instance)?;
172             indices.load(store, instance)
173         }
174         pub fn add_to_linker<T, U>(
175             linker: &mut wasmtime::component::Linker<T>,
176             get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
177         ) -> wasmtime::Result<()>
178         where
179             T: Send,
180             U: foo::foo::strings::Host + Send,
181         {
182             foo::foo::strings::add_to_linker(linker, get)?;
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;
196             #[wasmtime::component::__internal::async_trait]
197             pub trait Host: Send {
198                 async fn a(&mut self, x: wasmtime::component::__internal::String) -> ();
199                 async fn b(&mut self) -> wasmtime::component::__internal::String;
200                 async fn c(
201                     &mut self,
202                     a: wasmtime::component::__internal::String,
203                     b: wasmtime::component::__internal::String,
204                 ) -> wasmtime::component::__internal::String;
205             }
206             pub trait GetHost<
207                 T,
208             >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
209                 type Host: Host + Send;
210             }
211             impl<F, T, O> GetHost<T> for F
212             where
213                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
214                 O: Host + Send,
215             {
216                 type Host = O;
217             }
218             pub fn add_to_linker_get_host<T>(
219                 linker: &mut wasmtime::component::Linker<T>,
220                 host_getter: impl for<'a> GetHost<&'a mut T>,
221             ) -> wasmtime::Result<()>
222             where
223                 T: Send,
224             {
225                 let mut inst = linker.instance("foo:foo/strings")?;
226                 inst.func_wrap_async(
227                     "a",
228                     move |
229                         mut caller: wasmtime::StoreContextMut<'_, T>,
230                         (arg0,): (wasmtime::component::__internal::String,)|
231                     {
232                         wasmtime::component::__internal::Box::new(async move {
233                             let host = &mut host_getter(caller.data_mut());
234                             let r = Host::a(host, arg0).await;
235                             Ok(r)
236                         })
237                     },
238                 )?;
239                 inst.func_wrap_async(
240                     "b",
241                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
242                         wasmtime::component::__internal::Box::new(async move {
243                             let host = &mut host_getter(caller.data_mut());
244                             let r = Host::b(host).await;
245                             Ok((r,))
246                         })
247                     },
248                 )?;
249                 inst.func_wrap_async(
250                     "c",
251                     move |
252                         mut caller: wasmtime::StoreContextMut<'_, T>,
253                         (
254                             arg0,
255                             arg1,
256                         ): (
257                             wasmtime::component::__internal::String,
258                             wasmtime::component::__internal::String,
259                         )|
260                     {
261                         wasmtime::component::__internal::Box::new(async move {
262                             let host = &mut host_getter(caller.data_mut());
263                             let r = Host::c(host, arg0, arg1).await;
264                             Ok((r,))
265                         })
266                     },
267                 )?;
268                 Ok(())
269             }
270             pub fn add_to_linker<T, U>(
271                 linker: &mut wasmtime::component::Linker<T>,
272                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
273             ) -> wasmtime::Result<()>
274             where
275                 U: Host + Send,
276                 T: Send,
277             {
278                 add_to_linker_get_host(linker, get)
279             }
280             #[wasmtime::component::__internal::async_trait]
281             impl<_T: Host + ?Sized + Send> Host for &mut _T {
282                 async fn a(&mut self, x: wasmtime::component::__internal::String) -> () {
283                     Host::a(*self, x).await
284                 }
285                 async fn b(&mut self) -> wasmtime::component::__internal::String {
286                     Host::b(*self).await
287                 }
288                 async fn c(
289                     &mut self,
290                     a: wasmtime::component::__internal::String,
291                     b: wasmtime::component::__internal::String,
292                 ) -> wasmtime::component::__internal::String {
293                     Host::c(*self, a, b).await
294                 }
295             }
296         }
297     }
298 }
299 pub mod exports {
300     pub mod foo {
301         pub mod foo {
302             #[allow(clippy::all)]
303             pub mod strings {
304                 #[allow(unused_imports)]
305                 use wasmtime::component::__internal::anyhow;
306                 pub struct Guest {
307                     a: wasmtime::component::Func,
308                     b: wasmtime::component::Func,
309                     c: wasmtime::component::Func,
310                 }
311                 #[derive(Clone)]
312                 pub struct GuestIndices {
313                     a: wasmtime::component::ComponentExportIndex,
314                     b: wasmtime::component::ComponentExportIndex,
315                     c: wasmtime::component::ComponentExportIndex,
316                 }
317                 impl GuestIndices {
318                     /// Constructor for [`GuestIndices`] which takes a
319                     /// [`Component`](wasmtime::component::Component) as input and can be executed
320                     /// before instantiation.
321                     ///
322                     /// This constructor can be used to front-load string lookups to find exports
323                     /// within a component.
324                     pub fn new(
325                         component: &wasmtime::component::Component,
326                     ) -> wasmtime::Result<GuestIndices> {
327                         let (_, instance) = component
328                             .export_index(None, "foo:foo/strings")
329                             .ok_or_else(|| {
330                                 anyhow::anyhow!(
331                                     "no exported instance named `foo:foo/strings`"
332                                 )
333                             })?;
334                         Self::_new(|name| {
335                             component.export_index(Some(&instance), name).map(|p| p.1)
336                         })
337                     }
338                     /// This constructor is similar to [`GuestIndices::new`] except that it
339                     /// performs string lookups after instantiation time.
340                     pub fn new_instance(
341                         mut store: impl wasmtime::AsContextMut,
342                         instance: &wasmtime::component::Instance,
343                     ) -> wasmtime::Result<GuestIndices> {
344                         let instance_export = instance
345                             .get_export(&mut store, None, "foo:foo/strings")
346                             .ok_or_else(|| {
347                                 anyhow::anyhow!(
348                                     "no exported instance named `foo:foo/strings`"
349                                 )
350                             })?;
351                         Self::_new(|name| {
352                             instance.get_export(&mut store, Some(&instance_export), name)
353                         })
354                     }
355                     fn _new(
356                         mut lookup: impl FnMut(
357                             &str,
358                         ) -> Option<wasmtime::component::ComponentExportIndex>,
359                     ) -> wasmtime::Result<GuestIndices> {
360                         let mut lookup = move |name| {
361                             lookup(name)
362                                 .ok_or_else(|| {
363                                     anyhow::anyhow!(
364                                         "instance export `foo:foo/strings` does \
365                 not have export `{name}`"
366                                     )
367                                 })
368                         };
369                         let _ = &mut lookup;
370                         let a = lookup("a")?;
371                         let b = lookup("b")?;
372                         let c = lookup("c")?;
373                         Ok(GuestIndices { a, b, c })
374                     }
375                     pub fn load(
376                         &self,
377                         mut store: impl wasmtime::AsContextMut,
378                         instance: &wasmtime::component::Instance,
379                     ) -> wasmtime::Result<Guest> {
380                         let mut store = store.as_context_mut();
381                         let _ = &mut store;
382                         let _instance = instance;
383                         let a = *_instance
384                             .get_typed_func::<(&str,), ()>(&mut store, &self.a)?
385                             .func();
386                         let b = *_instance
387                             .get_typed_func::<
388                                 (),
389                                 (wasmtime::component::__internal::String,),
390                             >(&mut store, &self.b)?
391                             .func();
392                         let c = *_instance
393                             .get_typed_func::<
394                                 (&str, &str),
395                                 (wasmtime::component::__internal::String,),
396                             >(&mut store, &self.c)?
397                             .func();
398                         Ok(Guest { a, b, c })
399                     }
400                 }
401                 impl Guest {
402                     pub async fn call_a<S: wasmtime::AsContextMut>(
403                         &self,
404                         mut store: S,
405                         arg0: &str,
406                     ) -> wasmtime::Result<()>
407                     where
408                         <S as wasmtime::AsContext>::Data: Send,
409                     {
410                         let callee = unsafe {
411                             wasmtime::component::TypedFunc::<
412                                 (&str,),
413                                 (),
414                             >::new_unchecked(self.a)
415                         };
416                         let () = callee
417                             .call_async(store.as_context_mut(), (arg0,))
418                             .await?;
419                         callee.post_return_async(store.as_context_mut()).await?;
420                         Ok(())
421                     }
422                     pub async fn call_b<S: wasmtime::AsContextMut>(
423                         &self,
424                         mut store: S,
425                     ) -> wasmtime::Result<wasmtime::component::__internal::String>
426                     where
427                         <S as wasmtime::AsContext>::Data: Send,
428                     {
429                         let callee = unsafe {
430                             wasmtime::component::TypedFunc::<
431                                 (),
432                                 (wasmtime::component::__internal::String,),
433                             >::new_unchecked(self.b)
434                         };
435                         let (ret0,) = callee
436                             .call_async(store.as_context_mut(), ())
437                             .await?;
438                         callee.post_return_async(store.as_context_mut()).await?;
439                         Ok(ret0)
440                     }
441                     pub async fn call_c<S: wasmtime::AsContextMut>(
442                         &self,
443                         mut store: S,
444                         arg0: &str,
445                         arg1: &str,
446                     ) -> wasmtime::Result<wasmtime::component::__internal::String>
447                     where
448                         <S as wasmtime::AsContext>::Data: Send,
449                     {
450                         let callee = unsafe {
451                             wasmtime::component::TypedFunc::<
452                                 (&str, &str),
453                                 (wasmtime::component::__internal::String,),
454                             >::new_unchecked(self.c)
455                         };
456                         let (ret0,) = callee
457                             .call_async(store.as_context_mut(), (arg0, arg1))
458                             .await?;
459                         callee.post_return_async(store.as_context_mut()).await?;
460                         Ok(ret0)
461                     }
462                 }
463             }
464         }
465     }
466 }
467