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