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_async`] or by first creating 24 /// a [`FooPre`] followed by using 25 /// [`FooPre::instantiate_async`]. 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 async fn instantiate_async( 56 &self, 57 mut store: impl wasmtime::AsContextMut<Data = _T>, 58 ) -> wasmtime::Result<Foo> 59 where 60 _T: Send, 61 { 62 let mut store = store.as_context_mut(); 63 let _instance = self.instance_pre.instantiate_async(&mut store).await?; 64 let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func(); 65 Ok(Foo { new }) 66 } 67 pub fn engine(&self) -> &wasmtime::Engine { 68 self.instance_pre.engine() 69 } 70 pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 71 &self.instance_pre 72 } 73 } 74 impl Foo { 75 /// Convenience wrapper around [`FooPre::new`] and 76 /// [`FooPre::instantiate_async`]. 77 pub async fn instantiate_async<_T>( 78 mut store: impl wasmtime::AsContextMut<Data = _T>, 79 component: &wasmtime::component::Component, 80 linker: &wasmtime::component::Linker<_T>, 81 ) -> wasmtime::Result<Foo> 82 where 83 _T: Send, 84 { 85 let pre = linker.instantiate_pre(component)?; 86 FooPre::new(pre)?.instantiate_async(store).await 87 } 88 pub async fn call_new<S: wasmtime::AsContextMut>( 89 &self, 90 mut store: S, 91 ) -> wasmtime::Result<()> 92 where 93 <S as wasmtime::AsContext>::Data: Send, 94 { 95 let callee = unsafe { 96 wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new) 97 }; 98 let () = callee.call_async(store.as_context_mut(), ()).await?; 99 callee.post_return_async(store.as_context_mut()).await?; 100 Ok(()) 101 } 102 } 103 }; 104