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> {
clone(&self) -> Self14     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.
new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>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     }
engine(&self) -> &wasmtime::Engine33     pub fn engine(&self) -> &wasmtime::Engine {
34         self.instance_pre.engine()
35     }
instance_pre(&self) -> &wasmtime::component::InstancePre<_T>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.
instantiate( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<TheWorld>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.
new( component: &wasmtime::component::Component, ) -> wasmtime::Result<Self>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`].
new_instance( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Self>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.
load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<TheWorld>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`].
instantiate<_T>( mut store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<TheWorld>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`].
new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<TheWorld>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         }
add_to_linker<T, U>( linker: &mut wasmtime::component::Linker<T>, get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, ) -> wasmtime::Result<()> where U: foo::foo::multi_return::Host,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         }
foo_foo_multi_return(&self) -> &exports::foo::foo::multi_return::Guest180         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 {
mra(&mut self) -> ()192                 fn mra(&mut self) -> ();
mrb(&mut self) -> ()193                 fn mrb(&mut self) -> ();
mrc(&mut self) -> u32194                 fn mrc(&mut self) -> u32;
mrd(&mut self) -> u32195                 fn mrd(&mut self) -> u32;
mre(&mut self) -> (u32, f32)196                 fn mre(&mut self) -> (u32, f32);
197             }
198             pub trait GetHost<
199                 T,
200             >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
201                 type Host: Host;
202             }
203             impl<F, T, O> GetHost<T> for F
204             where
205                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
206                 O: Host,
207             {
208                 type Host = O;
209             }
add_to_linker_get_host<T, G: for<'a> GetHost<&'a mut T, Host: Host>>( linker: &mut wasmtime::component::Linker<T>, host_getter: G, ) -> wasmtime::Result<()>210             pub fn add_to_linker_get_host<T, G: for<'a> GetHost<&'a mut T, Host: Host>>(
211                 linker: &mut wasmtime::component::Linker<T>,
212                 host_getter: G,
213             ) -> wasmtime::Result<()> {
214                 let mut inst = linker.instance("foo:foo/multi-return")?;
215                 inst.func_wrap(
216                     "mra",
217                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
218                         let host = &mut host_getter(caller.data_mut());
219                         let r = Host::mra(host);
220                         Ok(r)
221                     },
222                 )?;
223                 inst.func_wrap(
224                     "mrb",
225                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
226                         let host = &mut host_getter(caller.data_mut());
227                         let r = Host::mrb(host);
228                         Ok(r)
229                     },
230                 )?;
231                 inst.func_wrap(
232                     "mrc",
233                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
234                         let host = &mut host_getter(caller.data_mut());
235                         let r = Host::mrc(host);
236                         Ok((r,))
237                     },
238                 )?;
239                 inst.func_wrap(
240                     "mrd",
241                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
242                         let host = &mut host_getter(caller.data_mut());
243                         let r = Host::mrd(host);
244                         Ok((r,))
245                     },
246                 )?;
247                 inst.func_wrap(
248                     "mre",
249                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
250                         let host = &mut host_getter(caller.data_mut());
251                         let r = Host::mre(host);
252                         Ok(r)
253                     },
254                 )?;
255                 Ok(())
256             }
add_to_linker<T, U>( linker: &mut wasmtime::component::Linker<T>, get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, ) -> wasmtime::Result<()> where U: Host,257             pub fn add_to_linker<T, U>(
258                 linker: &mut wasmtime::component::Linker<T>,
259                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
260             ) -> wasmtime::Result<()>
261             where
262                 U: Host,
263             {
264                 add_to_linker_get_host(linker, get)
265             }
266             impl<_T: Host + ?Sized> Host for &mut _T {
mra(&mut self) -> ()267                 fn mra(&mut self) -> () {
268                     Host::mra(*self)
269                 }
mrb(&mut self) -> ()270                 fn mrb(&mut self) -> () {
271                     Host::mrb(*self)
272                 }
mrc(&mut self) -> u32273                 fn mrc(&mut self) -> u32 {
274                     Host::mrc(*self)
275                 }
mrd(&mut self) -> u32276                 fn mrd(&mut self) -> u32 {
277                     Host::mrd(*self)
278                 }
mre(&mut self) -> (u32, f32)279                 fn mre(&mut self) -> (u32, f32) {
280                     Host::mre(*self)
281                 }
282             }
283         }
284     }
285 }
286 pub mod exports {
287     pub mod foo {
288         pub mod foo {
289             #[allow(clippy::all)]
290             pub mod multi_return {
291                 #[allow(unused_imports)]
292                 use wasmtime::component::__internal::{anyhow, Box};
293                 pub struct Guest {
294                     mra: wasmtime::component::Func,
295                     mrb: wasmtime::component::Func,
296                     mrc: wasmtime::component::Func,
297                     mrd: wasmtime::component::Func,
298                     mre: wasmtime::component::Func,
299                 }
300                 #[derive(Clone)]
301                 pub struct GuestIndices {
302                     mra: wasmtime::component::ComponentExportIndex,
303                     mrb: wasmtime::component::ComponentExportIndex,
304                     mrc: wasmtime::component::ComponentExportIndex,
305                     mrd: wasmtime::component::ComponentExportIndex,
306                     mre: wasmtime::component::ComponentExportIndex,
307                 }
308                 impl GuestIndices {
309                     /// Constructor for [`GuestIndices`] which takes a
310                     /// [`Component`](wasmtime::component::Component) as input and can be executed
311                     /// before instantiation.
312                     ///
313                     /// This constructor can be used to front-load string lookups to find exports
314                     /// within a component.
new( component: &wasmtime::component::Component, ) -> wasmtime::Result<GuestIndices>315                     pub fn new(
316                         component: &wasmtime::component::Component,
317                     ) -> wasmtime::Result<GuestIndices> {
318                         let (_, instance) = component
319                             .export_index(None, "foo:foo/multi-return")
320                             .ok_or_else(|| {
321                                 wasmtime::format_err!(
322                                     "no exported instance named `foo:foo/multi-return`"
323                                 )
324                             })?;
325                         Self::_new(|name| {
326                             component.export_index(Some(&instance), name).map(|p| p.1)
327                         })
328                     }
329                     /// This constructor is similar to [`GuestIndices::new`] except that it
330                     /// performs string lookups after instantiation time.
new_instance( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<GuestIndices>331                     pub fn new_instance(
332                         mut store: impl wasmtime::AsContextMut,
333                         instance: &wasmtime::component::Instance,
334                     ) -> wasmtime::Result<GuestIndices> {
335                         let instance_export = instance
336                             .get_export(&mut store, None, "foo:foo/multi-return")
337                             .ok_or_else(|| {
338                                 wasmtime::format_err!(
339                                     "no exported instance named `foo:foo/multi-return`"
340                                 )
341                             })?;
342                         Self::_new(|name| {
343                             instance.get_export(&mut store, Some(&instance_export), name)
344                         })
345                     }
_new( mut lookup: impl FnMut( &str, ) -> Option<wasmtime::component::ComponentExportIndex>, ) -> wasmtime::Result<GuestIndices>346                     fn _new(
347                         mut lookup: impl FnMut(
348                             &str,
349                         ) -> Option<wasmtime::component::ComponentExportIndex>,
350                     ) -> wasmtime::Result<GuestIndices> {
351                         let mut lookup = move |name| {
352                             lookup(name)
353                                 .ok_or_else(|| {
354                                     wasmtime::format_err!(
355                                         "instance export `foo:foo/multi-return` does \
356                 not have export `{name}`"
357                                     )
358                                 })
359                         };
360                         let _ = &mut lookup;
361                         let mra = lookup("mra")?;
362                         let mrb = lookup("mrb")?;
363                         let mrc = lookup("mrc")?;
364                         let mrd = lookup("mrd")?;
365                         let mre = lookup("mre")?;
366                         Ok(GuestIndices {
367                             mra,
368                             mrb,
369                             mrc,
370                             mrd,
371                             mre,
372                         })
373                     }
load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Guest>374                     pub fn load(
375                         &self,
376                         mut store: impl wasmtime::AsContextMut,
377                         instance: &wasmtime::component::Instance,
378                     ) -> wasmtime::Result<Guest> {
379                         let mut store = store.as_context_mut();
380                         let _ = &mut store;
381                         let _instance = instance;
382                         let mra = *_instance
383                             .get_typed_func::<(), ()>(&mut store, &self.mra)?
384                             .func();
385                         let mrb = *_instance
386                             .get_typed_func::<(), ()>(&mut store, &self.mrb)?
387                             .func();
388                         let mrc = *_instance
389                             .get_typed_func::<(), (u32,)>(&mut store, &self.mrc)?
390                             .func();
391                         let mrd = *_instance
392                             .get_typed_func::<(), (u32,)>(&mut store, &self.mrd)?
393                             .func();
394                         let mre = *_instance
395                             .get_typed_func::<(), (u32, f32)>(&mut store, &self.mre)?
396                             .func();
397                         Ok(Guest { mra, mrb, mrc, mrd, mre })
398                     }
399                 }
400                 impl Guest {
call_mra<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<()> where <S as wasmtime::AsContext>::Data: Send,401                     pub fn call_mra<S: wasmtime::AsContextMut>(
402                         &self,
403                         mut store: S,
404                     ) -> wasmtime::Result<()>
405                     where
406                         <S as wasmtime::AsContext>::Data: Send,
407                     {
408                         let callee = unsafe {
409                             wasmtime::component::TypedFunc::<
410                                 (),
411                                 (),
412                             >::new_unchecked(self.mra)
413                         };
414                         let () = callee.call(store.as_context_mut(), ())?;
415                         callee.post_return(store.as_context_mut())?;
416                         Ok(())
417                     }
call_mrb<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<()> where <S as wasmtime::AsContext>::Data: Send,418                     pub fn call_mrb<S: wasmtime::AsContextMut>(
419                         &self,
420                         mut store: S,
421                     ) -> wasmtime::Result<()>
422                     where
423                         <S as wasmtime::AsContext>::Data: Send,
424                     {
425                         let callee = unsafe {
426                             wasmtime::component::TypedFunc::<
427                                 (),
428                                 (),
429                             >::new_unchecked(self.mrb)
430                         };
431                         let () = callee.call(store.as_context_mut(), ())?;
432                         callee.post_return(store.as_context_mut())?;
433                         Ok(())
434                     }
call_mrc<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<u32> where <S as wasmtime::AsContext>::Data: Send,435                     pub fn call_mrc<S: wasmtime::AsContextMut>(
436                         &self,
437                         mut store: S,
438                     ) -> wasmtime::Result<u32>
439                     where
440                         <S as wasmtime::AsContext>::Data: Send,
441                     {
442                         let callee = unsafe {
443                             wasmtime::component::TypedFunc::<
444                                 (),
445                                 (u32,),
446                             >::new_unchecked(self.mrc)
447                         };
448                         let (ret0,) = callee.call(store.as_context_mut(), ())?;
449                         callee.post_return(store.as_context_mut())?;
450                         Ok(ret0)
451                     }
call_mrd<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<u32> where <S as wasmtime::AsContext>::Data: Send,452                     pub fn call_mrd<S: wasmtime::AsContextMut>(
453                         &self,
454                         mut store: S,
455                     ) -> wasmtime::Result<u32>
456                     where
457                         <S as wasmtime::AsContext>::Data: Send,
458                     {
459                         let callee = unsafe {
460                             wasmtime::component::TypedFunc::<
461                                 (),
462                                 (u32,),
463                             >::new_unchecked(self.mrd)
464                         };
465                         let (ret0,) = callee.call(store.as_context_mut(), ())?;
466                         callee.post_return(store.as_context_mut())?;
467                         Ok(ret0)
468                     }
call_mre<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<(u32, f32)> where <S as wasmtime::AsContext>::Data: Send,469                     pub fn call_mre<S: wasmtime::AsContextMut>(
470                         &self,
471                         mut store: S,
472                     ) -> wasmtime::Result<(u32, f32)>
473                     where
474                         <S as wasmtime::AsContext>::Data: Send,
475                     {
476                         let callee = unsafe {
477                             wasmtime::component::TypedFunc::<
478                                 (),
479                                 (u32, f32),
480                             >::new_unchecked(self.mre)
481                         };
482                         let (ret0, ret1) = callee.call(store.as_context_mut(), ())?;
483                         callee.post_return(store.as_context_mut())?;
484                         Ok((ret0, ret1))
485                     }
486                 }
487             }
488         }
489     }
490 }
491