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