1 /// Auto-generated bindings for a pre-instantiated version of a
2 /// component which implements the world `imports`.
3 ///
4 /// This structure is created through [`ImportsPre::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 [`Imports`] as well.
9 pub struct ImportsPre<T> {
10     instance_pre: wasmtime::component::InstancePre<T>,
11     indices: ImportsIndices,
12 }
13 impl<T> Clone for ImportsPre<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> ImportsPre<_T> {
22     /// Creates a new copy of `ImportsPre` 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 = ImportsIndices::new(instance_pre.component())?;
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 [`Imports`] 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<Imports>
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 /// `imports`.
60 ///
61 /// This is an implementation detail of [`ImportsPre`] and can
62 /// be constructed if needed as well.
63 ///
64 /// For more information see [`Imports`] as well.
65 #[derive(Clone)]
66 pub struct ImportsIndices {}
67 /// Auto-generated bindings for an instance a component which
68 /// implements the world `imports`.
69 ///
70 /// This structure can be created through a number of means
71 /// depending on your requirements and what you have on hand:
72 ///
73 /// * The most convenient way is to use
74 ///   [`Imports::instantiate_async`] which only needs a
75 ///   [`Store`], [`Component`], and [`Linker`].
76 ///
77 /// * Alternatively you can create a [`ImportsPre`] ahead of
78 ///   time with a [`Component`] to front-load string lookups
79 ///   of exports once instead of per-instantiation. This
80 ///   method then uses [`ImportsPre::instantiate_async`] to
81 ///   create a [`Imports`].
82 ///
83 /// * If you've instantiated the instance yourself already
84 ///   then you can use [`Imports::new`].
85 ///
86 /// * You can also access the guts of instantiation through
87 ///   [`ImportsIndices::new_instance`] followed
88 ///   by [`ImportsIndices::load`] to crate an instance of this
89 ///   type.
90 ///
91 /// These methods are all equivalent to one another and move
92 /// around the tradeoff of what work is performed when.
93 ///
94 /// [`Store`]: wasmtime::Store
95 /// [`Component`]: wasmtime::component::Component
96 /// [`Linker`]: wasmtime::component::Linker
97 pub struct Imports {}
98 const _: () = {
99     #[allow(unused_imports)]
100     use wasmtime::component::__internal::anyhow;
101     impl ImportsIndices {
102         /// Creates a new copy of `ImportsIndices` bindings which can then
103         /// be used to instantiate into a particular store.
104         ///
105         /// This method may fail if the component does not have the
106         /// required exports.
107         pub fn new(
108             component: &wasmtime::component::Component,
109         ) -> wasmtime::Result<Self> {
110             let _component = component;
111             Ok(ImportsIndices {})
112         }
113         /// Creates a new instance of [`ImportsIndices`] from an
114         /// instantiated component.
115         ///
116         /// This method of creating a [`Imports`] will perform string
117         /// lookups for all exports when this method is called. This
118         /// will only succeed if the provided instance matches the
119         /// requirements of [`Imports`].
120         pub fn new_instance(
121             mut store: impl wasmtime::AsContextMut,
122             instance: &wasmtime::component::Instance,
123         ) -> wasmtime::Result<Self> {
124             let _instance = instance;
125             Ok(ImportsIndices {})
126         }
127         /// Uses the indices stored in `self` to load an instance
128         /// of [`Imports`] from the instance provided.
129         ///
130         /// Note that at this time this method will additionally
131         /// perform type-checks of all exports.
132         pub fn load(
133             &self,
134             mut store: impl wasmtime::AsContextMut,
135             instance: &wasmtime::component::Instance,
136         ) -> wasmtime::Result<Imports> {
137             let _instance = instance;
138             Ok(Imports {})
139         }
140     }
141     impl Imports {
142         /// Convenience wrapper around [`ImportsPre::new`] and
143         /// [`ImportsPre::instantiate_async`].
144         pub async fn instantiate_async<_T>(
145             mut store: impl wasmtime::AsContextMut<Data = _T>,
146             component: &wasmtime::component::Component,
147             linker: &wasmtime::component::Linker<_T>,
148         ) -> wasmtime::Result<Imports>
149         where
150             _T: Send,
151         {
152             let pre = linker.instantiate_pre(component)?;
153             ImportsPre::new(pre)?.instantiate_async(store).await
154         }
155         /// Convenience wrapper around [`ImportsIndices::new_instance`] and
156         /// [`ImportsIndices::load`].
157         pub fn new(
158             mut store: impl wasmtime::AsContextMut,
159             instance: &wasmtime::component::Instance,
160         ) -> wasmtime::Result<Imports> {
161             let indices = ImportsIndices::new_instance(&mut store, instance)?;
162             indices.load(store, instance)
163         }
164         pub fn add_to_linker<T, U>(
165             linker: &mut wasmtime::component::Linker<T>,
166             get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
167         ) -> wasmtime::Result<()>
168         where
169             T: Send,
170             U: a::b::interface_with_live_type::Host
171                 + a::b::interface_with_dead_type::Host + Send,
172         {
173             a::b::interface_with_live_type::add_to_linker(linker, get)?;
174             a::b::interface_with_dead_type::add_to_linker(linker, get)?;
175             Ok(())
176         }
177     }
178 };
179 pub mod a {
180     pub mod b {
181         #[allow(clippy::all)]
182         pub mod interface_with_live_type {
183             #[allow(unused_imports)]
184             use wasmtime::component::__internal::{anyhow, Box};
185             #[derive(wasmtime::component::ComponentType)]
186             #[derive(wasmtime::component::Lift)]
187             #[derive(wasmtime::component::Lower)]
188             #[component(record)]
189             #[derive(Clone, Copy)]
190             pub struct LiveType {
191                 #[component(name = "a")]
192                 pub a: u32,
193             }
194             impl core::fmt::Debug for LiveType {
195                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
196                     f.debug_struct("LiveType").field("a", &self.a).finish()
197                 }
198             }
199             const _: () = {
200                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
201                 assert!(
202                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
203                 );
204             };
205             #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
206             pub trait Host: Send {
207                 async fn f(&mut self) -> LiveType;
208             }
209             pub trait GetHost<
210                 T,
211                 D,
212             >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static {
213                 type Host: Host + Send;
214             }
215             impl<F, T, D, O> GetHost<T, D> for F
216             where
217                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
218                 O: Host + Send,
219             {
220                 type Host = O;
221             }
222             pub fn add_to_linker_get_host<
223                 T,
224                 G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>,
225             >(
226                 linker: &mut wasmtime::component::Linker<T>,
227                 host_getter: G,
228             ) -> wasmtime::Result<()>
229             where
230                 T: Send,
231             {
232                 let mut inst = linker.instance("a:b/interface-with-live-type")?;
233                 inst.func_wrap_async(
234                     "f",
235                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
236                         wasmtime::component::__internal::Box::new(async move {
237                             let host = &mut host_getter(caller.data_mut());
238                             let r = Host::f(host).await;
239                             Ok((r,))
240                         })
241                     },
242                 )?;
243                 Ok(())
244             }
245             pub fn add_to_linker<T, U>(
246                 linker: &mut wasmtime::component::Linker<T>,
247                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
248             ) -> wasmtime::Result<()>
249             where
250                 U: Host + Send,
251                 T: Send,
252             {
253                 add_to_linker_get_host(linker, get)
254             }
255             impl<_T: Host + ?Sized + Send> Host for &mut _T {
256                 async fn f(&mut self) -> LiveType {
257                     Host::f(*self).await
258                 }
259             }
260         }
261         #[allow(clippy::all)]
262         pub mod interface_with_dead_type {
263             #[allow(unused_imports)]
264             use wasmtime::component::__internal::{anyhow, Box};
265             pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType;
266             const _: () = {
267                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
268                 assert!(
269                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
270                 );
271             };
272             #[derive(wasmtime::component::ComponentType)]
273             #[derive(wasmtime::component::Lift)]
274             #[derive(wasmtime::component::Lower)]
275             #[component(record)]
276             #[derive(Clone, Copy)]
277             pub struct DeadType {
278                 #[component(name = "a")]
279                 pub a: u32,
280             }
281             impl core::fmt::Debug for DeadType {
282                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
283                     f.debug_struct("DeadType").field("a", &self.a).finish()
284                 }
285             }
286             const _: () = {
287                 assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32);
288                 assert!(
289                     4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32
290                 );
291             };
292             #[derive(wasmtime::component::ComponentType)]
293             #[derive(wasmtime::component::Lift)]
294             #[derive(wasmtime::component::Lower)]
295             #[component(variant)]
296             #[derive(Clone, Copy)]
297             pub enum V {
298                 #[component(name = "a")]
299                 A(LiveType),
300                 #[component(name = "b")]
301                 B(DeadType),
302             }
303             impl core::fmt::Debug for V {
304                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
305                     match self {
306                         V::A(e) => f.debug_tuple("V::A").field(e).finish(),
307                         V::B(e) => f.debug_tuple("V::B").field(e).finish(),
308                     }
309                 }
310             }
311             const _: () = {
312                 assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32);
313                 assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32);
314             };
315             #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
316             pub trait Host: Send {}
317             pub trait GetHost<
318                 T,
319                 D,
320             >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static {
321                 type Host: Host + Send;
322             }
323             impl<F, T, D, O> GetHost<T, D> for F
324             where
325                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
326                 O: Host + Send,
327             {
328                 type Host = O;
329             }
330             pub fn add_to_linker_get_host<
331                 T,
332                 G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>,
333             >(
334                 linker: &mut wasmtime::component::Linker<T>,
335                 host_getter: G,
336             ) -> wasmtime::Result<()>
337             where
338                 T: Send,
339             {
340                 let mut inst = linker.instance("a:b/interface-with-dead-type")?;
341                 Ok(())
342             }
343             pub fn add_to_linker<T, U>(
344                 linker: &mut wasmtime::component::Linker<T>,
345                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
346             ) -> wasmtime::Result<()>
347             where
348                 U: Host + Send,
349                 T: Send,
350             {
351                 add_to_linker_get_host(linker, get)
352             }
353             impl<_T: Host + ?Sized + Send> Host for &mut _T {}
354         }
355     }
356 }
357