1 /// Auto-generated bindings for a pre-instantiated version of a
2 /// component which implements the world `host`.
3 ///
4 /// This structure is created through [`Host_Pre::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 [`Host_`] as well.
9 pub struct Host_Pre<T: 'static> {
10     instance_pre: wasmtime::component::InstancePre<T>,
11     indices: Host_Indices,
12 }
13 impl<T: 'static> Clone for Host_Pre<T> {
clone(&self) -> Self14     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> Host_Pre<_T> {
22     /// Creates a new copy of `Host_Pre` 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.
new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>27     pub fn new(
28         instance_pre: wasmtime::component::InstancePre<_T>,
29     ) -> wasmtime::Result<Self> {
30         let indices = Host_Indices::new(&instance_pre)?;
31         Ok(Self { instance_pre, indices })
32     }
engine(&self) -> &wasmtime::Engine33     pub fn engine(&self) -> &wasmtime::Engine {
34         self.instance_pre.engine()
35     }
instance_pre(&self) -> &wasmtime::component::InstancePre<_T>36     pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {
37         &self.instance_pre
38     }
39     /// Instantiates a new instance of [`Host_`] 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.
instantiate( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Host_>46     pub fn instantiate(
47         &self,
48         mut store: impl wasmtime::AsContextMut<Data = _T>,
49     ) -> wasmtime::Result<Host_> {
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> Host_Pre<_T> {
56     /// Same as [`Self::instantiate`], except with `async`.
instantiate_async( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Host_>57     pub async fn instantiate_async(
58         &self,
59         mut store: impl wasmtime::AsContextMut<Data = _T>,
60     ) -> wasmtime::Result<Host_> {
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 /// `host`.
68 ///
69 /// This is an implementation detail of [`Host_Pre`] and can
70 /// be constructed if needed as well.
71 ///
72 /// For more information see [`Host_`] as well.
73 #[derive(Clone)]
74 pub struct Host_Indices {}
75 /// Auto-generated bindings for an instance a component which
76 /// implements the world `host`.
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 ///   [`Host_::instantiate`] which only needs a
83 ///   [`Store`], [`Component`], and [`Linker`].
84 ///
85 /// * Alternatively you can create a [`Host_Pre`] 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 [`Host_Pre::instantiate`] to
89 ///   create a [`Host_`].
90 ///
91 /// * If you've instantiated the instance yourself already
92 ///   then you can use [`Host_::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 Host_ {}
101 pub trait Host_ImportsWithStore: wasmtime::component::HasData + Send {}
102 impl<_T: ?Sized> Host_ImportsWithStore for _T
103 where
104     _T: wasmtime::component::HasData + Send,
105 {}
106 pub trait Host_Imports: Send {
foo(&mut self) -> impl ::core::future::Future<Output = ()> + Send107     fn foo(&mut self) -> impl ::core::future::Future<Output = ()> + Send;
108 }
109 impl<_T: Host_Imports + ?Sized + Send> Host_Imports for &mut _T {
foo(&mut self) -> impl ::core::future::Future<Output = ()> + Send110     fn foo(&mut self) -> impl ::core::future::Future<Output = ()> + Send {
111         async move { Host_Imports::foo(*self).await }
112     }
113 }
114 const _: () = {
115     impl Host_Indices {
116         /// Creates a new copy of `Host_Indices` bindings which can then
117         /// be used to instantiate into a particular store.
118         ///
119         /// This method may fail if the component does not have the
120         /// required exports.
new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>121         pub fn new<_T>(
122             _instance_pre: &wasmtime::component::InstancePre<_T>,
123         ) -> wasmtime::Result<Self> {
124             let _component = _instance_pre.component();
125             let _instance_type = _instance_pre.instance_type();
126             Ok(Host_Indices {})
127         }
128         /// Uses the indices stored in `self` to load an instance
129         /// of [`Host_`] from the instance provided.
130         ///
131         /// Note that at this time this method will additionally
132         /// perform type-checks of all exports.
load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Host_>133         pub fn load(
134             &self,
135             mut store: impl wasmtime::AsContextMut,
136             instance: &wasmtime::component::Instance,
137         ) -> wasmtime::Result<Host_> {
138             let _ = &mut store;
139             let _instance = instance;
140             Ok(Host_ {})
141         }
142     }
143     impl Host_ {
144         /// Convenience wrapper around [`Host_Pre::new`] and
145         /// [`Host_Pre::instantiate`].
instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Host_>146         pub fn instantiate<_T>(
147             store: impl wasmtime::AsContextMut<Data = _T>,
148             component: &wasmtime::component::Component,
149             linker: &wasmtime::component::Linker<_T>,
150         ) -> wasmtime::Result<Host_> {
151             let pre = linker.instantiate_pre(component)?;
152             Host_Pre::new(pre)?.instantiate(store)
153         }
154         /// Convenience wrapper around [`Host_Indices::new`] and
155         /// [`Host_Indices::load`].
new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Host_>156         pub fn new(
157             mut store: impl wasmtime::AsContextMut,
158             instance: &wasmtime::component::Instance,
159         ) -> wasmtime::Result<Host_> {
160             let indices = Host_Indices::new(&instance.instance_pre(&store))?;
161             indices.load(&mut store, instance)
162         }
163         /// Convenience wrapper around [`Host_Pre::new`] and
164         /// [`Host_Pre::instantiate_async`].
instantiate_async<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Host_> where _T: Send,165         pub async fn instantiate_async<_T>(
166             store: impl wasmtime::AsContextMut<Data = _T>,
167             component: &wasmtime::component::Component,
168             linker: &wasmtime::component::Linker<_T>,
169         ) -> wasmtime::Result<Host_>
170         where
171             _T: Send,
172         {
173             let pre = linker.instantiate_pre(component)?;
174             Host_Pre::new(pre)?.instantiate_async(store).await
175         }
add_to_linker_imports<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: Host_ImportsWithStore, for<'a> D::Data<'a>: Host_Imports, T: 'static + Send,176         pub fn add_to_linker_imports<T, D>(
177             linker: &mut wasmtime::component::Linker<T>,
178             host_getter: fn(&mut T) -> D::Data<'_>,
179         ) -> wasmtime::Result<()>
180         where
181             D: Host_ImportsWithStore,
182             for<'a> D::Data<'a>: Host_Imports,
183             T: 'static + Send,
184         {
185             let mut linker = linker.root();
186             linker
187                 .func_wrap_async(
188                     "foo",
189                     move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
190                         wasmtime::component::__internal::Box::new(async move {
191                             let host = &mut host_getter(caller.data_mut());
192                             let r = Host_Imports::foo(host).await;
193                             Ok(r)
194                         })
195                     },
196                 )?;
197             Ok(())
198         }
add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: Host_ImportsWithStore + Send, for<'a> D::Data<'a>: Host_Imports + Send, T: 'static + Send,199         pub fn add_to_linker<T, D>(
200             linker: &mut wasmtime::component::Linker<T>,
201             host_getter: fn(&mut T) -> D::Data<'_>,
202         ) -> wasmtime::Result<()>
203         where
204             D: Host_ImportsWithStore + Send,
205             for<'a> D::Data<'a>: Host_Imports + Send,
206             T: 'static + Send,
207         {
208             Self::add_to_linker_imports::<T, D>(linker, host_getter)?;
209             Ok(())
210         }
211     }
212 };
213