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 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 /// Auto-generated bindings for index of the exports of
56 /// `the-world`.
57 ///
58 /// This is an implementation detail of [`TheWorldPre`] and can
59 /// be constructed if needed as well.
60 ///
61 /// For more information see [`TheWorld`] as well.
62 #[derive(Clone)]
63 pub struct TheWorldIndices {
64     interface0: exports::foo::foo::multi_return::GuestIndices,
65 }
66 /// Auto-generated bindings for an instance a component which
67 /// implements the world `the-world`.
68 ///
69 /// This structure can be created through a number of means
70 /// depending on your requirements and what you have on hand:
71 ///
72 /// * The most convenient way is to use
73 ///   [`TheWorld::instantiate`] which only needs a
74 ///   [`Store`], [`Component`], and [`Linker`].
75 ///
76 /// * Alternatively you can create a [`TheWorldPre`] ahead of
77 ///   time with a [`Component`] to front-load string lookups
78 ///   of exports once instead of per-instantiation. This
79 ///   method then uses [`TheWorldPre::instantiate`] to
80 ///   create a [`TheWorld`].
81 ///
82 /// * If you've instantiated the instance yourself already
83 ///   then you can use [`TheWorld::new`].
84 ///
85 /// * You can also access the guts of instantiation through
86 ///   [`TheWorldIndices::new_instance`] followed
87 ///   by [`TheWorldIndices::load`] to crate an instance of this
88 ///   type.
89 ///
90 /// These methods are all equivalent to one another and move
91 /// around the tradeoff of what work is performed when.
92 ///
93 /// [`Store`]: wasmtime::Store
94 /// [`Component`]: wasmtime::component::Component
95 /// [`Linker`]: wasmtime::component::Linker
96 pub struct TheWorld {
97     interface0: exports::foo::foo::multi_return::Guest,
98 }
99 const _: () = {
100     #[allow(unused_imports)]
101     use wasmtime::component::__internal::anyhow;
102     impl TheWorldIndices {
103         /// Creates a new copy of `TheWorldIndices` bindings which can then
104         /// be used to instantiate into a particular store.
105         ///
106         /// This method may fail if the component does not have the
107         /// required exports.
108         pub fn new(
109             component: &wasmtime::component::Component,
110         ) -> wasmtime::Result<Self> {
111             let _component = component;
112             let interface0 = exports::foo::foo::multi_return::GuestIndices::new(
113                 _component,
114             )?;
115             Ok(TheWorldIndices { interface0 })
116         }
117         /// Creates a new instance of [`TheWorldIndices`] from an
118         /// instantiated component.
119         ///
120         /// This method of creating a [`TheWorld`] will perform string
121         /// lookups for all exports when this method is called. This
122         /// will only succeed if the provided instance matches the
123         /// requirements of [`TheWorld`].
124         pub fn new_instance(
125             mut store: impl wasmtime::AsContextMut,
126             instance: &wasmtime::component::Instance,
127         ) -> wasmtime::Result<Self> {
128             let _instance = instance;
129             let interface0 = exports::foo::foo::multi_return::GuestIndices::new_instance(
130                 &mut store,
131                 _instance,
132             )?;
133             Ok(TheWorldIndices { interface0 })
134         }
135         /// Uses the indices stored in `self` to load an instance
136         /// of [`TheWorld`] from the instance provided.
137         ///
138         /// Note that at this time this method will additionally
139         /// perform type-checks of all exports.
140         pub fn load(
141             &self,
142             mut store: impl wasmtime::AsContextMut,
143             instance: &wasmtime::component::Instance,
144         ) -> wasmtime::Result<TheWorld> {
145             let _instance = instance;
146             let interface0 = self.interface0.load(&mut store, &_instance)?;
147             Ok(TheWorld { interface0 })
148         }
149     }
150     impl TheWorld {
151         /// Convenience wrapper around [`TheWorldPre::new`] and
152         /// [`TheWorldPre::instantiate`].
153         pub fn instantiate<_T>(
154             mut store: impl wasmtime::AsContextMut<Data = _T>,
155             component: &wasmtime::component::Component,
156             linker: &wasmtime::component::Linker<_T>,
157         ) -> wasmtime::Result<TheWorld> {
158             let pre = linker.instantiate_pre(component)?;
159             TheWorldPre::new(pre)?.instantiate(store)
160         }
161         /// Convenience wrapper around [`TheWorldIndices::new_instance`] and
162         /// [`TheWorldIndices::load`].
163         pub fn new(
164             mut store: impl wasmtime::AsContextMut,
165             instance: &wasmtime::component::Instance,
166         ) -> wasmtime::Result<TheWorld> {
167             let indices = TheWorldIndices::new_instance(&mut store, instance)?;
168             indices.load(store, instance)
169         }
170         pub fn add_to_linker<T, U>(
171             linker: &mut wasmtime::component::Linker<T>,
172             get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
173         ) -> wasmtime::Result<()>
174         where
175             U: foo::foo::multi_return::Host,
176         {
177             foo::foo::multi_return::add_to_linker(linker, get)?;
178             Ok(())
179         }
180         pub fn foo_foo_multi_return(&self) -> &exports::foo::foo::multi_return::Guest {
181             &self.interface0
182         }
183     }
184 };
185 pub mod foo {
186     pub mod foo {
187         #[allow(clippy::all)]
188         pub mod multi_return {
189             #[allow(unused_imports)]
190             use wasmtime::component::__internal::{anyhow, Box};
191             pub trait Host {
192                 fn mra(&mut self) -> ();
193                 fn mrb(&mut self) -> ();
194                 fn mrc(&mut self) -> u32;
195                 fn mrd(&mut self) -> u32;
196                 fn mre(&mut self) -> (u32, f32);
197             }
198             pub trait GetHost<
199                 T,
200                 D,
201             >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static {
202                 type Host: Host;
203             }
204             impl<F, T, D, O> GetHost<T, D> for F
205             where
206                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
207                 O: Host,
208             {
209                 type Host = O;
210             }
211             pub fn add_to_linker_get_host<
212                 T,
213                 G: for<'a> GetHost<&'a mut T, T, Host: Host>,
214             >(
215                 linker: &mut wasmtime::component::Linker<T>,
216                 host_getter: G,
217             ) -> wasmtime::Result<()> {
218                 let mut inst = linker.instance("foo:foo/multi-return")?;
219                 inst.func_wrap(
220                     "mra",
221                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
222                         let host = &mut host_getter(caller.data_mut());
223                         let r = Host::mra(host);
224                         Ok(r)
225                     },
226                 )?;
227                 inst.func_wrap(
228                     "mrb",
229                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
230                         let host = &mut host_getter(caller.data_mut());
231                         let r = Host::mrb(host);
232                         Ok(r)
233                     },
234                 )?;
235                 inst.func_wrap(
236                     "mrc",
237                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
238                         let host = &mut host_getter(caller.data_mut());
239                         let r = Host::mrc(host);
240                         Ok((r,))
241                     },
242                 )?;
243                 inst.func_wrap(
244                     "mrd",
245                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
246                         let host = &mut host_getter(caller.data_mut());
247                         let r = Host::mrd(host);
248                         Ok((r,))
249                     },
250                 )?;
251                 inst.func_wrap(
252                     "mre",
253                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
254                         let host = &mut host_getter(caller.data_mut());
255                         let r = Host::mre(host);
256                         Ok(r)
257                     },
258                 )?;
259                 Ok(())
260             }
261             pub fn add_to_linker<T, U>(
262                 linker: &mut wasmtime::component::Linker<T>,
263                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
264             ) -> wasmtime::Result<()>
265             where
266                 U: Host,
267             {
268                 add_to_linker_get_host(linker, get)
269             }
270             impl<_T: Host + ?Sized> Host for &mut _T {
271                 fn mra(&mut self) -> () {
272                     Host::mra(*self)
273                 }
274                 fn mrb(&mut self) -> () {
275                     Host::mrb(*self)
276                 }
277                 fn mrc(&mut self) -> u32 {
278                     Host::mrc(*self)
279                 }
280                 fn mrd(&mut self) -> u32 {
281                     Host::mrd(*self)
282                 }
283                 fn mre(&mut self) -> (u32, f32) {
284                     Host::mre(*self)
285                 }
286             }
287         }
288     }
289 }
290 pub mod exports {
291     pub mod foo {
292         pub mod foo {
293             #[allow(clippy::all)]
294             pub mod multi_return {
295                 #[allow(unused_imports)]
296                 use wasmtime::component::__internal::{anyhow, Box};
297                 pub struct Guest {
298                     mra: wasmtime::component::Func,
299                     mrb: wasmtime::component::Func,
300                     mrc: wasmtime::component::Func,
301                     mrd: wasmtime::component::Func,
302                     mre: wasmtime::component::Func,
303                 }
304                 #[derive(Clone)]
305                 pub struct GuestIndices {
306                     mra: wasmtime::component::ComponentExportIndex,
307                     mrb: wasmtime::component::ComponentExportIndex,
308                     mrc: wasmtime::component::ComponentExportIndex,
309                     mrd: wasmtime::component::ComponentExportIndex,
310                     mre: wasmtime::component::ComponentExportIndex,
311                 }
312                 impl GuestIndices {
313                     /// Constructor for [`GuestIndices`] which takes a
314                     /// [`Component`](wasmtime::component::Component) as input and can be executed
315                     /// before instantiation.
316                     ///
317                     /// This constructor can be used to front-load string lookups to find exports
318                     /// within a component.
319                     pub fn new(
320                         component: &wasmtime::component::Component,
321                     ) -> wasmtime::Result<GuestIndices> {
322                         let (_, instance) = component
323                             .export_index(None, "foo:foo/multi-return")
324                             .ok_or_else(|| {
325                                 anyhow::anyhow!(
326                                     "no exported instance named `foo:foo/multi-return`"
327                                 )
328                             })?;
329                         Self::_new(|name| {
330                             component.export_index(Some(&instance), name).map(|p| p.1)
331                         })
332                     }
333                     /// This constructor is similar to [`GuestIndices::new`] except that it
334                     /// performs string lookups after instantiation time.
335                     pub fn new_instance(
336                         mut store: impl wasmtime::AsContextMut,
337                         instance: &wasmtime::component::Instance,
338                     ) -> wasmtime::Result<GuestIndices> {
339                         let instance_export = instance
340                             .get_export(&mut store, None, "foo:foo/multi-return")
341                             .ok_or_else(|| {
342                                 anyhow::anyhow!(
343                                     "no exported instance named `foo:foo/multi-return`"
344                                 )
345                             })?;
346                         Self::_new(|name| {
347                             instance.get_export(&mut store, Some(&instance_export), name)
348                         })
349                     }
350                     fn _new(
351                         mut lookup: impl FnMut(
352                             &str,
353                         ) -> Option<wasmtime::component::ComponentExportIndex>,
354                     ) -> wasmtime::Result<GuestIndices> {
355                         let mut lookup = move |name| {
356                             lookup(name)
357                                 .ok_or_else(|| {
358                                     anyhow::anyhow!(
359                                         "instance export `foo:foo/multi-return` does \
360                 not have export `{name}`"
361                                     )
362                                 })
363                         };
364                         let _ = &mut lookup;
365                         let mra = lookup("mra")?;
366                         let mrb = lookup("mrb")?;
367                         let mrc = lookup("mrc")?;
368                         let mrd = lookup("mrd")?;
369                         let mre = lookup("mre")?;
370                         Ok(GuestIndices {
371                             mra,
372                             mrb,
373                             mrc,
374                             mrd,
375                             mre,
376                         })
377                     }
378                     pub fn load(
379                         &self,
380                         mut store: impl wasmtime::AsContextMut,
381                         instance: &wasmtime::component::Instance,
382                     ) -> wasmtime::Result<Guest> {
383                         let mut store = store.as_context_mut();
384                         let _ = &mut store;
385                         let _instance = instance;
386                         let mra = *_instance
387                             .get_typed_func::<(), ()>(&mut store, &self.mra)?
388                             .func();
389                         let mrb = *_instance
390                             .get_typed_func::<(), ()>(&mut store, &self.mrb)?
391                             .func();
392                         let mrc = *_instance
393                             .get_typed_func::<(), (u32,)>(&mut store, &self.mrc)?
394                             .func();
395                         let mrd = *_instance
396                             .get_typed_func::<(), (u32,)>(&mut store, &self.mrd)?
397                             .func();
398                         let mre = *_instance
399                             .get_typed_func::<(), (u32, f32)>(&mut store, &self.mre)?
400                             .func();
401                         Ok(Guest { mra, mrb, mrc, mrd, mre })
402                     }
403                 }
404                 impl Guest {
405                     pub fn call_mra<S: wasmtime::AsContextMut>(
406                         &self,
407                         mut store: S,
408                     ) -> wasmtime::Result<()>
409                     where
410                         <S as wasmtime::AsContext>::Data: Send,
411                     {
412                         let callee = unsafe {
413                             wasmtime::component::TypedFunc::<
414                                 (),
415                                 (),
416                             >::new_unchecked(self.mra)
417                         };
418                         let () = callee.call(store.as_context_mut(), ())?;
419                         callee.post_return(store.as_context_mut())?;
420                         Ok(())
421                     }
422                     pub fn call_mrb<S: wasmtime::AsContextMut>(
423                         &self,
424                         mut store: S,
425                     ) -> wasmtime::Result<()>
426                     where
427                         <S as wasmtime::AsContext>::Data: Send,
428                     {
429                         let callee = unsafe {
430                             wasmtime::component::TypedFunc::<
431                                 (),
432                                 (),
433                             >::new_unchecked(self.mrb)
434                         };
435                         let () = callee.call(store.as_context_mut(), ())?;
436                         callee.post_return(store.as_context_mut())?;
437                         Ok(())
438                     }
439                     pub fn call_mrc<S: wasmtime::AsContextMut>(
440                         &self,
441                         mut store: S,
442                     ) -> wasmtime::Result<u32>
443                     where
444                         <S as wasmtime::AsContext>::Data: Send,
445                     {
446                         let callee = unsafe {
447                             wasmtime::component::TypedFunc::<
448                                 (),
449                                 (u32,),
450                             >::new_unchecked(self.mrc)
451                         };
452                         let (ret0,) = callee.call(store.as_context_mut(), ())?;
453                         callee.post_return(store.as_context_mut())?;
454                         Ok(ret0)
455                     }
456                     pub fn call_mrd<S: wasmtime::AsContextMut>(
457                         &self,
458                         mut store: S,
459                     ) -> wasmtime::Result<u32>
460                     where
461                         <S as wasmtime::AsContext>::Data: Send,
462                     {
463                         let callee = unsafe {
464                             wasmtime::component::TypedFunc::<
465                                 (),
466                                 (u32,),
467                             >::new_unchecked(self.mrd)
468                         };
469                         let (ret0,) = callee.call(store.as_context_mut(), ())?;
470                         callee.post_return(store.as_context_mut())?;
471                         Ok(ret0)
472                     }
473                     pub fn call_mre<S: wasmtime::AsContextMut>(
474                         &self,
475                         mut store: S,
476                     ) -> wasmtime::Result<(u32, f32)>
477                     where
478                         <S as wasmtime::AsContext>::Data: Send,
479                     {
480                         let callee = unsafe {
481                             wasmtime::component::TypedFunc::<
482                                 (),
483                                 (u32, f32),
484                             >::new_unchecked(self.mre)
485                         };
486                         let (ret0, ret1) = callee.call(store.as_context_mut(), ())?;
487                         callee.post_return(store.as_context_mut())?;
488                         Ok((ret0, ret1))
489                     }
490                 }
491             }
492         }
493     }
494 }
495