1 /// Auto-generated bindings for a pre-instantiated version of a
2 /// component which implements the world `http-interface`.
3 ///
4 /// This structure is created through [`HttpInterfacePre::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 [`HttpInterface`] as well.
9 pub struct HttpInterfacePre<T: 'static> {
10     instance_pre: wasmtime::component::InstancePre<T>,
11     indices: HttpInterfaceIndices,
12 }
13 impl<T: 'static> Clone for HttpInterfacePre<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> HttpInterfacePre<_T> {
22     /// Creates a new copy of `HttpInterfacePre` 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 = HttpInterfaceIndices::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 [`HttpInterface`] 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<HttpInterface>46     pub fn instantiate(
47         &self,
48         mut store: impl wasmtime::AsContextMut<Data = _T>,
49     ) -> wasmtime::Result<HttpInterface> {
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> HttpInterfacePre<_T> {
56     /// Same as [`Self::instantiate`], except with `async`.
instantiate_async( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<HttpInterface>57     pub async fn instantiate_async(
58         &self,
59         mut store: impl wasmtime::AsContextMut<Data = _T>,
60     ) -> wasmtime::Result<HttpInterface> {
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 /// `http-interface`.
68 ///
69 /// This is an implementation detail of [`HttpInterfacePre`] and can
70 /// be constructed if needed as well.
71 ///
72 /// For more information see [`HttpInterface`] as well.
73 #[derive(Clone)]
74 pub struct HttpInterfaceIndices {
75     interface0: exports::http_handler::GuestIndices,
76 }
77 /// Auto-generated bindings for an instance a component which
78 /// implements the world `http-interface`.
79 ///
80 /// This structure can be created through a number of means
81 /// depending on your requirements and what you have on hand:
82 ///
83 /// * The most convenient way is to use
84 ///   [`HttpInterface::instantiate`] which only needs a
85 ///   [`Store`], [`Component`], and [`Linker`].
86 ///
87 /// * Alternatively you can create a [`HttpInterfacePre`] ahead of
88 ///   time with a [`Component`] to front-load string lookups
89 ///   of exports once instead of per-instantiation. This
90 ///   method then uses [`HttpInterfacePre::instantiate`] to
91 ///   create a [`HttpInterface`].
92 ///
93 /// * If you've instantiated the instance yourself already
94 ///   then you can use [`HttpInterface::new`].
95 ///
96 /// These methods are all equivalent to one another and move
97 /// around the tradeoff of what work is performed when.
98 ///
99 /// [`Store`]: wasmtime::Store
100 /// [`Component`]: wasmtime::component::Component
101 /// [`Linker`]: wasmtime::component::Linker
102 pub struct HttpInterface {
103     interface0: exports::http_handler::Guest,
104 }
105 const _: () = {
106     impl HttpInterfaceIndices {
107         /// Creates a new copy of `HttpInterfaceIndices` bindings which can then
108         /// be used to instantiate into a particular store.
109         ///
110         /// This method may fail if the component does not have the
111         /// required exports.
new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>112         pub fn new<_T>(
113             _instance_pre: &wasmtime::component::InstancePre<_T>,
114         ) -> wasmtime::Result<Self> {
115             let _component = _instance_pre.component();
116             let _instance_type = _instance_pre.instance_type();
117             let interface0 = exports::http_handler::GuestIndices::new(_instance_pre)?;
118             Ok(HttpInterfaceIndices { interface0 })
119         }
120         /// Uses the indices stored in `self` to load an instance
121         /// of [`HttpInterface`] from the instance provided.
122         ///
123         /// Note that at this time this method will additionally
124         /// perform type-checks of all exports.
load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<HttpInterface>125         pub fn load(
126             &self,
127             mut store: impl wasmtime::AsContextMut,
128             instance: &wasmtime::component::Instance,
129         ) -> wasmtime::Result<HttpInterface> {
130             let _ = &mut store;
131             let _instance = instance;
132             let interface0 = self.interface0.load(&mut store, &_instance)?;
133             Ok(HttpInterface { interface0 })
134         }
135     }
136     impl HttpInterface {
137         /// Convenience wrapper around [`HttpInterfacePre::new`] and
138         /// [`HttpInterfacePre::instantiate`].
instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<HttpInterface>139         pub fn instantiate<_T>(
140             store: impl wasmtime::AsContextMut<Data = _T>,
141             component: &wasmtime::component::Component,
142             linker: &wasmtime::component::Linker<_T>,
143         ) -> wasmtime::Result<HttpInterface> {
144             let pre = linker.instantiate_pre(component)?;
145             HttpInterfacePre::new(pre)?.instantiate(store)
146         }
147         /// Convenience wrapper around [`HttpInterfaceIndices::new`] and
148         /// [`HttpInterfaceIndices::load`].
new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<HttpInterface>149         pub fn new(
150             mut store: impl wasmtime::AsContextMut,
151             instance: &wasmtime::component::Instance,
152         ) -> wasmtime::Result<HttpInterface> {
153             let indices = HttpInterfaceIndices::new(&instance.instance_pre(&store))?;
154             indices.load(&mut store, instance)
155         }
156         /// Convenience wrapper around [`HttpInterfacePre::new`] and
157         /// [`HttpInterfacePre::instantiate_async`].
instantiate_async<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<HttpInterface> where _T: Send,158         pub async fn instantiate_async<_T>(
159             store: impl wasmtime::AsContextMut<Data = _T>,
160             component: &wasmtime::component::Component,
161             linker: &wasmtime::component::Linker<_T>,
162         ) -> wasmtime::Result<HttpInterface>
163         where
164             _T: Send,
165         {
166             let pre = linker.instantiate_pre(component)?;
167             HttpInterfacePre::new(pre)?.instantiate_async(store).await
168         }
add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: foo::foo::http_types::HostWithStore + http_fetch::HostWithStore, for<'a> D::Data<'a>: foo::foo::http_types::Host + http_fetch::Host, T: 'static,169         pub fn add_to_linker<T, D>(
170             linker: &mut wasmtime::component::Linker<T>,
171             host_getter: fn(&mut T) -> D::Data<'_>,
172         ) -> wasmtime::Result<()>
173         where
174             D: foo::foo::http_types::HostWithStore + http_fetch::HostWithStore,
175             for<'a> D::Data<'a>: foo::foo::http_types::Host + http_fetch::Host,
176             T: 'static,
177         {
178             foo::foo::http_types::add_to_linker::<T, D>(linker, host_getter)?;
179             http_fetch::add_to_linker::<T, D>(linker, host_getter)?;
180             Ok(())
181         }
http_handler(&self) -> &exports::http_handler::Guest182         pub fn http_handler(&self) -> &exports::http_handler::Guest {
183             &self.interface0
184         }
185     }
186 };
187 pub mod foo {
188     pub mod foo {
189         #[allow(clippy::all)]
190         pub mod http_types {
191             #[allow(unused_imports)]
192             use wasmtime::component::__internal::Box;
193             #[derive(wasmtime::component::ComponentType)]
194             #[derive(wasmtime::component::Lift)]
195             #[derive(wasmtime::component::Lower)]
196             #[component(record)]
197             #[derive(Clone)]
198             pub struct Request {
199                 #[component(name = "method")]
200                 pub method: wasmtime::component::__internal::String,
201             }
202             impl core::fmt::Debug for Request {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result203                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
204                     f.debug_struct("Request").field("method", &self.method).finish()
205                 }
206             }
207             const _: () = {
208                 assert!(8 == < Request as wasmtime::component::ComponentType >::SIZE32);
209                 assert!(4 == < Request as wasmtime::component::ComponentType >::ALIGN32);
210             };
211             #[derive(wasmtime::component::ComponentType)]
212             #[derive(wasmtime::component::Lift)]
213             #[derive(wasmtime::component::Lower)]
214             #[component(record)]
215             #[derive(Clone)]
216             pub struct Response {
217                 #[component(name = "body")]
218                 pub body: wasmtime::component::__internal::String,
219             }
220             impl core::fmt::Debug for Response {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result221                 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
222                     f.debug_struct("Response").field("body", &self.body).finish()
223                 }
224             }
225             const _: () = {
226                 assert!(8 == < Response as wasmtime::component::ComponentType >::SIZE32);
227                 assert!(
228                     4 == < Response as wasmtime::component::ComponentType >::ALIGN32
229                 );
230             };
231             pub trait HostWithStore: wasmtime::component::HasData {}
232             impl<_T: ?Sized> HostWithStore for _T
233             where
234                 _T: wasmtime::component::HasData,
235             {}
236             pub trait Host {}
237             impl<_T: Host + ?Sized> Host for &mut _T {}
add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static,238             pub fn add_to_linker<T, D>(
239                 linker: &mut wasmtime::component::Linker<T>,
240                 host_getter: fn(&mut T) -> D::Data<'_>,
241             ) -> wasmtime::Result<()>
242             where
243                 D: HostWithStore,
244                 for<'a> D::Data<'a>: Host,
245                 T: 'static,
246             {
247                 let mut inst = linker.instance("foo:foo/http-types")?;
248                 Ok(())
249             }
250         }
251     }
252 }
253 #[allow(clippy::all)]
254 pub mod http_fetch {
255     #[allow(unused_imports)]
256     use wasmtime::component::__internal::Box;
257     pub type Request = super::foo::foo::http_types::Request;
258     const _: () = {
259         assert!(8 == < Request as wasmtime::component::ComponentType >::SIZE32);
260         assert!(4 == < Request as wasmtime::component::ComponentType >::ALIGN32);
261     };
262     pub type Response = super::foo::foo::http_types::Response;
263     const _: () = {
264         assert!(8 == < Response as wasmtime::component::ComponentType >::SIZE32);
265         assert!(4 == < Response as wasmtime::component::ComponentType >::ALIGN32);
266     };
267     pub trait HostWithStore: wasmtime::component::HasData {}
268     impl<_T: ?Sized> HostWithStore for _T
269     where
270         _T: wasmtime::component::HasData,
271     {}
272     pub trait Host {
fetch_request(&mut self, request: Request) -> Response273         fn fetch_request(&mut self, request: Request) -> Response;
274     }
275     impl<_T: Host + ?Sized> Host for &mut _T {
fetch_request(&mut self, request: Request) -> Response276         fn fetch_request(&mut self, request: Request) -> Response {
277             Host::fetch_request(*self, request)
278         }
279     }
add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static,280     pub fn add_to_linker<T, D>(
281         linker: &mut wasmtime::component::Linker<T>,
282         host_getter: fn(&mut T) -> D::Data<'_>,
283     ) -> wasmtime::Result<()>
284     where
285         D: HostWithStore,
286         for<'a> D::Data<'a>: Host,
287         T: 'static,
288     {
289         let mut inst = linker.instance("http-fetch")?;
290         inst.func_wrap(
291             "fetch-request",
292             move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (Request,)| {
293                 let host = &mut host_getter(caller.data_mut());
294                 let r = Host::fetch_request(host, arg0);
295                 Ok((r,))
296             },
297         )?;
298         Ok(())
299     }
300 }
301 pub mod exports {
302     #[allow(clippy::all)]
303     pub mod http_handler {
304         #[allow(unused_imports)]
305         use wasmtime::component::__internal::Box;
306         pub type Request = super::super::foo::foo::http_types::Request;
307         const _: () = {
308             assert!(8 == < Request as wasmtime::component::ComponentType >::SIZE32);
309             assert!(4 == < Request as wasmtime::component::ComponentType >::ALIGN32);
310         };
311         pub type Response = super::super::foo::foo::http_types::Response;
312         const _: () = {
313             assert!(8 == < Response as wasmtime::component::ComponentType >::SIZE32);
314             assert!(4 == < Response as wasmtime::component::ComponentType >::ALIGN32);
315         };
316         #[derive(Clone)]
317         pub struct Guest {
318             handle_request: wasmtime::component::Func,
319         }
320         #[derive(Clone)]
321         pub struct GuestIndices {
322             handle_request: wasmtime::component::ComponentExportIndex,
323         }
324         impl GuestIndices {
325             /// Constructor for [`GuestIndices`] which takes a
326             /// [`Component`](wasmtime::component::Component) as input and can be executed
327             /// before instantiation.
328             ///
329             /// This constructor can be used to front-load string lookups to find exports
330             /// within a component.
new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<GuestIndices>331             pub fn new<_T>(
332                 _instance_pre: &wasmtime::component::InstancePre<_T>,
333             ) -> wasmtime::Result<GuestIndices> {
334                 let instance = _instance_pre
335                     .component()
336                     .get_export_index(None, "http-handler")
337                     .ok_or_else(|| {
338                         wasmtime::format_err!(
339                             "no exported instance named `http-handler`"
340                         )
341                     })?;
342                 let mut lookup = move |name| {
343                     _instance_pre
344                         .component()
345                         .get_export_index(Some(&instance), name)
346                         .ok_or_else(|| {
347                             wasmtime::format_err!(
348                                 "instance export `http-handler` does \
349               not have export `{name}`"
350                             )
351                         })
352                 };
353                 let _ = &mut lookup;
354                 let handle_request = lookup("handle-request")?;
355                 Ok(GuestIndices { handle_request })
356             }
load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Guest>357             pub fn load(
358                 &self,
359                 mut store: impl wasmtime::AsContextMut,
360                 instance: &wasmtime::component::Instance,
361             ) -> wasmtime::Result<Guest> {
362                 let _instance = instance;
363                 let _instance_pre = _instance.instance_pre(&store);
364                 let _instance_type = _instance_pre.instance_type();
365                 let mut store = store.as_context_mut();
366                 let _ = &mut store;
367                 let handle_request = *_instance
368                     .get_typed_func::<
369                         (&Request,),
370                         (Response,),
371                     >(&mut store, &self.handle_request)?
372                     .func();
373                 Ok(Guest { handle_request })
374             }
375         }
376         impl Guest {
call_handle_request<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: &Request, ) -> wasmtime::Result<Response>377             pub fn call_handle_request<S: wasmtime::AsContextMut>(
378                 &self,
379                 mut store: S,
380                 arg0: &Request,
381             ) -> wasmtime::Result<Response> {
382                 let callee = unsafe {
383                     wasmtime::component::TypedFunc::<
384                         (&Request,),
385                         (Response,),
386                     >::new_unchecked(self.handle_request)
387                 };
388                 let (ret0,) = callee.call(store.as_context_mut(), (arg0,))?;
389                 Ok(ret0)
390             }
391         }
392     }
393 }
394