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::strings::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::strings::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::strings::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::strings::HostWithStore + Send, for<'a> D::Data<'a>: foo::foo::strings::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::strings::HostWithStore + Send,
177             for<'a> D::Data<'a>: foo::foo::strings::Host + Send,
178             T: 'static + Send,
179         {
180             foo::foo::strings::add_to_linker::<T, D>(linker, host_getter)?;
181             Ok(())
182         }
foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest183         pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest {
184             &self.interface0
185         }
186     }
187 };
188 pub mod foo {
189     pub mod foo {
190         #[allow(clippy::all)]
191         pub mod strings {
192             #[allow(unused_imports)]
193             use wasmtime::component::__internal::Box;
194             pub trait HostWithStore: wasmtime::component::HasData + Send {
a<T: Send>( accessor: &wasmtime::component::Accessor<T, Self>, x: wasmtime::component::__internal::String, ) -> impl ::core::future::Future<Output = ()> + Send195                 fn a<T: Send>(
196                     accessor: &wasmtime::component::Accessor<T, Self>,
197                     x: wasmtime::component::__internal::String,
198                 ) -> impl ::core::future::Future<Output = ()> + Send;
b<T: Send>( accessor: &wasmtime::component::Accessor<T, Self>, ) -> impl ::core::future::Future< Output = wasmtime::component::__internal::String, > + Send199                 fn b<T: Send>(
200                     accessor: &wasmtime::component::Accessor<T, Self>,
201                 ) -> impl ::core::future::Future<
202                     Output = wasmtime::component::__internal::String,
203                 > + Send;
c<T: Send>( accessor: &wasmtime::component::Accessor<T, Self>, a: wasmtime::component::__internal::String, b: wasmtime::component::__internal::String, ) -> impl ::core::future::Future< Output = wasmtime::component::__internal::String, > + Send204                 fn c<T: Send>(
205                     accessor: &wasmtime::component::Accessor<T, Self>,
206                     a: wasmtime::component::__internal::String,
207                     b: wasmtime::component::__internal::String,
208                 ) -> impl ::core::future::Future<
209                     Output = wasmtime::component::__internal::String,
210                 > + Send;
211             }
212             pub trait Host: Send {}
213             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,214             pub fn add_to_linker<T, D>(
215                 linker: &mut wasmtime::component::Linker<T>,
216                 host_getter: fn(&mut T) -> D::Data<'_>,
217             ) -> wasmtime::Result<()>
218             where
219                 D: HostWithStore,
220                 for<'a> D::Data<'a>: Host,
221                 T: 'static + Send,
222             {
223                 let mut inst = linker.instance("foo:foo/strings")?;
224                 inst.func_wrap_concurrent(
225                     "a",
226                     move |
227                         caller: &wasmtime::component::Accessor<T>,
228                         (arg0,): (wasmtime::component::__internal::String,)|
229                     {
230                         wasmtime::component::__internal::Box::pin(async move {
231                             let host = &caller.with_getter(host_getter);
232                             let r = <D as HostWithStore>::a(host, arg0).await;
233                             Ok(r)
234                         })
235                     },
236                 )?;
237                 inst.func_wrap_concurrent(
238                     "b",
239                     move |caller: &wasmtime::component::Accessor<T>, (): ()| {
240                         wasmtime::component::__internal::Box::pin(async move {
241                             let host = &caller.with_getter(host_getter);
242                             let r = <D as HostWithStore>::b(host).await;
243                             Ok((r,))
244                         })
245                     },
246                 )?;
247                 inst.func_wrap_concurrent(
248                     "c",
249                     move |
250                         caller: &wasmtime::component::Accessor<T>,
251                         (
252                             arg0,
253                             arg1,
254                         ): (
255                             wasmtime::component::__internal::String,
256                             wasmtime::component::__internal::String,
257                         )|
258                     {
259                         wasmtime::component::__internal::Box::pin(async move {
260                             let host = &caller.with_getter(host_getter);
261                             let r = <D as HostWithStore>::c(host, arg0, arg1).await;
262                             Ok((r,))
263                         })
264                     },
265                 )?;
266                 Ok(())
267             }
268         }
269     }
270 }
271 pub mod exports {
272     pub mod foo {
273         pub mod foo {
274             #[allow(clippy::all)]
275             pub mod strings {
276                 #[allow(unused_imports)]
277                 use wasmtime::component::__internal::Box;
278                 #[derive(Clone)]
279                 pub struct Guest {
280                     a: wasmtime::component::Func,
281                     b: wasmtime::component::Func,
282                     c: wasmtime::component::Func,
283                 }
284                 #[derive(Clone)]
285                 pub struct GuestIndices {
286                     a: wasmtime::component::ComponentExportIndex,
287                     b: wasmtime::component::ComponentExportIndex,
288                     c: wasmtime::component::ComponentExportIndex,
289                 }
290                 impl GuestIndices {
291                     /// Constructor for [`GuestIndices`] which takes a
292                     /// [`Component`](wasmtime::component::Component) as input and can be executed
293                     /// before instantiation.
294                     ///
295                     /// This constructor can be used to front-load string lookups to find exports
296                     /// within a component.
new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<GuestIndices>297                     pub fn new<_T>(
298                         _instance_pre: &wasmtime::component::InstancePre<_T>,
299                     ) -> wasmtime::Result<GuestIndices> {
300                         let instance = _instance_pre
301                             .component()
302                             .get_export_index(None, "foo:foo/strings")
303                             .ok_or_else(|| {
304                                 wasmtime::format_err!(
305                                     "no exported instance named `foo:foo/strings`"
306                                 )
307                             })?;
308                         let mut lookup = move |name| {
309                             _instance_pre
310                                 .component()
311                                 .get_export_index(Some(&instance), name)
312                                 .ok_or_else(|| {
313                                     wasmtime::format_err!(
314                                         "instance export `foo:foo/strings` does \
315                 not have export `{name}`"
316                                     )
317                                 })
318                         };
319                         let _ = &mut lookup;
320                         let a = lookup("a")?;
321                         let b = lookup("b")?;
322                         let c = lookup("c")?;
323                         Ok(GuestIndices { a, b, c })
324                     }
load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Guest>325                     pub fn load(
326                         &self,
327                         mut store: impl wasmtime::AsContextMut,
328                         instance: &wasmtime::component::Instance,
329                     ) -> wasmtime::Result<Guest> {
330                         let _instance = instance;
331                         let _instance_pre = _instance.instance_pre(&store);
332                         let _instance_type = _instance_pre.instance_type();
333                         let mut store = store.as_context_mut();
334                         let _ = &mut store;
335                         let a = *_instance
336                             .get_typed_func::<(&str,), ()>(&mut store, &self.a)?
337                             .func();
338                         let b = *_instance
339                             .get_typed_func::<
340                                 (),
341                                 (wasmtime::component::__internal::String,),
342                             >(&mut store, &self.b)?
343                             .func();
344                         let c = *_instance
345                             .get_typed_func::<
346                                 (&str, &str),
347                                 (wasmtime::component::__internal::String,),
348                             >(&mut store, &self.c)?
349                             .func();
350                         Ok(Guest { a, b, c })
351                     }
352                 }
353                 impl Guest {
call_a<_T, _D>( &self, accessor: &wasmtime::component::Accessor<_T, _D>, arg0: wasmtime::component::__internal::String, ) -> wasmtime::Result<()> where _T: Send, _D: wasmtime::component::HasData,354                     pub async fn call_a<_T, _D>(
355                         &self,
356                         accessor: &wasmtime::component::Accessor<_T, _D>,
357                         arg0: wasmtime::component::__internal::String,
358                     ) -> wasmtime::Result<()>
359                     where
360                         _T: Send,
361                         _D: wasmtime::component::HasData,
362                     {
363                         let callee = unsafe {
364                             wasmtime::component::TypedFunc::<
365                                 (wasmtime::component::__internal::String,),
366                                 (),
367                             >::new_unchecked(self.a)
368                         };
369                         let () = callee.call_concurrent(accessor, (arg0,)).await?;
370                         Ok(())
371                     }
call_b<_T, _D>( &self, accessor: &wasmtime::component::Accessor<_T, _D>, ) -> wasmtime::Result<wasmtime::component::__internal::String> where _T: Send, _D: wasmtime::component::HasData,372                     pub async fn call_b<_T, _D>(
373                         &self,
374                         accessor: &wasmtime::component::Accessor<_T, _D>,
375                     ) -> wasmtime::Result<wasmtime::component::__internal::String>
376                     where
377                         _T: Send,
378                         _D: wasmtime::component::HasData,
379                     {
380                         let callee = unsafe {
381                             wasmtime::component::TypedFunc::<
382                                 (),
383                                 (wasmtime::component::__internal::String,),
384                             >::new_unchecked(self.b)
385                         };
386                         let (ret0,) = callee.call_concurrent(accessor, ()).await?;
387                         Ok(ret0)
388                     }
call_c<_T, _D>( &self, accessor: &wasmtime::component::Accessor<_T, _D>, arg0: wasmtime::component::__internal::String, arg1: wasmtime::component::__internal::String, ) -> wasmtime::Result<wasmtime::component::__internal::String> where _T: Send, _D: wasmtime::component::HasData,389                     pub async fn call_c<_T, _D>(
390                         &self,
391                         accessor: &wasmtime::component::Accessor<_T, _D>,
392                         arg0: wasmtime::component::__internal::String,
393                         arg1: wasmtime::component::__internal::String,
394                     ) -> wasmtime::Result<wasmtime::component::__internal::String>
395                     where
396                         _T: Send,
397                         _D: wasmtime::component::HasData,
398                     {
399                         let callee = unsafe {
400                             wasmtime::component::TypedFunc::<
401                                 (
402                                     wasmtime::component::__internal::String,
403                                     wasmtime::component::__internal::String,
404                                 ),
405                                 (wasmtime::component::__internal::String,),
406                             >::new_unchecked(self.c)
407                         };
408                         let (ret0,) = callee
409                             .call_concurrent(accessor, (arg0, arg1))
410                             .await?;
411                         Ok(ret0)
412                     }
413                 }
414             }
415         }
416     }
417 }
418