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::chars::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::chars::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::chars::GuestIndices::new(_instance_pre)?;
118             Ok(TheWorldIndices { interface0 })
119         }
120         /// Uses the indices stored in `self` to load an instance
121         /// of [`TheWorld`] from the instance provided.
122         ///
123         /// Note that at this time this method will additionally
124         /// perform type-checks of all exports.
load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<TheWorld>125         pub fn load(
126             &self,
127             mut store: impl wasmtime::AsContextMut,
128             instance: &wasmtime::component::Instance,
129         ) -> wasmtime::Result<TheWorld> {
130             let _ = &mut store;
131             let _instance = instance;
132             let interface0 = self.interface0.load(&mut store, &_instance)?;
133             Ok(TheWorld { interface0 })
134         }
135     }
136     impl TheWorld {
137         /// Convenience wrapper around [`TheWorldPre::new`] and
138         /// [`TheWorldPre::instantiate`].
instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<TheWorld>139         pub fn instantiate<_T>(
140             store: impl wasmtime::AsContextMut<Data = _T>,
141             component: &wasmtime::component::Component,
142             linker: &wasmtime::component::Linker<_T>,
143         ) -> wasmtime::Result<TheWorld> {
144             let pre = linker.instantiate_pre(component)?;
145             TheWorldPre::new(pre)?.instantiate(store)
146         }
147         /// Convenience wrapper around [`TheWorldIndices::new`] and
148         /// [`TheWorldIndices::load`].
new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<TheWorld>149         pub fn new(
150             mut store: impl wasmtime::AsContextMut,
151             instance: &wasmtime::component::Instance,
152         ) -> wasmtime::Result<TheWorld> {
153             let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;
154             indices.load(&mut store, instance)
155         }
156         /// Convenience wrapper around [`TheWorldPre::new`] and
157         /// [`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,158         pub async fn instantiate_async<_T>(
159             store: impl wasmtime::AsContextMut<Data = _T>,
160             component: &wasmtime::component::Component,
161             linker: &wasmtime::component::Linker<_T>,
162         ) -> wasmtime::Result<TheWorld>
163         where
164             _T: Send,
165         {
166             let pre = linker.instantiate_pre(component)?;
167             TheWorldPre::new(pre)?.instantiate_async(store).await
168         }
add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: foo::foo::chars::HostWithStore + Send, for<'a> D::Data<'a>: foo::foo::chars::Host + Send, T: 'static + Send,169         pub fn add_to_linker<T, D>(
170             linker: &mut wasmtime::component::Linker<T>,
171             host_getter: fn(&mut T) -> D::Data<'_>,
172         ) -> wasmtime::Result<()>
173         where
174             D: foo::foo::chars::HostWithStore + Send,
175             for<'a> D::Data<'a>: foo::foo::chars::Host + Send,
176             T: 'static + Send,
177         {
178             foo::foo::chars::add_to_linker::<T, D>(linker, host_getter)?;
179             Ok(())
180         }
foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest181         pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest {
182             &self.interface0
183         }
184     }
185 };
186 pub mod foo {
187     pub mod foo {
188         #[allow(clippy::all)]
189         pub mod chars {
190             #[allow(unused_imports)]
191             use wasmtime::component::__internal::Box;
192             pub trait HostWithStore: wasmtime::component::HasData + Send {}
193             impl<_T: ?Sized> HostWithStore for _T
194             where
195                 _T: wasmtime::component::HasData + Send,
196             {}
197             pub trait Host: Send {
198                 /// A function that accepts a character
take_char( &mut self, x: char, ) -> impl ::core::future::Future<Output = ()> + Send199                 fn take_char(
200                     &mut self,
201                     x: char,
202                 ) -> impl ::core::future::Future<Output = ()> + Send;
203                 /// A function that returns a character
return_char( &mut self, ) -> impl ::core::future::Future<Output = char> + Send204                 fn return_char(
205                     &mut self,
206                 ) -> impl ::core::future::Future<Output = char> + Send;
207             }
208             impl<_T: Host + ?Sized + Send> Host for &mut _T {
209                 /// A function that accepts a character
take_char( &mut self, x: char, ) -> impl ::core::future::Future<Output = ()> + Send210                 fn take_char(
211                     &mut self,
212                     x: char,
213                 ) -> impl ::core::future::Future<Output = ()> + Send {
214                     async move { Host::take_char(*self, x).await }
215                 }
216                 /// A function that returns a character
return_char( &mut self, ) -> impl ::core::future::Future<Output = char> + Send217                 fn return_char(
218                     &mut self,
219                 ) -> impl ::core::future::Future<Output = char> + Send {
220                     async move { Host::return_char(*self).await }
221                 }
222             }
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,223             pub fn add_to_linker<T, D>(
224                 linker: &mut wasmtime::component::Linker<T>,
225                 host_getter: fn(&mut T) -> D::Data<'_>,
226             ) -> wasmtime::Result<()>
227             where
228                 D: HostWithStore,
229                 for<'a> D::Data<'a>: Host,
230                 T: 'static + Send,
231             {
232                 let mut inst = linker.instance("foo:foo/chars")?;
233                 inst.func_wrap_async(
234                     "take-char",
235                     move |
236                         mut caller: wasmtime::StoreContextMut<'_, T>,
237                         (arg0,): (char,)|
238                     {
239                         use tracing::Instrument;
240                         let span = tracing::span!(
241                             tracing::Level::TRACE, "wit-bindgen import", module =
242                             "chars", function = "take-char",
243                         );
244                         wasmtime::component::__internal::Box::new(
245                             async move {
246                                 tracing::event!(
247                                     tracing::Level::TRACE, x = tracing::field::debug(& arg0),
248                                     "call"
249                                 );
250                                 let host = &mut host_getter(caller.data_mut());
251                                 let r = Host::take_char(host, arg0).await;
252                                 tracing::event!(
253                                     tracing::Level::TRACE, result = tracing::field::debug(& r),
254                                     "return"
255                                 );
256                                 Ok(r)
257                             }
258                                 .instrument(span),
259                         )
260                     },
261                 )?;
262                 inst.func_wrap_async(
263                     "return-char",
264                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
265                         use tracing::Instrument;
266                         let span = tracing::span!(
267                             tracing::Level::TRACE, "wit-bindgen import", module =
268                             "chars", function = "return-char",
269                         );
270                         wasmtime::component::__internal::Box::new(
271                             async move {
272                                 tracing::event!(tracing::Level::TRACE, "call");
273                                 let host = &mut host_getter(caller.data_mut());
274                                 let r = Host::return_char(host).await;
275                                 tracing::event!(
276                                     tracing::Level::TRACE, result = tracing::field::debug(& r),
277                                     "return"
278                                 );
279                                 Ok((r,))
280                             }
281                                 .instrument(span),
282                         )
283                     },
284                 )?;
285                 Ok(())
286             }
287         }
288     }
289 }
290 pub mod exports {
291     pub mod foo {
292         pub mod foo {
293             #[allow(clippy::all)]
294             pub mod chars {
295                 #[allow(unused_imports)]
296                 use wasmtime::component::__internal::Box;
297                 #[derive(Clone)]
298                 pub struct Guest {
299                     take_char: wasmtime::component::Func,
300                     return_char: wasmtime::component::Func,
301                 }
302                 #[derive(Clone)]
303                 pub struct GuestIndices {
304                     take_char: wasmtime::component::ComponentExportIndex,
305                     return_char: wasmtime::component::ComponentExportIndex,
306                 }
307                 impl GuestIndices {
308                     /// Constructor for [`GuestIndices`] which takes a
309                     /// [`Component`](wasmtime::component::Component) as input and can be executed
310                     /// before instantiation.
311                     ///
312                     /// This constructor can be used to front-load string lookups to find exports
313                     /// within a component.
new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<GuestIndices>314                     pub fn new<_T>(
315                         _instance_pre: &wasmtime::component::InstancePre<_T>,
316                     ) -> wasmtime::Result<GuestIndices> {
317                         let instance = _instance_pre
318                             .component()
319                             .get_export_index(None, "foo:foo/chars")
320                             .ok_or_else(|| {
321                                 wasmtime::format_err!(
322                                     "no exported instance named `foo:foo/chars`"
323                                 )
324                             })?;
325                         let mut lookup = move |name| {
326                             _instance_pre
327                                 .component()
328                                 .get_export_index(Some(&instance), name)
329                                 .ok_or_else(|| {
330                                     wasmtime::format_err!(
331                                         "instance export `foo:foo/chars` does \
332                     not have export `{name}`"
333                                     )
334                                 })
335                         };
336                         let _ = &mut lookup;
337                         let take_char = lookup("take-char")?;
338                         let return_char = lookup("return-char")?;
339                         Ok(GuestIndices {
340                             take_char,
341                             return_char,
342                         })
343                     }
load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Guest>344                     pub fn load(
345                         &self,
346                         mut store: impl wasmtime::AsContextMut,
347                         instance: &wasmtime::component::Instance,
348                     ) -> wasmtime::Result<Guest> {
349                         let _instance = instance;
350                         let _instance_pre = _instance.instance_pre(&store);
351                         let _instance_type = _instance_pre.instance_type();
352                         let mut store = store.as_context_mut();
353                         let _ = &mut store;
354                         let take_char = *_instance
355                             .get_typed_func::<(char,), ()>(&mut store, &self.take_char)?
356                             .func();
357                         let return_char = *_instance
358                             .get_typed_func::<
359                                 (),
360                                 (char,),
361                             >(&mut store, &self.return_char)?
362                             .func();
363                         Ok(Guest { take_char, return_char })
364                     }
365                 }
366                 impl Guest {
367                     /// A function that accepts a character
call_take_char<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: char, ) -> wasmtime::Result<()> where <S as wasmtime::AsContext>::Data: Send,368                     pub async fn call_take_char<S: wasmtime::AsContextMut>(
369                         &self,
370                         mut store: S,
371                         arg0: char,
372                     ) -> wasmtime::Result<()>
373                     where
374                         <S as wasmtime::AsContext>::Data: Send,
375                     {
376                         use tracing::Instrument;
377                         let span = tracing::span!(
378                             tracing::Level::TRACE, "wit-bindgen export", module =
379                             "foo:foo/chars", function = "take-char",
380                         );
381                         let callee = unsafe {
382                             wasmtime::component::TypedFunc::<
383                                 (char,),
384                                 (),
385                             >::new_unchecked(self.take_char)
386                         };
387                         let () = callee
388                             .call_async(store.as_context_mut(), (arg0,))
389                             .instrument(span.clone())
390                             .await?;
391                         Ok(())
392                     }
393                     /// A function that returns a character
call_return_char<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<char> where <S as wasmtime::AsContext>::Data: Send,394                     pub async fn call_return_char<S: wasmtime::AsContextMut>(
395                         &self,
396                         mut store: S,
397                     ) -> wasmtime::Result<char>
398                     where
399                         <S as wasmtime::AsContext>::Data: Send,
400                     {
401                         use tracing::Instrument;
402                         let span = tracing::span!(
403                             tracing::Level::TRACE, "wit-bindgen export", module =
404                             "foo:foo/chars", function = "return-char",
405                         );
406                         let callee = unsafe {
407                             wasmtime::component::TypedFunc::<
408                                 (),
409                                 (char,),
410                             >::new_unchecked(self.return_char)
411                         };
412                         let (ret0,) = callee
413                             .call_async(store.as_context_mut(), ())
414                             .instrument(span.clone())
415                             .await?;
416                         Ok(ret0)
417                     }
418                 }
419             }
420         }
421     }
422 }
423