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 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 /// * You can also access the guts of instantiation through
84 ///   [`ImportsIndices::new_instance`] followed
85 ///   by [`ImportsIndices::load`] to crate an instance of this
86 ///   type.
87 ///
88 /// These methods are all equivalent to one another and move
89 /// around the tradeoff of what work is performed when.
90 ///
91 /// [`Store`]: wasmtime::Store
92 /// [`Component`]: wasmtime::component::Component
93 /// [`Linker`]: wasmtime::component::Linker
94 pub struct Imports {}
95 const _: () = {
96     #[allow(unused_imports)]
97     use wasmtime::component::__internal::anyhow;
98     impl ImportsIndices {
99         /// Creates a new copy of `ImportsIndices` bindings which can then
100         /// be used to instantiate into a particular store.
101         ///
102         /// This method may fail if the component does not have the
103         /// required exports.
104         pub fn new(
105             component: &wasmtime::component::Component,
106         ) -> wasmtime::Result<Self> {
107             let _component = component;
108             Ok(ImportsIndices {})
109         }
110         /// Creates a new instance of [`ImportsIndices`] from an
111         /// instantiated component.
112         ///
113         /// This method of creating a [`Imports`] will perform string
114         /// lookups for all exports when this method is called. This
115         /// will only succeed if the provided instance matches the
116         /// requirements of [`Imports`].
117         pub fn new_instance(
118             mut store: impl wasmtime::AsContextMut,
119             instance: &wasmtime::component::Instance,
120         ) -> wasmtime::Result<Self> {
121             let _instance = instance;
122             Ok(ImportsIndices {})
123         }
124         /// Uses the indices stored in `self` to load an instance
125         /// of [`Imports`] from the instance provided.
126         ///
127         /// Note that at this time this method will additionally
128         /// perform type-checks of all exports.
129         pub fn load(
130             &self,
131             mut store: impl wasmtime::AsContextMut,
132             instance: &wasmtime::component::Instance,
133         ) -> wasmtime::Result<Imports> {
134             let _instance = instance;
135             Ok(Imports {})
136         }
137     }
138     impl Imports {
139         /// Convenience wrapper around [`ImportsPre::new`] and
140         /// [`ImportsPre::instantiate`].
141         pub fn instantiate<_T>(
142             mut store: impl wasmtime::AsContextMut<Data = _T>,
143             component: &wasmtime::component::Component,
144             linker: &wasmtime::component::Linker<_T>,
145         ) -> wasmtime::Result<Imports> {
146             let pre = linker.instantiate_pre(component)?;
147             ImportsPre::new(pre)?.instantiate(store)
148         }
149         /// Convenience wrapper around [`ImportsIndices::new_instance`] and
150         /// [`ImportsIndices::load`].
151         pub fn new(
152             mut store: impl wasmtime::AsContextMut,
153             instance: &wasmtime::component::Instance,
154         ) -> wasmtime::Result<Imports> {
155             let indices = ImportsIndices::new_instance(&mut store, instance)?;
156             indices.load(store, instance)
157         }
158         pub fn add_to_linker<T, U>(
159             linker: &mut wasmtime::component::Linker<T>,
160             get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
161         ) -> wasmtime::Result<()>
162         where
163             U: a::b::interface_with_live_type::Host
164                 + a::b::interface_with_dead_type::Host,
165         {
166             a::b::interface_with_live_type::add_to_linker(linker, get)?;
167             a::b::interface_with_dead_type::add_to_linker(linker, get)?;
168             Ok(())
169         }
170     }
171 };
172 pub mod a {
173     pub mod b {
174         #[allow(clippy::all)]
175         pub mod interface_with_live_type {
176             #[allow(unused_imports)]
177             use wasmtime::component::__internal::{anyhow, Box};
178             #[derive(wasmtime::component::ComponentType)]
179             #[derive(wasmtime::component::Lift)]
180             #[derive(wasmtime::component::Lower)]
181             #[component(record)]
182             #[derive(Clone, Copy)]
183             pub struct LiveType {
184                 #[component(name = "a")]
185                 pub a: u32,
186             }
187             impl core::fmt::Debug for LiveType {
188                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
189                     f.debug_struct("LiveType").field("a", &self.a).finish()
190                 }
191             }
192             const _: () = {
193                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
194                 assert!(
195                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
196                 );
197             };
198             pub trait Host {
199                 fn f(&mut self) -> LiveType;
200             }
201             pub trait GetHost<
202                 T,
203             >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
204                 type Host: Host;
205             }
206             impl<F, T, O> GetHost<T> for F
207             where
208                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
209                 O: Host,
210             {
211                 type Host = O;
212             }
213             pub fn add_to_linker_get_host<T>(
214                 linker: &mut wasmtime::component::Linker<T>,
215                 host_getter: impl for<'a> GetHost<&'a mut T>,
216             ) -> wasmtime::Result<()> {
217                 let mut inst = linker.instance("a:b/interface-with-live-type")?;
218                 inst.func_wrap(
219                     "f",
220                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
221                         let host = &mut host_getter(caller.data_mut());
222                         let r = Host::f(host);
223                         Ok((r,))
224                     },
225                 )?;
226                 Ok(())
227             }
228             pub fn add_to_linker<T, U>(
229                 linker: &mut wasmtime::component::Linker<T>,
230                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
231             ) -> wasmtime::Result<()>
232             where
233                 U: Host,
234             {
235                 add_to_linker_get_host(linker, get)
236             }
237             impl<_T: Host + ?Sized> Host for &mut _T {
238                 fn f(&mut self) -> LiveType {
239                     Host::f(*self)
240                 }
241             }
242         }
243         #[allow(clippy::all)]
244         pub mod interface_with_dead_type {
245             #[allow(unused_imports)]
246             use wasmtime::component::__internal::{anyhow, Box};
247             pub trait Host {}
248             pub trait GetHost<
249                 T,
250             >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
251                 type Host: Host;
252             }
253             impl<F, T, O> GetHost<T> for F
254             where
255                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
256                 O: Host,
257             {
258                 type Host = O;
259             }
260             pub fn add_to_linker_get_host<T>(
261                 linker: &mut wasmtime::component::Linker<T>,
262                 host_getter: impl for<'a> GetHost<&'a mut T>,
263             ) -> wasmtime::Result<()> {
264                 let mut inst = linker.instance("a:b/interface-with-dead-type")?;
265                 Ok(())
266             }
267             pub fn add_to_linker<T, U>(
268                 linker: &mut wasmtime::component::Linker<T>,
269                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
270             ) -> wasmtime::Result<()>
271             where
272                 U: Host,
273             {
274                 add_to_linker_get_host(linker, get)
275             }
276             impl<_T: Host + ?Sized> Host for &mut _T {}
277         }
278     }
279 }
280