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 /// 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 /// These methods are all equivalent to one another and move
86 /// around the tradeoff of what work is performed when.
87 ///
88 /// [`Store`]: wasmtime::Store
89 /// [`Component`]: wasmtime::component::Component
90 /// [`Linker`]: wasmtime::component::Linker
91 pub struct Foo {
92     new: wasmtime::component::Func,
93 }
94 const _: () = {
95     #[allow(unused_imports)]
96     use wasmtime::component::__internal::anyhow;
97     impl FooIndices {
98         /// Creates a new copy of `FooIndices` bindings which can then
99         /// be used to instantiate into a particular store.
100         ///
101         /// This method may fail if the component does not have the
102         /// required exports.
103         pub fn new<_T>(
104             _instance_pre: &wasmtime::component::InstancePre<_T>,
105         ) -> wasmtime::Result<Self> {
106             let _component = _instance_pre.component();
107             let _instance_type = _instance_pre.instance_type();
108             let new = {
109                 let (item, index) = _component
110                     .get_export(None, "new")
111                     .ok_or_else(|| anyhow::anyhow!("no export `new` found"))?;
112                 match item {
113                     wasmtime::component::types::ComponentItem::ComponentFunc(func) => {
114                         anyhow::Context::context(
115                             func.typecheck::<(), ()>(&_instance_type),
116                             "type-checking export func `new`",
117                         )?;
118                         index
119                     }
120                     _ => Err(anyhow::anyhow!("export `new` is not a function"))?,
121                 }
122             };
123             Ok(FooIndices { new })
124         }
125         /// Uses the indices stored in `self` to load an instance
126         /// of [`Foo`] from the instance provided.
127         ///
128         /// Note that at this time this method will additionally
129         /// perform type-checks of all exports.
130         pub fn load(
131             &self,
132             mut store: impl wasmtime::AsContextMut,
133             instance: &wasmtime::component::Instance,
134         ) -> wasmtime::Result<Foo> {
135             let _ = &mut store;
136             let _instance = instance;
137             let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func();
138             Ok(Foo { new })
139         }
140     }
141     impl Foo {
142         /// Convenience wrapper around [`FooPre::new`] and
143         /// [`FooPre::instantiate`].
144         pub fn instantiate<_T>(
145             store: impl wasmtime::AsContextMut<Data = _T>,
146             component: &wasmtime::component::Component,
147             linker: &wasmtime::component::Linker<_T>,
148         ) -> wasmtime::Result<Foo> {
149             let pre = linker.instantiate_pre(component)?;
150             FooPre::new(pre)?.instantiate(store)
151         }
152         /// Convenience wrapper around [`FooIndices::new`] and
153         /// [`FooIndices::load`].
154         pub fn new(
155             mut store: impl wasmtime::AsContextMut,
156             instance: &wasmtime::component::Instance,
157         ) -> wasmtime::Result<Foo> {
158             let indices = FooIndices::new(&instance.instance_pre(&store))?;
159             indices.load(&mut store, instance)
160         }
161         pub fn call_new<S: wasmtime::AsContextMut>(
162             &self,
163             mut store: S,
164         ) -> wasmtime::Result<()> {
165             let callee = unsafe {
166                 wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new)
167             };
168             let () = callee.call(store.as_context_mut(), ())?;
169             callee.post_return(store.as_context_mut())?;
170             Ok(())
171         }
172     }
173 };
174