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