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> {
10     instance_pre: wasmtime::component::InstancePre<T>,
11     indices: FooIndices,
12 }
13 impl<T> 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> 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.component())?;
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 /// Auto-generated bindings for index of the exports of
56 /// `foo`.
57 ///
58 /// This is an implementation detail of [`FooPre`] and can
59 /// be constructed if needed as well.
60 ///
61 /// For more information see [`Foo`] as well.
62 #[derive(Clone)]
63 pub struct FooIndices {
64     new: wasmtime::component::ComponentExportIndex,
65 }
66 /// Auto-generated bindings for an instance a component which
67 /// implements the world `foo`.
68 ///
69 /// This structure can be created through a number of means
70 /// depending on your requirements and what you have on hand:
71 ///
72 /// * The most convenient way is to use
73 ///   [`Foo::instantiate`] which only needs a
74 ///   [`Store`], [`Component`], and [`Linker`].
75 ///
76 /// * Alternatively you can create a [`FooPre`] ahead of
77 ///   time with a [`Component`] to front-load string lookups
78 ///   of exports once instead of per-instantiation. This
79 ///   method then uses [`FooPre::instantiate`] to
80 ///   create a [`Foo`].
81 ///
82 /// * If you've instantiated the instance yourself already
83 ///   then you can use [`Foo::new`].
84 ///
85 /// * You can also access the guts of instantiation through
86 ///   [`FooIndices::new_instance`] followed
87 ///   by [`FooIndices::load`] to crate an instance of this
88 ///   type.
89 ///
90 /// These methods are all equivalent to one another and move
91 /// around the tradeoff of what work is performed when.
92 ///
93 /// [`Store`]: wasmtime::Store
94 /// [`Component`]: wasmtime::component::Component
95 /// [`Linker`]: wasmtime::component::Linker
96 pub struct Foo {
97     new: wasmtime::component::Func,
98 }
99 const _: () = {
100     #[allow(unused_imports)]
101     use wasmtime::component::__internal::anyhow;
102     impl FooIndices {
103         /// Creates a new copy of `FooIndices` bindings which can then
104         /// be used to instantiate into a particular store.
105         ///
106         /// This method may fail if the component does not have the
107         /// required exports.
108         pub fn new(
109             component: &wasmtime::component::Component,
110         ) -> wasmtime::Result<Self> {
111             let _component = component;
112             let new = _component
113                 .export_index(None, "new")
114                 .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?
115                 .1;
116             Ok(FooIndices { new })
117         }
118         /// Creates a new instance of [`FooIndices`] from an
119         /// instantiated component.
120         ///
121         /// This method of creating a [`Foo`] will perform string
122         /// lookups for all exports when this method is called. This
123         /// will only succeed if the provided instance matches the
124         /// requirements of [`Foo`].
125         pub fn new_instance(
126             mut store: impl wasmtime::AsContextMut,
127             instance: &wasmtime::component::Instance,
128         ) -> wasmtime::Result<Self> {
129             let _instance = instance;
130             let new = _instance
131                 .get_export(&mut store, None, "new")
132                 .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?;
133             Ok(FooIndices { new })
134         }
135         /// Uses the indices stored in `self` to load an instance
136         /// of [`Foo`] from the instance provided.
137         ///
138         /// Note that at this time this method will additionally
139         /// perform type-checks of all exports.
140         pub fn load(
141             &self,
142             mut store: impl wasmtime::AsContextMut,
143             instance: &wasmtime::component::Instance,
144         ) -> wasmtime::Result<Foo> {
145             let _instance = instance;
146             let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func();
147             Ok(Foo { new })
148         }
149     }
150     impl Foo {
151         /// Convenience wrapper around [`FooPre::new`] and
152         /// [`FooPre::instantiate`].
153         pub fn instantiate<_T>(
154             mut store: impl wasmtime::AsContextMut<Data = _T>,
155             component: &wasmtime::component::Component,
156             linker: &wasmtime::component::Linker<_T>,
157         ) -> wasmtime::Result<Foo> {
158             let pre = linker.instantiate_pre(component)?;
159             FooPre::new(pre)?.instantiate(store)
160         }
161         /// Convenience wrapper around [`FooIndices::new_instance`] and
162         /// [`FooIndices::load`].
163         pub fn new(
164             mut store: impl wasmtime::AsContextMut,
165             instance: &wasmtime::component::Instance,
166         ) -> wasmtime::Result<Foo> {
167             let indices = FooIndices::new_instance(&mut store, instance)?;
168             indices.load(store, instance)
169         }
170         pub fn call_new<S: wasmtime::AsContextMut>(
171             &self,
172             mut store: S,
173         ) -> wasmtime::Result<()>
174         where
175             <S as wasmtime::AsContext>::Data: Send,
176         {
177             let callee = unsafe {
178                 wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new)
179             };
180             let () = callee.call(store.as_context_mut(), ())?;
181             callee.post_return(store.as_context_mut())?;
182             Ok(())
183         }
184     }
185 };
186