1 pub struct TheWorld { 2 interface0: exports::foo::foo::strings::Guest, 3 } 4 const _: () = { 5 #[allow(unused_imports)] 6 use wasmtime::component::__internal::anyhow; 7 impl TheWorld { 8 pub fn add_to_linker<T, U>( 9 linker: &mut wasmtime::component::Linker<T>, 10 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 11 ) -> wasmtime::Result<()> 12 where 13 U: foo::foo::strings::Host + Send, 14 T: Send, 15 { 16 foo::foo::strings::add_to_linker(linker, get)?; 17 Ok(()) 18 } 19 /// Instantiates the provided `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_async<T: Send>( 23 mut store: impl wasmtime::AsContextMut<Data = T>, 24 component: &wasmtime::component::Component, 25 linker: &wasmtime::component::Linker<T>, 26 ) -> wasmtime::Result<(Self, wasmtime::component::Instance)> { 27 let instance = linker.instantiate_async(&mut store, component).await?; 28 Ok((Self::new(store, &instance)?, instance)) 29 } 30 /// Instantiates a pre-instantiated module using the specified 31 /// parameters, wrapping up the result in a structure that 32 /// translates between wasm and the host. 33 pub async fn instantiate_pre<T: Send>( 34 mut store: impl wasmtime::AsContextMut<Data = T>, 35 instance_pre: &wasmtime::component::InstancePre<T>, 36 ) -> wasmtime::Result<(Self, wasmtime::component::Instance)> { 37 let instance = instance_pre.instantiate_async(&mut store).await?; 38 Ok((Self::new(store, &instance)?, instance)) 39 } 40 /// Low-level creation wrapper for wrapping up the exports 41 /// of the `instance` provided in this structure of wasm 42 /// exports. 43 /// 44 /// This function will extract exports from the `instance` 45 /// defined within `store` and wrap them all up in the 46 /// returned structure which can be used to interact with 47 /// the wasm module. 48 pub fn new( 49 mut store: impl wasmtime::AsContextMut, 50 instance: &wasmtime::component::Instance, 51 ) -> wasmtime::Result<Self> { 52 let mut store = store.as_context_mut(); 53 let mut exports = instance.exports(&mut store); 54 let mut __exports = exports.root(); 55 let interface0 = exports::foo::foo::strings::Guest::new( 56 &mut __exports 57 .instance("foo:foo/strings") 58 .ok_or_else(|| { 59 anyhow::anyhow!( 60 "exported instance `foo:foo/strings` not present" 61 ) 62 })?, 63 )?; 64 Ok(TheWorld { interface0 }) 65 } 66 pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { 67 &self.interface0 68 } 69 } 70 }; 71 pub mod foo { 72 pub mod foo { 73 #[allow(clippy::all)] 74 pub mod strings { 75 #[allow(unused_imports)] 76 use wasmtime::component::__internal::anyhow; 77 #[wasmtime::component::__internal::async_trait] 78 pub trait Host { 79 async fn a(&mut self, x: wasmtime::component::__internal::String) -> (); 80 async fn b(&mut self) -> wasmtime::component::__internal::String; 81 async fn c( 82 &mut self, 83 a: wasmtime::component::__internal::String, 84 b: wasmtime::component::__internal::String, 85 ) -> wasmtime::component::__internal::String; 86 } 87 pub fn add_to_linker<T, U>( 88 linker: &mut wasmtime::component::Linker<T>, 89 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 90 ) -> wasmtime::Result<()> 91 where 92 T: Send, 93 U: Host + Send, 94 { 95 let mut inst = linker.instance("foo:foo/strings")?; 96 inst.func_wrap_async( 97 "a", 98 move | 99 mut caller: wasmtime::StoreContextMut<'_, T>, 100 (arg0,): (wasmtime::component::__internal::String,)| 101 wasmtime::component::__internal::Box::new(async move { 102 let host = get(caller.data_mut()); 103 let r = Host::a(host, arg0).await; 104 Ok(r) 105 }), 106 )?; 107 inst.func_wrap_async( 108 "b", 109 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| wasmtime::component::__internal::Box::new(async move { 110 let host = get(caller.data_mut()); 111 let r = Host::b(host).await; 112 Ok((r,)) 113 }), 114 )?; 115 inst.func_wrap_async( 116 "c", 117 move | 118 mut caller: wasmtime::StoreContextMut<'_, T>, 119 ( 120 arg0, 121 arg1, 122 ): ( 123 wasmtime::component::__internal::String, 124 wasmtime::component::__internal::String, 125 )| 126 wasmtime::component::__internal::Box::new(async move { 127 let host = get(caller.data_mut()); 128 let r = Host::c(host, arg0, arg1).await; 129 Ok((r,)) 130 }), 131 )?; 132 Ok(()) 133 } 134 } 135 } 136 } 137 pub mod exports { 138 pub mod foo { 139 pub mod foo { 140 #[allow(clippy::all)] 141 pub mod strings { 142 #[allow(unused_imports)] 143 use wasmtime::component::__internal::anyhow; 144 pub struct Guest { 145 a: wasmtime::component::Func, 146 b: wasmtime::component::Func, 147 c: wasmtime::component::Func, 148 } 149 impl Guest { 150 pub fn new( 151 __exports: &mut wasmtime::component::ExportInstance<'_, '_>, 152 ) -> wasmtime::Result<Guest> { 153 let a = *__exports.typed_func::<(&str,), ()>("a")?.func(); 154 let b = *__exports 155 .typed_func::< 156 (), 157 (wasmtime::component::__internal::String,), 158 >("b")? 159 .func(); 160 let c = *__exports 161 .typed_func::< 162 (&str, &str), 163 (wasmtime::component::__internal::String,), 164 >("c")? 165 .func(); 166 Ok(Guest { a, b, c }) 167 } 168 pub async fn call_a<S: wasmtime::AsContextMut>( 169 &self, 170 mut store: S, 171 arg0: &str, 172 ) -> wasmtime::Result<()> 173 where 174 <S as wasmtime::AsContext>::Data: Send, 175 { 176 let callee = unsafe { 177 wasmtime::component::TypedFunc::< 178 (&str,), 179 (), 180 >::new_unchecked(self.a) 181 }; 182 let () = callee 183 .call_async(store.as_context_mut(), (arg0,)) 184 .await?; 185 callee.post_return_async(store.as_context_mut()).await?; 186 Ok(()) 187 } 188 pub async fn call_b<S: wasmtime::AsContextMut>( 189 &self, 190 mut store: S, 191 ) -> wasmtime::Result<wasmtime::component::__internal::String> 192 where 193 <S as wasmtime::AsContext>::Data: Send, 194 { 195 let callee = unsafe { 196 wasmtime::component::TypedFunc::< 197 (), 198 (wasmtime::component::__internal::String,), 199 >::new_unchecked(self.b) 200 }; 201 let (ret0,) = callee 202 .call_async(store.as_context_mut(), ()) 203 .await?; 204 callee.post_return_async(store.as_context_mut()).await?; 205 Ok(ret0) 206 } 207 pub async fn call_c<S: wasmtime::AsContextMut>( 208 &self, 209 mut store: S, 210 arg0: &str, 211 arg1: &str, 212 ) -> wasmtime::Result<wasmtime::component::__internal::String> 213 where 214 <S as wasmtime::AsContext>::Data: Send, 215 { 216 let callee = unsafe { 217 wasmtime::component::TypedFunc::< 218 (&str, &str), 219 (wasmtime::component::__internal::String,), 220 >::new_unchecked(self.c) 221 }; 222 let (ret0,) = callee 223 .call_async(store.as_context_mut(), (arg0, arg1)) 224 .await?; 225 callee.post_return_async(store.as_context_mut()).await?; 226 Ok(ret0) 227 } 228 } 229 } 230 } 231 } 232 } 233