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