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