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