1 /// Auto-generated bindings for a pre-instantiated version of a
2 /// component which implements the world `the-world`.
3 ///
4 /// This structure is created through [`TheWorldPre::new`] which
5 /// takes a [`InstancePre`](wasmtime::component::InstancePre) that
6 /// has been created through a [`Linker`](wasmtime::component::Linker).
7 ///
8 /// For more information see [`TheWorld`] as well.
9 pub struct TheWorldPre<T> {
10     instance_pre: wasmtime::component::InstancePre<T>,
11     indices: TheWorldIndices,
12 }
13 impl<T> Clone for TheWorldPre<T> {
14     fn clone(&self) -> Self {
15         Self {
16             instance_pre: self.instance_pre.clone(),
17             indices: self.indices.clone(),
18         }
19     }
20 }
21 impl<_T> TheWorldPre<_T> {
22     /// Creates a new copy of `TheWorldPre` bindings which can then
23     /// be used to instantiate into a particular store.
24     ///
25     /// This method may fail if the component behind `instance_pre`
26     /// does not have the required exports.
27     pub fn new(
28         instance_pre: wasmtime::component::InstancePre<_T>,
29     ) -> wasmtime::Result<Self> {
30         let indices = TheWorldIndices::new(&instance_pre)?;
31         Ok(Self { instance_pre, indices })
32     }
33     pub fn engine(&self) -> &wasmtime::Engine {
34         self.instance_pre.engine()
35     }
36     pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {
37         &self.instance_pre
38     }
39     /// Instantiates a new instance of [`TheWorld`] within the
40     /// `store` provided.
41     ///
42     /// This function will use `self` as the pre-instantiated
43     /// instance to perform instantiation. Afterwards the preloaded
44     /// indices in `self` are used to lookup all exports on the
45     /// resulting instance.
46     pub async fn instantiate_async(
47         &self,
48         mut store: impl wasmtime::AsContextMut<Data = _T>,
49     ) -> wasmtime::Result<TheWorld>
50     where
51         _T: Send,
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, U>(
156             linker: &mut wasmtime::component::Linker<T>,
157             get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
158         ) -> wasmtime::Result<()>
159         where
160             T: Send,
161             U: foo::foo::strings::Host + Send,
162         {
163             foo::foo::strings::add_to_linker(linker, get)?;
164             Ok(())
165         }
166         pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest {
167             &self.interface0
168         }
169     }
170 };
171 pub mod foo {
172     pub mod foo {
173         #[allow(clippy::all)]
174         pub mod strings {
175             #[allow(unused_imports)]
176             use wasmtime::component::__internal::{anyhow, Box};
177             #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
178             pub trait Host: Send {
179                 async fn a(&mut self, x: wasmtime::component::__internal::String) -> ();
180                 async fn b(&mut self) -> wasmtime::component::__internal::String;
181                 async fn c(
182                     &mut self,
183                     a: wasmtime::component::__internal::String,
184                     b: wasmtime::component::__internal::String,
185                 ) -> wasmtime::component::__internal::String;
186             }
187             pub trait GetHost<
188                 T,
189                 D,
190             >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static {
191                 type Host: Host + Send;
192             }
193             impl<F, T, D, O> GetHost<T, D> for F
194             where
195                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
196                 O: Host + Send,
197             {
198                 type Host = O;
199             }
200             pub fn add_to_linker_get_host<
201                 T,
202                 G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>,
203             >(
204                 linker: &mut wasmtime::component::Linker<T>,
205                 host_getter: G,
206             ) -> wasmtime::Result<()>
207             where
208                 T: Send,
209             {
210                 let mut inst = linker.instance("foo:foo/strings")?;
211                 inst.func_wrap_async(
212                     "a",
213                     move |
214                         mut caller: wasmtime::StoreContextMut<'_, T>,
215                         (arg0,): (wasmtime::component::__internal::String,)|
216                     {
217                         wasmtime::component::__internal::Box::new(async move {
218                             let host = &mut host_getter(caller.data_mut());
219                             let r = Host::a(host, arg0).await;
220                             Ok(r)
221                         })
222                     },
223                 )?;
224                 inst.func_wrap_async(
225                     "b",
226                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
227                         wasmtime::component::__internal::Box::new(async move {
228                             let host = &mut host_getter(caller.data_mut());
229                             let r = Host::b(host).await;
230                             Ok((r,))
231                         })
232                     },
233                 )?;
234                 inst.func_wrap_async(
235                     "c",
236                     move |
237                         mut caller: wasmtime::StoreContextMut<'_, T>,
238                         (
239                             arg0,
240                             arg1,
241                         ): (
242                             wasmtime::component::__internal::String,
243                             wasmtime::component::__internal::String,
244                         )|
245                     {
246                         wasmtime::component::__internal::Box::new(async move {
247                             let host = &mut host_getter(caller.data_mut());
248                             let r = Host::c(host, arg0, arg1).await;
249                             Ok((r,))
250                         })
251                     },
252                 )?;
253                 Ok(())
254             }
255             pub fn add_to_linker<T, U>(
256                 linker: &mut wasmtime::component::Linker<T>,
257                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
258             ) -> wasmtime::Result<()>
259             where
260                 U: Host + Send,
261                 T: Send,
262             {
263                 add_to_linker_get_host(linker, get)
264             }
265             impl<_T: Host + ?Sized + Send> Host for &mut _T {
266                 async fn a(&mut self, x: wasmtime::component::__internal::String) -> () {
267                     Host::a(*self, x).await
268                 }
269                 async fn b(&mut self) -> wasmtime::component::__internal::String {
270                     Host::b(*self).await
271                 }
272                 async fn c(
273                     &mut self,
274                     a: wasmtime::component::__internal::String,
275                     b: wasmtime::component::__internal::String,
276                 ) -> wasmtime::component::__internal::String {
277                     Host::c(*self, a, b).await
278                 }
279             }
280         }
281     }
282 }
283 pub mod exports {
284     pub mod foo {
285         pub mod foo {
286             #[allow(clippy::all)]
287             pub mod strings {
288                 #[allow(unused_imports)]
289                 use wasmtime::component::__internal::{anyhow, Box};
290                 pub struct Guest {
291                     a: wasmtime::component::Func,
292                     b: wasmtime::component::Func,
293                     c: wasmtime::component::Func,
294                 }
295                 #[derive(Clone)]
296                 pub struct GuestIndices {
297                     a: wasmtime::component::ComponentExportIndex,
298                     b: wasmtime::component::ComponentExportIndex,
299                     c: wasmtime::component::ComponentExportIndex,
300                 }
301                 impl GuestIndices {
302                     /// Constructor for [`GuestIndices`] which takes a
303                     /// [`Component`](wasmtime::component::Component) as input and can be executed
304                     /// before instantiation.
305                     ///
306                     /// This constructor can be used to front-load string lookups to find exports
307                     /// within a component.
308                     pub fn new<_T>(
309                         _instance_pre: &wasmtime::component::InstancePre<_T>,
310                     ) -> wasmtime::Result<GuestIndices> {
311                         let instance = _instance_pre
312                             .component()
313                             .get_export_index(None, "foo:foo/strings")
314                             .ok_or_else(|| {
315                                 anyhow::anyhow!(
316                                     "no exported instance named `foo:foo/strings`"
317                                 )
318                             })?;
319                         let mut lookup = move |name| {
320                             _instance_pre
321                                 .component()
322                                 .get_export_index(Some(&instance), name)
323                                 .ok_or_else(|| {
324                                     anyhow::anyhow!(
325                                         "instance export `foo:foo/strings` does \
326                 not have export `{name}`"
327                                     )
328                                 })
329                         };
330                         let _ = &mut lookup;
331                         let a = lookup("a")?;
332                         let b = lookup("b")?;
333                         let c = lookup("c")?;
334                         Ok(GuestIndices { a, b, c })
335                     }
336                     pub fn load(
337                         &self,
338                         mut store: impl wasmtime::AsContextMut,
339                         instance: &wasmtime::component::Instance,
340                     ) -> wasmtime::Result<Guest> {
341                         let _instance = instance;
342                         let _instance_pre = _instance.instance_pre(&store);
343                         let _instance_type = _instance_pre.instance_type();
344                         let mut store = store.as_context_mut();
345                         let _ = &mut store;
346                         let a = *_instance
347                             .get_typed_func::<(&str,), ()>(&mut store, &self.a)?
348                             .func();
349                         let b = *_instance
350                             .get_typed_func::<
351                                 (),
352                                 (wasmtime::component::__internal::String,),
353                             >(&mut store, &self.b)?
354                             .func();
355                         let c = *_instance
356                             .get_typed_func::<
357                                 (&str, &str),
358                                 (wasmtime::component::__internal::String,),
359                             >(&mut store, &self.c)?
360                             .func();
361                         Ok(Guest { a, b, c })
362                     }
363                 }
364                 impl Guest {
365                     pub async fn call_a<S: wasmtime::AsContextMut>(
366                         &self,
367                         mut store: S,
368                         arg0: &str,
369                     ) -> wasmtime::Result<()>
370                     where
371                         <S as wasmtime::AsContext>::Data: Send,
372                     {
373                         let callee = unsafe {
374                             wasmtime::component::TypedFunc::<
375                                 (&str,),
376                                 (),
377                             >::new_unchecked(self.a)
378                         };
379                         let () = callee
380                             .call_async(store.as_context_mut(), (arg0,))
381                             .await?;
382                         callee.post_return_async(store.as_context_mut()).await?;
383                         Ok(())
384                     }
385                     pub async fn call_b<S: wasmtime::AsContextMut>(
386                         &self,
387                         mut store: S,
388                     ) -> wasmtime::Result<wasmtime::component::__internal::String>
389                     where
390                         <S as wasmtime::AsContext>::Data: Send,
391                     {
392                         let callee = unsafe {
393                             wasmtime::component::TypedFunc::<
394                                 (),
395                                 (wasmtime::component::__internal::String,),
396                             >::new_unchecked(self.b)
397                         };
398                         let (ret0,) = callee
399                             .call_async(store.as_context_mut(), ())
400                             .await?;
401                         callee.post_return_async(store.as_context_mut()).await?;
402                         Ok(ret0)
403                     }
404                     pub async fn call_c<S: wasmtime::AsContextMut>(
405                         &self,
406                         mut store: S,
407                         arg0: &str,
408                         arg1: &str,
409                     ) -> wasmtime::Result<wasmtime::component::__internal::String>
410                     where
411                         <S as wasmtime::AsContext>::Data: Send,
412                     {
413                         let callee = unsafe {
414                             wasmtime::component::TypedFunc::<
415                                 (&str, &str),
416                                 (wasmtime::component::__internal::String,),
417                             >::new_unchecked(self.c)
418                         };
419                         let (ret0,) = callee
420                             .call_async(store.as_context_mut(), (arg0, arg1))
421                             .await?;
422                         callee.post_return_async(store.as_context_mut()).await?;
423                         Ok(ret0)
424                     }
425                 }
426             }
427         }
428     }
429 }
430