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> {
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: '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.
27     pub fn new(
28         instance_pre: wasmtime::component::InstancePre<_T>,
29     ) -> wasmtime::Result<Self> {
30         let indices = TheWorldIndices::new(&instance_pre)?;
31         Ok(Self { instance_pre, indices })
32     }
33     pub fn engine(&self) -> &wasmtime::Engine {
34         self.instance_pre.engine()
35     }
36     pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {
37         &self.instance_pre
38     }
39     /// Instantiates a new instance of [`TheWorld`] within the
40     /// `store` provided.
41     ///
42     /// This function will use `self` as the pre-instantiated
43     /// instance to perform instantiation. Afterwards the preloaded
44     /// indices in `self` are used to lookup all exports on the
45     /// resulting instance.
46     pub async fn instantiate_async(
47         &self,
48         mut store: impl wasmtime::AsContextMut<Data = _T>,
49     ) -> wasmtime::Result<TheWorld>
50     where
51         _T: Send,
52     {
53         let mut store = store.as_context_mut();
54         let instance = self.instance_pre.instantiate_async(&mut store).await?;
55         self.indices.load(&mut store, &instance)
56     }
57 }
58 /// Auto-generated bindings for index of the exports of
59 /// `the-world`.
60 ///
61 /// This is an implementation detail of [`TheWorldPre`] and can
62 /// be constructed if needed as well.
63 ///
64 /// For more information see [`TheWorld`] as well.
65 #[derive(Clone)]
66 pub struct TheWorldIndices {
67     interface0: exports::foo::foo::strings::GuestIndices,
68 }
69 /// Auto-generated bindings for an instance a component which
70 /// implements the world `the-world`.
71 ///
72 /// This structure can be created through a number of means
73 /// depending on your requirements and what you have on hand:
74 ///
75 /// * The most convenient way is to use
76 ///   [`TheWorld::instantiate_async`] which only needs a
77 ///   [`Store`], [`Component`], and [`Linker`].
78 ///
79 /// * Alternatively you can create a [`TheWorldPre`] ahead of
80 ///   time with a [`Component`] to front-load string lookups
81 ///   of exports once instead of per-instantiation. This
82 ///   method then uses [`TheWorldPre::instantiate_async`] to
83 ///   create a [`TheWorld`].
84 ///
85 /// * If you've instantiated the instance yourself already
86 ///   then you can use [`TheWorld::new`].
87 ///
88 /// These methods are all equivalent to one another and move
89 /// around the tradeoff of what work is performed when.
90 ///
91 /// [`Store`]: wasmtime::Store
92 /// [`Component`]: wasmtime::component::Component
93 /// [`Linker`]: wasmtime::component::Linker
94 pub struct TheWorld {
95     interface0: exports::foo::foo::strings::Guest,
96 }
97 const _: () = {
98     #[allow(unused_imports)]
99     use wasmtime::component::__internal::anyhow;
100     impl TheWorldIndices {
101         /// Creates a new copy of `TheWorldIndices` bindings which can then
102         /// be used to instantiate into a particular store.
103         ///
104         /// This method may fail if the component does not have the
105         /// required exports.
106         pub fn new<_T>(
107             _instance_pre: &wasmtime::component::InstancePre<_T>,
108         ) -> wasmtime::Result<Self> {
109             let _component = _instance_pre.component();
110             let _instance_type = _instance_pre.instance_type();
111             let interface0 = exports::foo::foo::strings::GuestIndices::new(
112                 _instance_pre,
113             )?;
114             Ok(TheWorldIndices { interface0 })
115         }
116         /// Uses the indices stored in `self` to load an instance
117         /// of [`TheWorld`] from the instance provided.
118         ///
119         /// Note that at this time this method will additionally
120         /// perform type-checks of all exports.
121         pub fn load(
122             &self,
123             mut store: impl wasmtime::AsContextMut,
124             instance: &wasmtime::component::Instance,
125         ) -> wasmtime::Result<TheWorld> {
126             let _ = &mut store;
127             let _instance = instance;
128             let interface0 = self.interface0.load(&mut store, &_instance)?;
129             Ok(TheWorld { interface0 })
130         }
131     }
132     impl TheWorld {
133         /// Convenience wrapper around [`TheWorldPre::new`] and
134         /// [`TheWorldPre::instantiate_async`].
135         pub async fn instantiate_async<_T>(
136             store: impl wasmtime::AsContextMut<Data = _T>,
137             component: &wasmtime::component::Component,
138             linker: &wasmtime::component::Linker<_T>,
139         ) -> wasmtime::Result<TheWorld>
140         where
141             _T: Send,
142         {
143             let pre = linker.instantiate_pre(component)?;
144             TheWorldPre::new(pre)?.instantiate_async(store).await
145         }
146         /// Convenience wrapper around [`TheWorldIndices::new`] and
147         /// [`TheWorldIndices::load`].
148         pub fn new(
149             mut store: impl wasmtime::AsContextMut,
150             instance: &wasmtime::component::Instance,
151         ) -> wasmtime::Result<TheWorld> {
152             let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;
153             indices.load(&mut store, instance)
154         }
155         pub fn add_to_linker<T, D>(
156             linker: &mut wasmtime::component::Linker<T>,
157             host_getter: fn(&mut T) -> D::Data<'_>,
158         ) -> wasmtime::Result<()>
159         where
160             D: foo::foo::strings::HostConcurrent + Send,
161             for<'a> D::Data<'a>: foo::foo::strings::Host + Send,
162             T: 'static + Send,
163         {
164             foo::foo::strings::add_to_linker::<T, D>(linker, host_getter)?;
165             Ok(())
166         }
167         pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest {
168             &self.interface0
169         }
170     }
171 };
172 pub mod foo {
173     pub mod foo {
174         #[allow(clippy::all)]
175         pub mod strings {
176             #[allow(unused_imports)]
177             use wasmtime::component::__internal::{anyhow, Box};
178             #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
179             pub trait HostConcurrent: wasmtime::component::HasData + Send {
180                 fn a<T: 'static>(
181                     accessor: &mut wasmtime::component::Accessor<T, Self>,
182                     x: wasmtime::component::__internal::String,
183                 ) -> impl ::core::future::Future<Output = ()> + Send
184                 where
185                     Self: Sized;
186                 fn b<T: 'static>(
187                     accessor: &mut wasmtime::component::Accessor<T, Self>,
188                 ) -> impl ::core::future::Future<
189                     Output = wasmtime::component::__internal::String,
190                 > + Send
191                 where
192                     Self: Sized;
193                 fn c<T: 'static>(
194                     accessor: &mut wasmtime::component::Accessor<T, Self>,
195                     a: wasmtime::component::__internal::String,
196                     b: wasmtime::component::__internal::String,
197                 ) -> impl ::core::future::Future<
198                     Output = wasmtime::component::__internal::String,
199                 > + Send
200                 where
201                     Self: Sized;
202             }
203             #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
204             pub trait Host: Send {}
205             impl<_T: Host + ?Sized + Send> Host for &mut _T {}
206             pub fn add_to_linker<T, D>(
207                 linker: &mut wasmtime::component::Linker<T>,
208                 host_getter: fn(&mut T) -> D::Data<'_>,
209             ) -> wasmtime::Result<()>
210             where
211                 D: HostConcurrent,
212                 for<'a> D::Data<'a>: Host,
213                 T: 'static + Send,
214             {
215                 let mut inst = linker.instance("foo:foo/strings")?;
216                 inst.func_wrap_concurrent(
217                     "a",
218                     move |
219                         caller: &mut wasmtime::component::Accessor<T>,
220                         (arg0,): (wasmtime::component::__internal::String,)|
221                     {
222                         wasmtime::component::__internal::Box::pin(async move {
223                             let accessor = &mut unsafe { caller.with_data(host_getter) };
224                             let r = <D as HostConcurrent>::a(accessor, arg0).await;
225                             Ok(r)
226                         })
227                     },
228                 )?;
229                 inst.func_wrap_concurrent(
230                     "b",
231                     move |caller: &mut wasmtime::component::Accessor<T>, (): ()| {
232                         wasmtime::component::__internal::Box::pin(async move {
233                             let accessor = &mut unsafe { caller.with_data(host_getter) };
234                             let r = <D as HostConcurrent>::b(accessor).await;
235                             Ok((r,))
236                         })
237                     },
238                 )?;
239                 inst.func_wrap_concurrent(
240                     "c",
241                     move |
242                         caller: &mut wasmtime::component::Accessor<T>,
243                         (
244                             arg0,
245                             arg1,
246                         ): (
247                             wasmtime::component::__internal::String,
248                             wasmtime::component::__internal::String,
249                         )|
250                     {
251                         wasmtime::component::__internal::Box::pin(async move {
252                             let accessor = &mut unsafe { caller.with_data(host_getter) };
253                             let r = <D as HostConcurrent>::c(accessor, arg0, arg1).await;
254                             Ok((r,))
255                         })
256                     },
257                 )?;
258                 Ok(())
259             }
260         }
261     }
262 }
263 pub mod exports {
264     pub mod foo {
265         pub mod foo {
266             #[allow(clippy::all)]
267             pub mod strings {
268                 #[allow(unused_imports)]
269                 use wasmtime::component::__internal::{anyhow, Box};
270                 pub struct Guest {
271                     a: wasmtime::component::Func,
272                     b: wasmtime::component::Func,
273                     c: wasmtime::component::Func,
274                 }
275                 #[derive(Clone)]
276                 pub struct GuestIndices {
277                     a: wasmtime::component::ComponentExportIndex,
278                     b: wasmtime::component::ComponentExportIndex,
279                     c: wasmtime::component::ComponentExportIndex,
280                 }
281                 impl GuestIndices {
282                     /// Constructor for [`GuestIndices`] which takes a
283                     /// [`Component`](wasmtime::component::Component) as input and can be executed
284                     /// before instantiation.
285                     ///
286                     /// This constructor can be used to front-load string lookups to find exports
287                     /// within a component.
288                     pub fn new<_T>(
289                         _instance_pre: &wasmtime::component::InstancePre<_T>,
290                     ) -> wasmtime::Result<GuestIndices> {
291                         let instance = _instance_pre
292                             .component()
293                             .get_export_index(None, "foo:foo/strings")
294                             .ok_or_else(|| {
295                                 anyhow::anyhow!(
296                                     "no exported instance named `foo:foo/strings`"
297                                 )
298                             })?;
299                         let mut lookup = move |name| {
300                             _instance_pre
301                                 .component()
302                                 .get_export_index(Some(&instance), name)
303                                 .ok_or_else(|| {
304                                     anyhow::anyhow!(
305                                         "instance export `foo:foo/strings` does \
306                 not have export `{name}`"
307                                     )
308                                 })
309                         };
310                         let _ = &mut lookup;
311                         let a = lookup("a")?;
312                         let b = lookup("b")?;
313                         let c = lookup("c")?;
314                         Ok(GuestIndices { a, b, c })
315                     }
316                     pub fn load(
317                         &self,
318                         mut store: impl wasmtime::AsContextMut,
319                         instance: &wasmtime::component::Instance,
320                     ) -> wasmtime::Result<Guest> {
321                         let _instance = instance;
322                         let _instance_pre = _instance.instance_pre(&store);
323                         let _instance_type = _instance_pre.instance_type();
324                         let mut store = store.as_context_mut();
325                         let _ = &mut store;
326                         let a = *_instance
327                             .get_typed_func::<(&str,), ()>(&mut store, &self.a)?
328                             .func();
329                         let b = *_instance
330                             .get_typed_func::<
331                                 (),
332                                 (wasmtime::component::__internal::String,),
333                             >(&mut store, &self.b)?
334                             .func();
335                         let c = *_instance
336                             .get_typed_func::<
337                                 (&str, &str),
338                                 (wasmtime::component::__internal::String,),
339                             >(&mut store, &self.c)?
340                             .func();
341                         Ok(Guest { a, b, c })
342                     }
343                 }
344                 impl Guest {
345                     pub fn call_a<S: wasmtime::AsContextMut>(
346                         &self,
347                         mut store: S,
348                         arg0: wasmtime::component::__internal::String,
349                     ) -> impl wasmtime::component::__internal::Future<
350                         Output = wasmtime::Result<()>,
351                     > + Send + 'static + use<S>
352                     where
353                         <S as wasmtime::AsContext>::Data: Send + 'static,
354                     {
355                         let callee = unsafe {
356                             wasmtime::component::TypedFunc::<
357                                 (wasmtime::component::__internal::String,),
358                                 (),
359                             >::new_unchecked(self.a)
360                         };
361                         callee.call_concurrent(store.as_context_mut(), (arg0,))
362                     }
363                     pub fn call_b<S: wasmtime::AsContextMut>(
364                         &self,
365                         mut store: S,
366                     ) -> impl wasmtime::component::__internal::Future<
367                         Output = wasmtime::Result<
368                             wasmtime::component::__internal::String,
369                         >,
370                     > + Send + 'static + use<S>
371                     where
372                         <S as wasmtime::AsContext>::Data: Send + 'static,
373                     {
374                         let callee = unsafe {
375                             wasmtime::component::TypedFunc::<
376                                 (),
377                                 (wasmtime::component::__internal::String,),
378                             >::new_unchecked(self.b)
379                         };
380                         wasmtime::component::__internal::FutureExt::map(
381                             callee.call_concurrent(store.as_context_mut(), ()),
382                             |v| v.map(|(v,)| v),
383                         )
384                     }
385                     pub fn call_c<S: wasmtime::AsContextMut>(
386                         &self,
387                         mut store: S,
388                         arg0: wasmtime::component::__internal::String,
389                         arg1: wasmtime::component::__internal::String,
390                     ) -> impl wasmtime::component::__internal::Future<
391                         Output = wasmtime::Result<
392                             wasmtime::component::__internal::String,
393                         >,
394                     > + Send + 'static + use<S>
395                     where
396                         <S as wasmtime::AsContext>::Data: Send + 'static,
397                     {
398                         let callee = unsafe {
399                             wasmtime::component::TypedFunc::<
400                                 (
401                                     wasmtime::component::__internal::String,
402                                     wasmtime::component::__internal::String,
403                                 ),
404                                 (wasmtime::component::__internal::String,),
405                             >::new_unchecked(self.c)
406                         };
407                         wasmtime::component::__internal::FutureExt::map(
408                             callee.call_concurrent(store.as_context_mut(), (arg0, arg1)),
409                             |v| v.map(|(v,)| v),
410                         )
411                     }
412                 }
413             }
414         }
415     }
416 }
417