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: 'static> {
10     instance_pre: wasmtime::component::InstancePre<T>,
11     indices: ImportsIndices,
12 }
13 impl<T: 'static> 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: 'static> 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, D>(
142             linker: &mut wasmtime::component::Linker<T>,
143             get: fn(&mut T) -> D::Data<'_>,
144         ) -> wasmtime::Result<()>
145         where
146             D: wasmtime::component::HasData,
147             for<'a> D::Data<
148                 'a,
149             >: a::b::interface_with_live_type::Host
150                 + a::b::interface_with_dead_type::Host,
151             T: 'static,
152         {
153             a::b::interface_with_live_type::add_to_linker::<T, D>(linker, get)?;
154             a::b::interface_with_dead_type::add_to_linker::<T, D>(linker, get)?;
155             Ok(())
156         }
157     }
158 };
159 pub mod a {
160     pub mod b {
161         #[allow(clippy::all)]
162         pub mod interface_with_live_type {
163             #[allow(unused_imports)]
164             use wasmtime::component::__internal::{anyhow, Box};
165             #[derive(wasmtime::component::ComponentType)]
166             #[derive(wasmtime::component::Lift)]
167             #[derive(wasmtime::component::Lower)]
168             #[component(record)]
169             #[derive(Clone, Copy)]
170             pub struct LiveType {
171                 #[component(name = "a")]
172                 pub a: u32,
173             }
174             impl core::fmt::Debug for LiveType {
175                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
176                     f.debug_struct("LiveType").field("a", &self.a).finish()
177                 }
178             }
179             const _: () = {
180                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
181                 assert!(
182                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
183                 );
184             };
185             pub trait Host {
186                 fn f(&mut self) -> LiveType;
187             }
188             pub fn add_to_linker<T, D>(
189                 linker: &mut wasmtime::component::Linker<T>,
190                 host_getter: fn(&mut T) -> D::Data<'_>,
191             ) -> wasmtime::Result<()>
192             where
193                 D: wasmtime::component::HasData,
194                 for<'a> D::Data<'a>: Host,
195                 T: 'static,
196             {
197                 let mut inst = linker.instance("a:b/interface-with-live-type")?;
198                 inst.func_wrap(
199                     "f",
200                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
201                         let host = &mut host_getter(caller.data_mut());
202                         let r = Host::f(host);
203                         Ok((r,))
204                     },
205                 )?;
206                 Ok(())
207             }
208             impl<_T: Host + ?Sized> Host for &mut _T {
209                 fn f(&mut self) -> LiveType {
210                     Host::f(*self)
211                 }
212             }
213         }
214         #[allow(clippy::all)]
215         pub mod interface_with_dead_type {
216             #[allow(unused_imports)]
217             use wasmtime::component::__internal::{anyhow, Box};
218             pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType;
219             const _: () = {
220                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
221                 assert!(
222                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
223                 );
224             };
225             #[derive(wasmtime::component::ComponentType)]
226             #[derive(wasmtime::component::Lift)]
227             #[derive(wasmtime::component::Lower)]
228             #[component(record)]
229             #[derive(Clone, Copy)]
230             pub struct DeadType {
231                 #[component(name = "a")]
232                 pub a: u32,
233             }
234             impl core::fmt::Debug for DeadType {
235                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
236                     f.debug_struct("DeadType").field("a", &self.a).finish()
237                 }
238             }
239             const _: () = {
240                 assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32);
241                 assert!(
242                     4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32
243                 );
244             };
245             #[derive(wasmtime::component::ComponentType)]
246             #[derive(wasmtime::component::Lift)]
247             #[derive(wasmtime::component::Lower)]
248             #[component(variant)]
249             #[derive(Clone, Copy)]
250             pub enum V {
251                 #[component(name = "a")]
252                 A(LiveType),
253                 #[component(name = "b")]
254                 B(DeadType),
255             }
256             impl core::fmt::Debug for V {
257                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
258                     match self {
259                         V::A(e) => f.debug_tuple("V::A").field(e).finish(),
260                         V::B(e) => f.debug_tuple("V::B").field(e).finish(),
261                     }
262                 }
263             }
264             const _: () = {
265                 assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32);
266                 assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32);
267             };
268             pub trait Host {}
269             pub fn add_to_linker<T, D>(
270                 linker: &mut wasmtime::component::Linker<T>,
271                 host_getter: fn(&mut T) -> D::Data<'_>,
272             ) -> wasmtime::Result<()>
273             where
274                 D: wasmtime::component::HasData,
275                 for<'a> D::Data<'a>: Host,
276                 T: 'static,
277             {
278                 let mut inst = linker.instance("a:b/interface-with-dead-type")?;
279                 Ok(())
280             }
281             impl<_T: Host + ?Sized> Host for &mut _T {}
282         }
283     }
284 }
285