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