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::strings::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::strings::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::strings::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::strings::Host,
88         {
89             foo::foo::strings::add_to_linker(linker, get)?;
90             Ok(())
91         }
92         pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest {
93             &self.interface0
94         }
95     }
96 };
97 pub mod foo {
98     pub mod foo {
99         #[allow(clippy::all)]
100         pub mod strings {
101             #[allow(unused_imports)]
102             use wasmtime::component::__internal::anyhow;
103             pub trait Host {
104                 fn a(&mut self, x: wasmtime::component::__internal::String) -> ();
105                 fn b(&mut self) -> wasmtime::component::__internal::String;
106                 fn c(
107                     &mut self,
108                     a: wasmtime::component::__internal::String,
109                     b: wasmtime::component::__internal::String,
110                 ) -> wasmtime::component::__internal::String;
111             }
112             pub trait GetHost<
113                 T,
114             >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
115                 type Host: Host;
116             }
117             impl<F, T, O> GetHost<T> for F
118             where
119                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
120                 O: Host,
121             {
122                 type Host = O;
123             }
124             pub fn add_to_linker_get_host<T>(
125                 linker: &mut wasmtime::component::Linker<T>,
126                 host_getter: impl for<'a> GetHost<&'a mut T>,
127             ) -> wasmtime::Result<()> {
128                 let mut inst = linker.instance("foo:foo/strings")?;
129                 inst.func_wrap(
130                     "a",
131                     move |
132                         mut caller: wasmtime::StoreContextMut<'_, T>,
133                         (arg0,): (wasmtime::component::__internal::String,)|
134                     {
135                         let host = &mut host_getter(caller.data_mut());
136                         let r = Host::a(host, arg0);
137                         Ok(r)
138                     },
139                 )?;
140                 inst.func_wrap(
141                     "b",
142                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
143                         let host = &mut host_getter(caller.data_mut());
144                         let r = Host::b(host);
145                         Ok((r,))
146                     },
147                 )?;
148                 inst.func_wrap(
149                     "c",
150                     move |
151                         mut caller: wasmtime::StoreContextMut<'_, T>,
152                         (
153                             arg0,
154                             arg1,
155                         ): (
156                             wasmtime::component::__internal::String,
157                             wasmtime::component::__internal::String,
158                         )|
159                     {
160                         let host = &mut host_getter(caller.data_mut());
161                         let r = Host::c(host, arg0, arg1);
162                         Ok((r,))
163                     },
164                 )?;
165                 Ok(())
166             }
167             pub fn add_to_linker<T, U>(
168                 linker: &mut wasmtime::component::Linker<T>,
169                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
170             ) -> wasmtime::Result<()>
171             where
172                 U: Host,
173             {
174                 add_to_linker_get_host(linker, get)
175             }
176             impl<_T: Host + ?Sized> Host for &mut _T {
177                 fn a(&mut self, x: wasmtime::component::__internal::String) -> () {
178                     Host::a(*self, x)
179                 }
180                 fn b(&mut self) -> wasmtime::component::__internal::String {
181                     Host::b(*self)
182                 }
183                 fn c(
184                     &mut self,
185                     a: wasmtime::component::__internal::String,
186                     b: wasmtime::component::__internal::String,
187                 ) -> wasmtime::component::__internal::String {
188                     Host::c(*self, a, b)
189                 }
190             }
191         }
192     }
193 }
194 pub mod exports {
195     pub mod foo {
196         pub mod foo {
197             #[allow(clippy::all)]
198             pub mod strings {
199                 #[allow(unused_imports)]
200                 use wasmtime::component::__internal::anyhow;
201                 pub struct Guest {
202                     a: wasmtime::component::Func,
203                     b: wasmtime::component::Func,
204                     c: wasmtime::component::Func,
205                 }
206                 #[derive(Clone)]
207                 pub struct GuestPre {
208                     a: wasmtime::component::ComponentExportIndex,
209                     b: wasmtime::component::ComponentExportIndex,
210                     c: wasmtime::component::ComponentExportIndex,
211                 }
212                 impl GuestPre {
213                     pub fn new(
214                         component: &wasmtime::component::Component,
215                     ) -> wasmtime::Result<GuestPre> {
216                         let _component = component;
217                         let (_, instance) = component
218                             .export_index(None, "foo:foo/strings")
219                             .ok_or_else(|| {
220                                 anyhow::anyhow!(
221                                     "no exported instance named `foo:foo/strings`"
222                                 )
223                             })?;
224                         let _lookup = |name: &str| {
225                             _component
226                                 .export_index(Some(&instance), name)
227                                 .map(|p| p.1)
228                                 .ok_or_else(|| {
229                                     anyhow::anyhow!(
230                                         "instance export `foo:foo/strings` does \
231                 not have export `{name}`"
232                                     )
233                                 })
234                         };
235                         let a = _lookup("a")?;
236                         let b = _lookup("b")?;
237                         let c = _lookup("c")?;
238                         Ok(GuestPre { a, b, c })
239                     }
240                     pub fn load(
241                         &self,
242                         mut store: impl wasmtime::AsContextMut,
243                         instance: &wasmtime::component::Instance,
244                     ) -> wasmtime::Result<Guest> {
245                         let mut store = store.as_context_mut();
246                         let _ = &mut store;
247                         let _instance = instance;
248                         let a = *_instance
249                             .get_typed_func::<(&str,), ()>(&mut store, &self.a)?
250                             .func();
251                         let b = *_instance
252                             .get_typed_func::<
253                                 (),
254                                 (wasmtime::component::__internal::String,),
255                             >(&mut store, &self.b)?
256                             .func();
257                         let c = *_instance
258                             .get_typed_func::<
259                                 (&str, &str),
260                                 (wasmtime::component::__internal::String,),
261                             >(&mut store, &self.c)?
262                             .func();
263                         Ok(Guest { a, b, c })
264                     }
265                 }
266                 impl Guest {
267                     pub fn call_a<S: wasmtime::AsContextMut>(
268                         &self,
269                         mut store: S,
270                         arg0: &str,
271                     ) -> wasmtime::Result<()> {
272                         let callee = unsafe {
273                             wasmtime::component::TypedFunc::<
274                                 (&str,),
275                                 (),
276                             >::new_unchecked(self.a)
277                         };
278                         let () = callee.call(store.as_context_mut(), (arg0,))?;
279                         callee.post_return(store.as_context_mut())?;
280                         Ok(())
281                     }
282                     pub fn call_b<S: wasmtime::AsContextMut>(
283                         &self,
284                         mut store: S,
285                     ) -> wasmtime::Result<wasmtime::component::__internal::String> {
286                         let callee = unsafe {
287                             wasmtime::component::TypedFunc::<
288                                 (),
289                                 (wasmtime::component::__internal::String,),
290                             >::new_unchecked(self.b)
291                         };
292                         let (ret0,) = callee.call(store.as_context_mut(), ())?;
293                         callee.post_return(store.as_context_mut())?;
294                         Ok(ret0)
295                     }
296                     pub fn call_c<S: wasmtime::AsContextMut>(
297                         &self,
298                         mut store: S,
299                         arg0: &str,
300                         arg1: &str,
301                     ) -> wasmtime::Result<wasmtime::component::__internal::String> {
302                         let callee = unsafe {
303                             wasmtime::component::TypedFunc::<
304                                 (&str, &str),
305                                 (wasmtime::component::__internal::String,),
306                             >::new_unchecked(self.c)
307                         };
308                         let (ret0,) = callee.call(store.as_context_mut(), (arg0, arg1))?;
309                         callee.post_return(store.as_context_mut())?;
310                         Ok(ret0)
311                     }
312                 }
313             }
314         }
315     }
316 }
317