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`] or by first creating 16 /// a [`FooPre`] followed by using 17 /// [`FooPre::instantiate`]. 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 fn instantiate( 48 &self, 49 mut store: impl wasmtime::AsContextMut<Data = _T>, 50 ) -> wasmtime::Result<Foo> { 51 let mut store = store.as_context_mut(); 52 let _instance = self.instance_pre.instantiate(&mut store)?; 53 let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func(); 54 Ok(Foo { new }) 55 } 56 } 57 impl Foo { 58 /// Convenience wrapper around [`FooPre::new`] and 59 /// [`FooPre::instantiate`]. 60 pub fn instantiate<_T>( 61 mut store: impl wasmtime::AsContextMut<Data = _T>, 62 component: &wasmtime::component::Component, 63 linker: &wasmtime::component::Linker<_T>, 64 ) -> wasmtime::Result<Foo> { 65 let pre = linker.instantiate_pre(component)?; 66 FooPre::new(pre)?.instantiate(store) 67 } 68 pub fn call_new<S: wasmtime::AsContextMut>( 69 &self, 70 mut store: S, 71 ) -> wasmtime::Result<()> { 72 let callee = unsafe { 73 wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new) 74 }; 75 let () = callee.call(store.as_context_mut(), ())?; 76 callee.post_return(store.as_context_mut())?; 77 Ok(()) 78 } 79 } 80 }; 81