1 /// Auto-generated bindings for a pre-instantiated version of a
2 /// copmonent 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 pub struct FooPre<T> {
8     instance_pre: wasmtime::component::InstancePre<T>,
9     new: wasmtime::component::ComponentExportIndex,
10 }
11 /// Auto-generated bindings for an instance a component which
12 /// implements the world `foo`.
13 ///
14 /// This structure is created through either
15 /// [`Foo::instantiate_async`] or by first creating
16 /// a [`FooPre`] followed by using
17 /// [`FooPre::instantiate_async`].
18 pub struct Foo {
19     new: wasmtime::component::Func,
20 }
21 const _: () = {
22     #[allow(unused_imports)]
23     use wasmtime::component::__internal::anyhow;
24     impl<_T> FooPre<_T> {
25         /// Creates a new copy of `FooPre` bindings which can then
26         /// be used to instantiate into a particular store.
27         ///
28         /// This method may fail if the compoennt behind `instance_pre`
29         /// does not have the required exports.
30         pub fn new(
31             instance_pre: wasmtime::component::InstancePre<_T>,
32         ) -> wasmtime::Result<Self> {
33             let _component = instance_pre.component();
34             let new = _component
35                 .export_index(None, "new")
36                 .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?
37                 .1;
38             Ok(FooPre { instance_pre, new })
39         }
40         /// Instantiates a new instance of [`Foo`] within the
41         /// `store` provided.
42         ///
43         /// This function will use `self` as the pre-instantiated
44         /// instance to perform instantiation. Afterwards the preloaded
45         /// indices in `self` are used to lookup all exports on the
46         /// resulting instance.
47         pub async fn instantiate_async(
48             &self,
49             mut store: impl wasmtime::AsContextMut<Data = _T>,
50         ) -> wasmtime::Result<Foo>
51         where
52             _T: Send,
53         {
54             let mut store = store.as_context_mut();
55             let _instance = self.instance_pre.instantiate_async(&mut store).await?;
56             let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func();
57             Ok(Foo { new })
58         }
59     }
60     impl Foo {
61         /// Convenience wrapper around [`FooPre::new`] and
62         /// [`FooPre::instantiate_async`].
63         pub async fn instantiate_async<_T>(
64             mut store: impl wasmtime::AsContextMut<Data = _T>,
65             component: &wasmtime::component::Component,
66             linker: &wasmtime::component::Linker<_T>,
67         ) -> wasmtime::Result<Foo>
68         where
69             _T: Send,
70         {
71             let pre = linker.instantiate_pre(component)?;
72             FooPre::new(pre)?.instantiate_async(store).await
73         }
74         pub async fn call_new<S: wasmtime::AsContextMut>(
75             &self,
76             mut store: S,
77         ) -> wasmtime::Result<()>
78         where
79             <S as wasmtime::AsContext>::Data: Send,
80         {
81             let callee = unsafe {
82                 wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new)
83             };
84             let () = callee.call_async(store.as_context_mut(), ()).await?;
85             callee.post_return_async(store.as_context_mut()).await?;
86             Ok(())
87         }
88     }
89 };
90