1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// component which implements the world `the-world`. 3 /// 4 /// This structure is created through [`TheWorldPre::new`] which 5 /// takes a [`InstancePre`](wasmtime::component::InstancePre) that 6 /// has been created through a [`Linker`](wasmtime::component::Linker). 7 pub struct TheWorldPre<T> { 8 instance_pre: wasmtime::component::InstancePre<T>, 9 interface0: exports::foo::foo::simple::GuestPre, 10 } 11 impl<T> Clone for TheWorldPre<T> { 12 fn clone(&self) -> Self { 13 Self { 14 instance_pre: self.instance_pre.clone(), 15 interface0: self.interface0.clone(), 16 } 17 } 18 } 19 /// Auto-generated bindings for an instance a component which 20 /// implements the world `the-world`. 21 /// 22 /// This structure is created through either 23 /// [`TheWorld::instantiate_async`] or by first creating 24 /// a [`TheWorldPre`] followed by using 25 /// [`TheWorldPre::instantiate_async`]. 26 pub struct TheWorld { 27 interface0: exports::foo::foo::simple::Guest, 28 } 29 const _: () = { 30 #[allow(unused_imports)] 31 use wasmtime::component::__internal::anyhow; 32 impl<_T> TheWorldPre<_T> { 33 /// Creates a new copy of `TheWorldPre` 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 interface0 = exports::foo::foo::simple::GuestPre::new(_component)?; 43 Ok(TheWorldPre { 44 instance_pre, 45 interface0, 46 }) 47 } 48 /// Instantiates a new instance of [`TheWorld`] 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<TheWorld> 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 interface0 = self.interface0.load(&mut store, &_instance)?; 65 Ok(TheWorld { interface0 }) 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 TheWorld { 75 /// Convenience wrapper around [`TheWorldPre::new`] and 76 /// [`TheWorldPre::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<TheWorld> 82 where 83 _T: Send, 84 { 85 let pre = linker.instantiate_pre(component)?; 86 TheWorldPre::new(pre)?.instantiate_async(store).await 87 } 88 pub fn add_to_linker<T, U>( 89 linker: &mut wasmtime::component::Linker<T>, 90 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 91 ) -> wasmtime::Result<()> 92 where 93 T: Send, 94 U: foo::foo::simple::Host + Send, 95 { 96 foo::foo::simple::add_to_linker(linker, get)?; 97 Ok(()) 98 } 99 pub fn foo_foo_simple(&self) -> &exports::foo::foo::simple::Guest { 100 &self.interface0 101 } 102 } 103 }; 104 pub mod foo { 105 pub mod foo { 106 #[allow(clippy::all)] 107 pub mod simple { 108 #[allow(unused_imports)] 109 use wasmtime::component::__internal::anyhow; 110 #[wasmtime::component::__internal::async_trait] 111 pub trait Host: Send { 112 async fn f1(&mut self) -> (); 113 async fn f2(&mut self, a: u32) -> (); 114 async fn f3(&mut self, a: u32, b: u32) -> (); 115 async fn f4(&mut self) -> u32; 116 async fn f5(&mut self) -> (u32, u32); 117 async fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32); 118 } 119 pub trait GetHost< 120 T, 121 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 122 type Host: Host + Send; 123 } 124 impl<F, T, O> GetHost<T> for F 125 where 126 F: Fn(T) -> O + Send + Sync + Copy + 'static, 127 O: Host + Send, 128 { 129 type Host = O; 130 } 131 pub fn add_to_linker_get_host<T>( 132 linker: &mut wasmtime::component::Linker<T>, 133 host_getter: impl for<'a> GetHost<&'a mut T>, 134 ) -> wasmtime::Result<()> 135 where 136 T: Send, 137 { 138 let mut inst = linker.instance("foo:foo/simple")?; 139 inst.func_wrap_async( 140 "f1", 141 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| wasmtime::component::__internal::Box::new(async move { 142 let host = &mut host_getter(caller.data_mut()); 143 let r = Host::f1(host).await; 144 Ok(r) 145 }), 146 )?; 147 inst.func_wrap_async( 148 "f2", 149 move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (u32,)| wasmtime::component::__internal::Box::new(async move { 150 let host = &mut host_getter(caller.data_mut()); 151 let r = Host::f2(host, arg0).await; 152 Ok(r) 153 }), 154 )?; 155 inst.func_wrap_async( 156 "f3", 157 move | 158 mut caller: wasmtime::StoreContextMut<'_, T>, 159 (arg0, arg1): (u32, u32)| 160 wasmtime::component::__internal::Box::new(async move { 161 let host = &mut host_getter(caller.data_mut()); 162 let r = Host::f3(host, arg0, arg1).await; 163 Ok(r) 164 }), 165 )?; 166 inst.func_wrap_async( 167 "f4", 168 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| wasmtime::component::__internal::Box::new(async move { 169 let host = &mut host_getter(caller.data_mut()); 170 let r = Host::f4(host).await; 171 Ok((r,)) 172 }), 173 )?; 174 inst.func_wrap_async( 175 "f5", 176 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| wasmtime::component::__internal::Box::new(async move { 177 let host = &mut host_getter(caller.data_mut()); 178 let r = Host::f5(host).await; 179 Ok((r,)) 180 }), 181 )?; 182 inst.func_wrap_async( 183 "f6", 184 move | 185 mut caller: wasmtime::StoreContextMut<'_, T>, 186 (arg0, arg1, arg2): (u32, u32, u32)| 187 wasmtime::component::__internal::Box::new(async move { 188 let host = &mut host_getter(caller.data_mut()); 189 let r = Host::f6(host, arg0, arg1, arg2).await; 190 Ok((r,)) 191 }), 192 )?; 193 Ok(()) 194 } 195 pub fn add_to_linker<T, U>( 196 linker: &mut wasmtime::component::Linker<T>, 197 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 198 ) -> wasmtime::Result<()> 199 where 200 U: Host + Send, 201 T: Send, 202 { 203 add_to_linker_get_host(linker, get) 204 } 205 #[wasmtime::component::__internal::async_trait] 206 impl<_T: Host + ?Sized + Send> Host for &mut _T { 207 async fn f1(&mut self) -> () { 208 Host::f1(*self).await 209 } 210 async fn f2(&mut self, a: u32) -> () { 211 Host::f2(*self, a).await 212 } 213 async fn f3(&mut self, a: u32, b: u32) -> () { 214 Host::f3(*self, a, b).await 215 } 216 async fn f4(&mut self) -> u32 { 217 Host::f4(*self).await 218 } 219 async fn f5(&mut self) -> (u32, u32) { 220 Host::f5(*self).await 221 } 222 async fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32) { 223 Host::f6(*self, a, b, c).await 224 } 225 } 226 } 227 } 228 } 229 pub mod exports { 230 pub mod foo { 231 pub mod foo { 232 #[allow(clippy::all)] 233 pub mod simple { 234 #[allow(unused_imports)] 235 use wasmtime::component::__internal::anyhow; 236 pub struct Guest { 237 f1: wasmtime::component::Func, 238 f2: wasmtime::component::Func, 239 f3: wasmtime::component::Func, 240 f4: wasmtime::component::Func, 241 f5: wasmtime::component::Func, 242 f6: wasmtime::component::Func, 243 } 244 #[derive(Clone)] 245 pub struct GuestPre { 246 f1: wasmtime::component::ComponentExportIndex, 247 f2: wasmtime::component::ComponentExportIndex, 248 f3: wasmtime::component::ComponentExportIndex, 249 f4: wasmtime::component::ComponentExportIndex, 250 f5: wasmtime::component::ComponentExportIndex, 251 f6: wasmtime::component::ComponentExportIndex, 252 } 253 impl GuestPre { 254 pub fn new( 255 component: &wasmtime::component::Component, 256 ) -> wasmtime::Result<GuestPre> { 257 let _component = component; 258 let (_, instance) = component 259 .export_index(None, "foo:foo/simple") 260 .ok_or_else(|| { 261 anyhow::anyhow!( 262 "no exported instance named `foo:foo/simple`" 263 ) 264 })?; 265 let _lookup = |name: &str| { 266 _component 267 .export_index(Some(&instance), name) 268 .map(|p| p.1) 269 .ok_or_else(|| { 270 anyhow::anyhow!( 271 "instance export `foo:foo/simple` does \ 272 not have export `{name}`" 273 ) 274 }) 275 }; 276 let f1 = _lookup("f1")?; 277 let f2 = _lookup("f2")?; 278 let f3 = _lookup("f3")?; 279 let f4 = _lookup("f4")?; 280 let f5 = _lookup("f5")?; 281 let f6 = _lookup("f6")?; 282 Ok(GuestPre { f1, f2, f3, f4, f5, f6 }) 283 } 284 pub fn load( 285 &self, 286 mut store: impl wasmtime::AsContextMut, 287 instance: &wasmtime::component::Instance, 288 ) -> wasmtime::Result<Guest> { 289 let mut store = store.as_context_mut(); 290 let _ = &mut store; 291 let _instance = instance; 292 let f1 = *_instance 293 .get_typed_func::<(), ()>(&mut store, &self.f1)? 294 .func(); 295 let f2 = *_instance 296 .get_typed_func::<(u32,), ()>(&mut store, &self.f2)? 297 .func(); 298 let f3 = *_instance 299 .get_typed_func::<(u32, u32), ()>(&mut store, &self.f3)? 300 .func(); 301 let f4 = *_instance 302 .get_typed_func::<(), (u32,)>(&mut store, &self.f4)? 303 .func(); 304 let f5 = *_instance 305 .get_typed_func::<(), ((u32, u32),)>(&mut store, &self.f5)? 306 .func(); 307 let f6 = *_instance 308 .get_typed_func::< 309 (u32, u32, u32), 310 ((u32, u32, u32),), 311 >(&mut store, &self.f6)? 312 .func(); 313 Ok(Guest { f1, f2, f3, f4, f5, f6 }) 314 } 315 } 316 impl Guest { 317 pub async fn call_f1<S: wasmtime::AsContextMut>( 318 &self, 319 mut store: S, 320 ) -> wasmtime::Result<()> 321 where 322 <S as wasmtime::AsContext>::Data: Send, 323 { 324 let callee = unsafe { 325 wasmtime::component::TypedFunc::< 326 (), 327 (), 328 >::new_unchecked(self.f1) 329 }; 330 let () = callee.call_async(store.as_context_mut(), ()).await?; 331 callee.post_return_async(store.as_context_mut()).await?; 332 Ok(()) 333 } 334 pub async fn call_f2<S: wasmtime::AsContextMut>( 335 &self, 336 mut store: S, 337 arg0: u32, 338 ) -> wasmtime::Result<()> 339 where 340 <S as wasmtime::AsContext>::Data: Send, 341 { 342 let callee = unsafe { 343 wasmtime::component::TypedFunc::< 344 (u32,), 345 (), 346 >::new_unchecked(self.f2) 347 }; 348 let () = callee 349 .call_async(store.as_context_mut(), (arg0,)) 350 .await?; 351 callee.post_return_async(store.as_context_mut()).await?; 352 Ok(()) 353 } 354 pub async fn call_f3<S: wasmtime::AsContextMut>( 355 &self, 356 mut store: S, 357 arg0: u32, 358 arg1: u32, 359 ) -> wasmtime::Result<()> 360 where 361 <S as wasmtime::AsContext>::Data: Send, 362 { 363 let callee = unsafe { 364 wasmtime::component::TypedFunc::< 365 (u32, u32), 366 (), 367 >::new_unchecked(self.f3) 368 }; 369 let () = callee 370 .call_async(store.as_context_mut(), (arg0, arg1)) 371 .await?; 372 callee.post_return_async(store.as_context_mut()).await?; 373 Ok(()) 374 } 375 pub async fn call_f4<S: wasmtime::AsContextMut>( 376 &self, 377 mut store: S, 378 ) -> wasmtime::Result<u32> 379 where 380 <S as wasmtime::AsContext>::Data: Send, 381 { 382 let callee = unsafe { 383 wasmtime::component::TypedFunc::< 384 (), 385 (u32,), 386 >::new_unchecked(self.f4) 387 }; 388 let (ret0,) = callee 389 .call_async(store.as_context_mut(), ()) 390 .await?; 391 callee.post_return_async(store.as_context_mut()).await?; 392 Ok(ret0) 393 } 394 pub async fn call_f5<S: wasmtime::AsContextMut>( 395 &self, 396 mut store: S, 397 ) -> wasmtime::Result<(u32, u32)> 398 where 399 <S as wasmtime::AsContext>::Data: Send, 400 { 401 let callee = unsafe { 402 wasmtime::component::TypedFunc::< 403 (), 404 ((u32, u32),), 405 >::new_unchecked(self.f5) 406 }; 407 let (ret0,) = callee 408 .call_async(store.as_context_mut(), ()) 409 .await?; 410 callee.post_return_async(store.as_context_mut()).await?; 411 Ok(ret0) 412 } 413 pub async fn call_f6<S: wasmtime::AsContextMut>( 414 &self, 415 mut store: S, 416 arg0: u32, 417 arg1: u32, 418 arg2: u32, 419 ) -> wasmtime::Result<(u32, u32, u32)> 420 where 421 <S as wasmtime::AsContext>::Data: Send, 422 { 423 let callee = unsafe { 424 wasmtime::component::TypedFunc::< 425 (u32, u32, u32), 426 ((u32, u32, u32),), 427 >::new_unchecked(self.f6) 428 }; 429 let (ret0,) = callee 430 .call_async(store.as_context_mut(), (arg0, arg1, arg2)) 431 .await?; 432 callee.post_return_async(store.as_context_mut()).await?; 433 Ok(ret0) 434 } 435 } 436 } 437 } 438 } 439 } 440