1 /// Auto-generated bindings for a pre-instantiated version of a
2 /// copmonent 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 pub struct ImportsPre<T> {
8     instance_pre: wasmtime::component::InstancePre<T>,
9 }
10 /// Auto-generated bindings for an instance a component which
11 /// implements the world `imports`.
12 ///
13 /// This structure is created through either
14 /// [`Imports::instantiate`] or by first creating
15 /// a [`ImportsPre`] followed by using
16 /// [`ImportsPre::instantiate`].
17 pub struct Imports {}
18 const _: () = {
19     #[allow(unused_imports)]
20     use wasmtime::component::__internal::anyhow;
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 compoennt 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 _component = instance_pre.component();
31             Ok(ImportsPre { instance_pre })
32         }
33         /// Instantiates a new instance of [`Imports`] within the
34         /// `store` provided.
35         ///
36         /// This function will use `self` as the pre-instantiated
37         /// instance to perform instantiation. Afterwards the preloaded
38         /// indices in `self` are used to lookup all exports on the
39         /// resulting instance.
40         pub fn instantiate(
41             &self,
42             mut store: impl wasmtime::AsContextMut<Data = _T>,
43         ) -> wasmtime::Result<Imports> {
44             let mut store = store.as_context_mut();
45             let _instance = self.instance_pre.instantiate(&mut store)?;
46             Ok(Imports {})
47         }
48     }
49     impl Imports {
50         /// Convenience wrapper around [`ImportsPre::new`] and
51         /// [`ImportsPre::instantiate`].
52         pub fn instantiate<_T>(
53             mut store: impl wasmtime::AsContextMut<Data = _T>,
54             component: &wasmtime::component::Component,
55             linker: &wasmtime::component::Linker<_T>,
56         ) -> wasmtime::Result<Imports> {
57             let pre = linker.instantiate_pre(component)?;
58             ImportsPre::new(pre)?.instantiate(store)
59         }
60         pub fn add_to_linker<T, U>(
61             linker: &mut wasmtime::component::Linker<T>,
62             get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
63         ) -> wasmtime::Result<()>
64         where
65             U: a::b::interface_with_live_type::Host
66                 + a::b::interface_with_dead_type::Host,
67         {
68             a::b::interface_with_live_type::add_to_linker(linker, get)?;
69             a::b::interface_with_dead_type::add_to_linker(linker, get)?;
70             Ok(())
71         }
72     }
73 };
74 pub mod a {
75     pub mod b {
76         #[allow(clippy::all)]
77         pub mod interface_with_live_type {
78             #[allow(unused_imports)]
79             use wasmtime::component::__internal::anyhow;
80             #[derive(wasmtime::component::ComponentType)]
81             #[derive(wasmtime::component::Lift)]
82             #[derive(wasmtime::component::Lower)]
83             #[component(record)]
84             #[derive(Clone, Copy)]
85             pub struct LiveType {
86                 #[component(name = "a")]
87                 pub a: u32,
88             }
89             impl core::fmt::Debug for LiveType {
90                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
91                     f.debug_struct("LiveType").field("a", &self.a).finish()
92                 }
93             }
94             const _: () = {
95                 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
96                 assert!(
97                     4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
98                 );
99             };
100             pub trait Host {
101                 fn f(&mut self) -> LiveType;
102             }
103             pub trait GetHost<
104                 T,
105             >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
106                 type Host: Host;
107             }
108             impl<F, T, O> GetHost<T> for F
109             where
110                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
111                 O: Host,
112             {
113                 type Host = O;
114             }
115             pub fn add_to_linker_get_host<T>(
116                 linker: &mut wasmtime::component::Linker<T>,
117                 host_getter: impl for<'a> GetHost<&'a mut T>,
118             ) -> wasmtime::Result<()> {
119                 let mut inst = linker.instance("a:b/interface-with-live-type")?;
120                 inst.func_wrap(
121                     "f",
122                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
123                         let host = &mut host_getter(caller.data_mut());
124                         let r = Host::f(host);
125                         Ok((r,))
126                     },
127                 )?;
128                 Ok(())
129             }
130             pub fn add_to_linker<T, U>(
131                 linker: &mut wasmtime::component::Linker<T>,
132                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
133             ) -> wasmtime::Result<()>
134             where
135                 U: Host,
136             {
137                 add_to_linker_get_host(linker, get)
138             }
139             impl<_T: Host + ?Sized> Host for &mut _T {
140                 fn f(&mut self) -> LiveType {
141                     Host::f(*self)
142                 }
143             }
144         }
145         #[allow(clippy::all)]
146         pub mod interface_with_dead_type {
147             #[allow(unused_imports)]
148             use wasmtime::component::__internal::anyhow;
149             pub trait Host {}
150             pub trait GetHost<
151                 T,
152             >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
153                 type Host: Host;
154             }
155             impl<F, T, O> GetHost<T> for F
156             where
157                 F: Fn(T) -> O + Send + Sync + Copy + 'static,
158                 O: Host,
159             {
160                 type Host = O;
161             }
162             pub fn add_to_linker_get_host<T>(
163                 linker: &mut wasmtime::component::Linker<T>,
164                 host_getter: impl for<'a> GetHost<&'a mut T>,
165             ) -> wasmtime::Result<()> {
166                 let mut inst = linker.instance("a:b/interface-with-dead-type")?;
167                 Ok(())
168             }
169             pub fn add_to_linker<T, U>(
170                 linker: &mut wasmtime::component::Linker<T>,
171                 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
172             ) -> wasmtime::Result<()>
173             where
174                 U: Host,
175             {
176                 add_to_linker_get_host(linker, get)
177             }
178             impl<_T: Host + ?Sized> Host for &mut _T {}
179         }
180     }
181 }
182