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