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 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 /// These methods are all equivalent to one another and move
87 /// around the tradeoff of what work is performed when.
88 ///
89 /// [`Store`]: wasmtime::Store
90 /// [`Component`]: wasmtime::component::Component
91 /// [`Linker`]: wasmtime::component::Linker
92 pub struct Imports {}
93 const _: () = {
94     #[allow(unused_imports)]
95     use wasmtime::component::__internal::anyhow;
96     impl ImportsIndices {
97         /// Creates a new copy of `ImportsIndices` bindings which can then
98         /// be used to instantiate into a particular store.
99         ///
100         /// This method may fail if the component does not have the
101         /// required exports.
102         pub fn new<_T>(
103             _instance_pre: &wasmtime::component::InstancePre<_T>,
104         ) -> wasmtime::Result<Self> {
105             let _component = _instance_pre.component();
106             let _instance_type = _instance_pre.instance_type();
107             Ok(ImportsIndices {})
108         }
109         /// Uses the indices stored in `self` to load an instance
110         /// of [`Imports`] from the instance provided.
111         ///
112         /// Note that at this time this method will additionally
113         /// perform type-checks of all exports.
114         pub fn load(
115             &self,
116             mut store: impl wasmtime::AsContextMut,
117             instance: &wasmtime::component::Instance,
118         ) -> wasmtime::Result<Imports> {
119             let _ = &mut store;
120             let _instance = instance;
121             Ok(Imports {})
122         }
123     }
124     impl Imports {
125         /// Convenience wrapper around [`ImportsPre::new`] and
126         /// [`ImportsPre::instantiate_async`].
127         pub async fn instantiate_async<_T>(
128             store: impl wasmtime::AsContextMut<Data = _T>,
129             component: &wasmtime::component::Component,
130             linker: &wasmtime::component::Linker<_T>,
131         ) -> wasmtime::Result<Imports>
132         where
133             _T: Send,
134         {
135             let pre = linker.instantiate_pre(component)?;
136             ImportsPre::new(pre)?.instantiate_async(store).await
137         }
138         /// Convenience wrapper around [`ImportsIndices::new`] and
139         /// [`ImportsIndices::load`].
140         pub fn new(
141             mut store: impl wasmtime::AsContextMut,
142             instance: &wasmtime::component::Instance,
143         ) -> wasmtime::Result<Imports> {
144             let indices = ImportsIndices::new(&instance.instance_pre(&store))?;
145             indices.load(&mut store, instance)
146         }
147         pub fn add_to_linker<T, U>(
148             linker: &mut wasmtime::component::Linker<T>,
149             get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
150         ) -> wasmtime::Result<()>
151         where
152             T: Send,
153             U: a::b::interface_with_live_type::Host
154                 + a::b::interface_with_dead_type::Host + Send,
155         {
156             a::b::interface_with_live_type::add_to_linker(linker, get)?;
157             a::b::interface_with_dead_type::add_to_linker(linker, get)?;
158             Ok(())
159         }
160     }
161 };
162 pub mod a {
163     pub mod b {
164         #[allow(clippy::all)]
165         pub mod interface_with_live_type {
166             #[allow(unused_imports)]
167             use wasmtime::component::__internal::{anyhow, Box};
168             #[derive(wasmtime::component::ComponentType)]
169             #[derive(wasmtime::component::Lift)]
170             #[derive(wasmtime::component::Lower)]
171             #[component(record)]
172             #[derive(Clone, Copy)]
173             pub struct LiveType {
174                 #[component(name = "a")]
175                 pub a: u32,
176             }
177             impl core::fmt::Debug for LiveType {
178                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
179                     f.debug_struct("LiveType").field("a", &self.a).finish()
180                 }
181             }
182             const _: () = {
183                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
184                 assert!(
185                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
186                 );
187             };
188             #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
189             pub trait Host: Send {
190                 async fn f(&mut self) -> LiveType;
191             }
192             pub trait GetHost<
193                 T,
194                 D,
195             >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static {
196                 type Host: Host + Send;
197             }
198             impl<F, T, D, O> GetHost<T, D> for F
199             where
200                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
201                 O: Host + Send,
202             {
203                 type Host = O;
204             }
205             pub fn add_to_linker_get_host<
206                 T,
207                 G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>,
208             >(
209                 linker: &mut wasmtime::component::Linker<T>,
210                 host_getter: G,
211             ) -> wasmtime::Result<()>
212             where
213                 T: Send,
214             {
215                 let mut inst = linker.instance("a:b/interface-with-live-type")?;
216                 inst.func_wrap_async(
217                     "f",
218                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
219                         use tracing::Instrument;
220                         let span = tracing::span!(
221                             tracing::Level::TRACE, "wit-bindgen import", module =
222                             "interface-with-live-type", function = "f",
223                         );
224                         wasmtime::component::__internal::Box::new(
225                             async move {
226                                 tracing::event!(tracing::Level::TRACE, "call");
227                                 let host = &mut host_getter(caller.data_mut());
228                                 let r = Host::f(host).await;
229                                 tracing::event!(
230                                     tracing::Level::TRACE, result = tracing::field::debug(& r),
231                                     "return"
232                                 );
233                                 Ok((r,))
234                             }
235                                 .instrument(span),
236                         )
237                     },
238                 )?;
239                 Ok(())
240             }
241             pub fn add_to_linker<T, U>(
242                 linker: &mut wasmtime::component::Linker<T>,
243                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
244             ) -> wasmtime::Result<()>
245             where
246                 U: Host + Send,
247                 T: Send,
248             {
249                 add_to_linker_get_host(linker, get)
250             }
251             impl<_T: Host + ?Sized + Send> Host for &mut _T {
252                 async fn f(&mut self) -> LiveType {
253                     Host::f(*self).await
254                 }
255             }
256         }
257         #[allow(clippy::all)]
258         pub mod interface_with_dead_type {
259             #[allow(unused_imports)]
260             use wasmtime::component::__internal::{anyhow, Box};
261             pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType;
262             const _: () = {
263                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
264                 assert!(
265                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
266                 );
267             };
268             #[derive(wasmtime::component::ComponentType)]
269             #[derive(wasmtime::component::Lift)]
270             #[derive(wasmtime::component::Lower)]
271             #[component(record)]
272             #[derive(Clone, Copy)]
273             pub struct DeadType {
274                 #[component(name = "a")]
275                 pub a: u32,
276             }
277             impl core::fmt::Debug for DeadType {
278                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
279                     f.debug_struct("DeadType").field("a", &self.a).finish()
280                 }
281             }
282             const _: () = {
283                 assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32);
284                 assert!(
285                     4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32
286                 );
287             };
288             #[derive(wasmtime::component::ComponentType)]
289             #[derive(wasmtime::component::Lift)]
290             #[derive(wasmtime::component::Lower)]
291             #[component(variant)]
292             #[derive(Clone, Copy)]
293             pub enum V {
294                 #[component(name = "a")]
295                 A(LiveType),
296                 #[component(name = "b")]
297                 B(DeadType),
298             }
299             impl core::fmt::Debug for V {
300                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
301                     match self {
302                         V::A(e) => f.debug_tuple("V::A").field(e).finish(),
303                         V::B(e) => f.debug_tuple("V::B").field(e).finish(),
304                     }
305                 }
306             }
307             const _: () = {
308                 assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32);
309                 assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32);
310             };
311             #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
312             pub trait Host: Send {}
313             pub trait GetHost<
314                 T,
315                 D,
316             >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static {
317                 type Host: Host + Send;
318             }
319             impl<F, T, D, O> GetHost<T, D> for F
320             where
321                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
322                 O: Host + Send,
323             {
324                 type Host = O;
325             }
326             pub fn add_to_linker_get_host<
327                 T,
328                 G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>,
329             >(
330                 linker: &mut wasmtime::component::Linker<T>,
331                 host_getter: G,
332             ) -> wasmtime::Result<()>
333             where
334                 T: Send,
335             {
336                 let mut inst = linker.instance("a:b/interface-with-dead-type")?;
337                 Ok(())
338             }
339             pub fn add_to_linker<T, U>(
340                 linker: &mut wasmtime::component::Linker<T>,
341                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
342             ) -> wasmtime::Result<()>
343             where
344                 U: Host + Send,
345                 T: Send,
346             {
347                 add_to_linker_get_host(linker, get)
348             }
349             impl<_T: Host + ?Sized + Send> Host for &mut _T {}
350         }
351     }
352 }
353