1 pub struct Foo { 2 new: wasmtime::component::Func, 3 } 4 const _: () = { 5 #[allow(unused_imports)] 6 use wasmtime::component::__internal::anyhow; 7 impl Foo { 8 /// Instantiates the provided `module` using the specified 9 /// parameters, wrapping up the result in a structure that 10 /// translates between wasm and the host. 11 pub async fn instantiate_async<T: Send>( 12 mut store: impl wasmtime::AsContextMut<Data = T>, 13 component: &wasmtime::component::Component, 14 linker: &wasmtime::component::Linker<T>, 15 ) -> wasmtime::Result<(Self, wasmtime::component::Instance)> { 16 let instance = linker.instantiate_async(&mut store, component).await?; 17 Ok((Self::new(store, &instance)?, instance)) 18 } 19 /// Instantiates a pre-instantiated module using the specified 20 /// parameters, wrapping up the result in a structure that 21 /// translates between wasm and the host. 22 pub async fn instantiate_pre<T: Send>( 23 mut store: impl wasmtime::AsContextMut<Data = T>, 24 instance_pre: &wasmtime::component::InstancePre<T>, 25 ) -> wasmtime::Result<(Self, wasmtime::component::Instance)> { 26 let instance = instance_pre.instantiate_async(&mut store).await?; 27 Ok((Self::new(store, &instance)?, instance)) 28 } 29 /// Low-level creation wrapper for wrapping up the exports 30 /// of the `instance` provided in this structure of wasm 31 /// exports. 32 /// 33 /// This function will extract exports from the `instance` 34 /// defined within `store` and wrap them all up in the 35 /// returned structure which can be used to interact with 36 /// the wasm module. 37 pub fn new( 38 mut store: impl wasmtime::AsContextMut, 39 instance: &wasmtime::component::Instance, 40 ) -> wasmtime::Result<Self> { 41 let mut store = store.as_context_mut(); 42 let mut exports = instance.exports(&mut store); 43 let mut __exports = exports.root(); 44 let new = *__exports.typed_func::<(), ()>("new")?.func(); 45 Ok(Foo { new }) 46 } 47 pub async fn call_new<S: wasmtime::AsContextMut>( 48 &self, 49 mut store: S, 50 ) -> wasmtime::Result<()> 51 where 52 <S as wasmtime::AsContext>::Data: Send, 53 { 54 let callee = unsafe { 55 wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new) 56 }; 57 let () = callee.call_async(store.as_context_mut(), ()).await?; 58 callee.post_return_async(store.as_context_mut()).await?; 59 Ok(()) 60 } 61 } 62 }; 63