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 pub struct TheWorldPre<T> {
8     instance_pre: wasmtime::component::InstancePre<T>,
9     interface0: exports::foo::foo::multi_return::GuestPre,
10 }
11 impl<T> Clone for TheWorldPre<T> {
12     fn clone(&self) -> Self {
13         Self {
14             instance_pre: self.instance_pre.clone(),
15             interface0: self.interface0.clone(),
16         }
17     }
18 }
19 /// Auto-generated bindings for an instance a component which
20 /// implements the world `the-world`.
21 ///
22 /// This structure is created through either
23 /// [`TheWorld::instantiate`] or by first creating
24 /// a [`TheWorldPre`] followed by using
25 /// [`TheWorldPre::instantiate`].
26 pub struct TheWorld {
27     interface0: exports::foo::foo::multi_return::Guest,
28 }
29 const _: () = {
30     #[allow(unused_imports)]
31     use wasmtime::component::__internal::anyhow;
32     impl<_T> TheWorldPre<_T> {
33         /// Creates a new copy of `TheWorldPre` bindings which can then
34         /// be used to instantiate into a particular store.
35         ///
36         /// This method may fail if the component behind `instance_pre`
37         /// does not have the required exports.
38         pub fn new(
39             instance_pre: wasmtime::component::InstancePre<_T>,
40         ) -> wasmtime::Result<Self> {
41             let _component = instance_pre.component();
42             let interface0 = exports::foo::foo::multi_return::GuestPre::new(_component)?;
43             Ok(TheWorldPre {
44                 instance_pre,
45                 interface0,
46             })
47         }
48         /// Instantiates a new instance of [`TheWorld`] within the
49         /// `store` provided.
50         ///
51         /// This function will use `self` as the pre-instantiated
52         /// instance to perform instantiation. Afterwards the preloaded
53         /// indices in `self` are used to lookup all exports on the
54         /// resulting instance.
55         pub fn instantiate(
56             &self,
57             mut store: impl wasmtime::AsContextMut<Data = _T>,
58         ) -> wasmtime::Result<TheWorld> {
59             let mut store = store.as_context_mut();
60             let _instance = self.instance_pre.instantiate(&mut store)?;
61             let interface0 = self.interface0.load(&mut store, &_instance)?;
62             Ok(TheWorld { interface0 })
63         }
64         pub fn engine(&self) -> &wasmtime::Engine {
65             self.instance_pre.engine()
66         }
67         pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {
68             &self.instance_pre
69         }
70     }
71     impl TheWorld {
72         /// Convenience wrapper around [`TheWorldPre::new`] and
73         /// [`TheWorldPre::instantiate`].
74         pub fn instantiate<_T>(
75             mut store: impl wasmtime::AsContextMut<Data = _T>,
76             component: &wasmtime::component::Component,
77             linker: &wasmtime::component::Linker<_T>,
78         ) -> wasmtime::Result<TheWorld> {
79             let pre = linker.instantiate_pre(component)?;
80             TheWorldPre::new(pre)?.instantiate(store)
81         }
82         pub fn add_to_linker<T, U>(
83             linker: &mut wasmtime::component::Linker<T>,
84             get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
85         ) -> wasmtime::Result<()>
86         where
87             U: foo::foo::multi_return::Host,
88         {
89             foo::foo::multi_return::add_to_linker(linker, get)?;
90             Ok(())
91         }
92         pub fn foo_foo_multi_return(&self) -> &exports::foo::foo::multi_return::Guest {
93             &self.interface0
94         }
95     }
96 };
97 pub mod foo {
98     pub mod foo {
99         #[allow(clippy::all)]
100         pub mod multi_return {
101             #[allow(unused_imports)]
102             use wasmtime::component::__internal::anyhow;
103             pub trait Host {
104                 fn mra(&mut self) -> ();
105                 fn mrb(&mut self) -> ();
106                 fn mrc(&mut self) -> u32;
107                 fn mrd(&mut self) -> u32;
108                 fn mre(&mut self) -> (u32, f32);
109             }
110             pub trait GetHost<
111                 T,
112             >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
113                 type Host: Host;
114             }
115             impl<F, T, O> GetHost<T> for F
116             where
117                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
118                 O: Host,
119             {
120                 type Host = O;
121             }
122             pub fn add_to_linker_get_host<T>(
123                 linker: &mut wasmtime::component::Linker<T>,
124                 host_getter: impl for<'a> GetHost<&'a mut T>,
125             ) -> wasmtime::Result<()> {
126                 let mut inst = linker.instance("foo:foo/multi-return")?;
127                 inst.func_wrap(
128                     "mra",
129                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
130                         let host = &mut host_getter(caller.data_mut());
131                         let r = Host::mra(host);
132                         Ok(r)
133                     },
134                 )?;
135                 inst.func_wrap(
136                     "mrb",
137                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
138                         let host = &mut host_getter(caller.data_mut());
139                         let r = Host::mrb(host);
140                         Ok(r)
141                     },
142                 )?;
143                 inst.func_wrap(
144                     "mrc",
145                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
146                         let host = &mut host_getter(caller.data_mut());
147                         let r = Host::mrc(host);
148                         Ok((r,))
149                     },
150                 )?;
151                 inst.func_wrap(
152                     "mrd",
153                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
154                         let host = &mut host_getter(caller.data_mut());
155                         let r = Host::mrd(host);
156                         Ok((r,))
157                     },
158                 )?;
159                 inst.func_wrap(
160                     "mre",
161                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
162                         let host = &mut host_getter(caller.data_mut());
163                         let r = Host::mre(host);
164                         Ok(r)
165                     },
166                 )?;
167                 Ok(())
168             }
169             pub fn add_to_linker<T, U>(
170                 linker: &mut wasmtime::component::Linker<T>,
171                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
172             ) -> wasmtime::Result<()>
173             where
174                 U: Host,
175             {
176                 add_to_linker_get_host(linker, get)
177             }
178             impl<_T: Host + ?Sized> Host for &mut _T {
179                 fn mra(&mut self) -> () {
180                     Host::mra(*self)
181                 }
182                 fn mrb(&mut self) -> () {
183                     Host::mrb(*self)
184                 }
185                 fn mrc(&mut self) -> u32 {
186                     Host::mrc(*self)
187                 }
188                 fn mrd(&mut self) -> u32 {
189                     Host::mrd(*self)
190                 }
191                 fn mre(&mut self) -> (u32, f32) {
192                     Host::mre(*self)
193                 }
194             }
195         }
196     }
197 }
198 pub mod exports {
199     pub mod foo {
200         pub mod foo {
201             #[allow(clippy::all)]
202             pub mod multi_return {
203                 #[allow(unused_imports)]
204                 use wasmtime::component::__internal::anyhow;
205                 pub struct Guest {
206                     mra: wasmtime::component::Func,
207                     mrb: wasmtime::component::Func,
208                     mrc: wasmtime::component::Func,
209                     mrd: wasmtime::component::Func,
210                     mre: wasmtime::component::Func,
211                 }
212                 #[derive(Clone)]
213                 pub struct GuestPre {
214                     mra: wasmtime::component::ComponentExportIndex,
215                     mrb: wasmtime::component::ComponentExportIndex,
216                     mrc: wasmtime::component::ComponentExportIndex,
217                     mrd: wasmtime::component::ComponentExportIndex,
218                     mre: wasmtime::component::ComponentExportIndex,
219                 }
220                 impl GuestPre {
221                     pub fn new(
222                         component: &wasmtime::component::Component,
223                     ) -> wasmtime::Result<GuestPre> {
224                         let _component = component;
225                         let (_, instance) = component
226                             .export_index(None, "foo:foo/multi-return")
227                             .ok_or_else(|| {
228                                 anyhow::anyhow!(
229                                     "no exported instance named `foo:foo/multi-return`"
230                                 )
231                             })?;
232                         let _lookup = |name: &str| {
233                             _component
234                                 .export_index(Some(&instance), name)
235                                 .map(|p| p.1)
236                                 .ok_or_else(|| {
237                                     anyhow::anyhow!(
238                                         "instance export `foo:foo/multi-return` does \
239                 not have export `{name}`"
240                                     )
241                                 })
242                         };
243                         let mra = _lookup("mra")?;
244                         let mrb = _lookup("mrb")?;
245                         let mrc = _lookup("mrc")?;
246                         let mrd = _lookup("mrd")?;
247                         let mre = _lookup("mre")?;
248                         Ok(GuestPre {
249                             mra,
250                             mrb,
251                             mrc,
252                             mrd,
253                             mre,
254                         })
255                     }
256                     pub fn load(
257                         &self,
258                         mut store: impl wasmtime::AsContextMut,
259                         instance: &wasmtime::component::Instance,
260                     ) -> wasmtime::Result<Guest> {
261                         let mut store = store.as_context_mut();
262                         let _ = &mut store;
263                         let _instance = instance;
264                         let mra = *_instance
265                             .get_typed_func::<(), ()>(&mut store, &self.mra)?
266                             .func();
267                         let mrb = *_instance
268                             .get_typed_func::<(), ()>(&mut store, &self.mrb)?
269                             .func();
270                         let mrc = *_instance
271                             .get_typed_func::<(), (u32,)>(&mut store, &self.mrc)?
272                             .func();
273                         let mrd = *_instance
274                             .get_typed_func::<(), (u32,)>(&mut store, &self.mrd)?
275                             .func();
276                         let mre = *_instance
277                             .get_typed_func::<(), (u32, f32)>(&mut store, &self.mre)?
278                             .func();
279                         Ok(Guest { mra, mrb, mrc, mrd, mre })
280                     }
281                 }
282                 impl Guest {
283                     pub fn call_mra<S: wasmtime::AsContextMut>(
284                         &self,
285                         mut store: S,
286                     ) -> wasmtime::Result<()> {
287                         let callee = unsafe {
288                             wasmtime::component::TypedFunc::<
289                                 (),
290                                 (),
291                             >::new_unchecked(self.mra)
292                         };
293                         let () = callee.call(store.as_context_mut(), ())?;
294                         callee.post_return(store.as_context_mut())?;
295                         Ok(())
296                     }
297                     pub fn call_mrb<S: wasmtime::AsContextMut>(
298                         &self,
299                         mut store: S,
300                     ) -> wasmtime::Result<()> {
301                         let callee = unsafe {
302                             wasmtime::component::TypedFunc::<
303                                 (),
304                                 (),
305                             >::new_unchecked(self.mrb)
306                         };
307                         let () = callee.call(store.as_context_mut(), ())?;
308                         callee.post_return(store.as_context_mut())?;
309                         Ok(())
310                     }
311                     pub fn call_mrc<S: wasmtime::AsContextMut>(
312                         &self,
313                         mut store: S,
314                     ) -> wasmtime::Result<u32> {
315                         let callee = unsafe {
316                             wasmtime::component::TypedFunc::<
317                                 (),
318                                 (u32,),
319                             >::new_unchecked(self.mrc)
320                         };
321                         let (ret0,) = callee.call(store.as_context_mut(), ())?;
322                         callee.post_return(store.as_context_mut())?;
323                         Ok(ret0)
324                     }
325                     pub fn call_mrd<S: wasmtime::AsContextMut>(
326                         &self,
327                         mut store: S,
328                     ) -> wasmtime::Result<u32> {
329                         let callee = unsafe {
330                             wasmtime::component::TypedFunc::<
331                                 (),
332                                 (u32,),
333                             >::new_unchecked(self.mrd)
334                         };
335                         let (ret0,) = callee.call(store.as_context_mut(), ())?;
336                         callee.post_return(store.as_context_mut())?;
337                         Ok(ret0)
338                     }
339                     pub fn call_mre<S: wasmtime::AsContextMut>(
340                         &self,
341                         mut store: S,
342                     ) -> wasmtime::Result<(u32, f32)> {
343                         let callee = unsafe {
344                             wasmtime::component::TypedFunc::<
345                                 (),
346                                 (u32, f32),
347                             >::new_unchecked(self.mre)
348                         };
349                         let (ret0, ret1) = callee.call(store.as_context_mut(), ())?;
350                         callee.post_return(store.as_context_mut())?;
351                         Ok((ret0, ret1))
352                     }
353                 }
354             }
355         }
356     }
357 }
358