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> {
14     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.
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     }
33     pub fn engine(&self) -> &wasmtime::Engine {
34         self.instance_pre.engine()
35     }
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.
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`.
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     new: wasmtime::component::ComponentExportIndex,
76 }
77 /// Auto-generated bindings for an instance a component which
78 /// implements the world `foo`.
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 ///   [`Foo::instantiate`] which only needs a
85 ///   [`Store`], [`Component`], and [`Linker`].
86 ///
87 /// * Alternatively you can create a [`FooPre`] 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 [`FooPre::instantiate`] to
91 ///   create a [`Foo`].
92 ///
93 /// * If you've instantiated the instance yourself already
94 ///   then you can use [`Foo::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 Foo {
103     new: wasmtime::component::Func,
104 }
105 const _: () = {
106     #[allow(unused_imports)]
107     use wasmtime::component::__internal::anyhow;
108     impl FooIndices {
109         /// Creates a new copy of `FooIndices` bindings which can then
110         /// be used to instantiate into a particular store.
111         ///
112         /// This method may fail if the component does not have the
113         /// required exports.
114         pub fn new<_T>(
115             _instance_pre: &wasmtime::component::InstancePre<_T>,
116         ) -> wasmtime::Result<Self> {
117             let _component = _instance_pre.component();
118             let _instance_type = _instance_pre.instance_type();
119             let new = {
120                 let (item, index) = _component
121                     .get_export(None, "new")
122                     .ok_or_else(|| anyhow::anyhow!("no export `new` found"))?;
123                 match item {
124                     wasmtime::component::types::ComponentItem::ComponentFunc(func) => {
125                         anyhow::Context::context(
126                             func.typecheck::<(), ()>(&_instance_type),
127                             "type-checking export func `new`",
128                         )?;
129                         index
130                     }
131                     _ => Err(anyhow::anyhow!("export `new` is not a function"))?,
132                 }
133             };
134             Ok(FooIndices { new })
135         }
136         /// Uses the indices stored in `self` to load an instance
137         /// of [`Foo`] from the instance provided.
138         ///
139         /// Note that at this time this method will additionally
140         /// perform type-checks of all exports.
141         pub fn load(
142             &self,
143             mut store: impl wasmtime::AsContextMut,
144             instance: &wasmtime::component::Instance,
145         ) -> wasmtime::Result<Foo> {
146             let _ = &mut store;
147             let _instance = instance;
148             let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func();
149             Ok(Foo { new })
150         }
151     }
152     impl Foo {
153         /// Convenience wrapper around [`FooPre::new`] and
154         /// [`FooPre::instantiate`].
155         pub fn instantiate<_T>(
156             store: impl wasmtime::AsContextMut<Data = _T>,
157             component: &wasmtime::component::Component,
158             linker: &wasmtime::component::Linker<_T>,
159         ) -> wasmtime::Result<Foo> {
160             let pre = linker.instantiate_pre(component)?;
161             FooPre::new(pre)?.instantiate(store)
162         }
163         /// Convenience wrapper around [`FooIndices::new`] and
164         /// [`FooIndices::load`].
165         pub fn new(
166             mut store: impl wasmtime::AsContextMut,
167             instance: &wasmtime::component::Instance,
168         ) -> wasmtime::Result<Foo> {
169             let indices = FooIndices::new(&instance.instance_pre(&store))?;
170             indices.load(&mut store, instance)
171         }
172         /// Convenience wrapper around [`FooPre::new`] and
173         /// [`FooPre::instantiate_async`].
174         pub async fn instantiate_async<_T>(
175             store: impl wasmtime::AsContextMut<Data = _T>,
176             component: &wasmtime::component::Component,
177             linker: &wasmtime::component::Linker<_T>,
178         ) -> wasmtime::Result<Foo>
179         where
180             _T: Send,
181         {
182             let pre = linker.instantiate_pre(component)?;
183             FooPre::new(pre)?.instantiate_async(store).await
184         }
185         pub async fn call_new<_T, _D>(
186             &self,
187             accessor: &wasmtime::component::Accessor<_T, _D>,
188         ) -> wasmtime::Result<()>
189         where
190             _T: Send,
191             _D: wasmtime::component::HasData,
192         {
193             let callee = unsafe {
194                 wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new)
195             };
196             let ((), _) = callee.call_concurrent(accessor, ()).await?;
197             Ok(())
198         }
199     }
200 };
201