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 /// 8 /// For more information see [`TheWorld`] as well. 9 pub struct TheWorldPre<T: 'static> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: TheWorldIndices, 12 } 13 impl<T: 'static> Clone for TheWorldPre<T> { 14 fn clone(&self) -> Self { 15 Self { 16 instance_pre: self.instance_pre.clone(), 17 indices: self.indices.clone(), 18 } 19 } 20 } 21 impl<_T: 'static> TheWorldPre<_T> { 22 /// Creates a new copy of `TheWorldPre` bindings which can then 23 /// be used to instantiate into a particular store. 24 /// 25 /// This method may fail if the component behind `instance_pre` 26 /// does not have the required exports. 27 pub fn new( 28 instance_pre: wasmtime::component::InstancePre<_T>, 29 ) -> wasmtime::Result<Self> { 30 let indices = TheWorldIndices::new(&instance_pre)?; 31 Ok(Self { instance_pre, indices }) 32 } 33 pub fn engine(&self) -> &wasmtime::Engine { 34 self.instance_pre.engine() 35 } 36 pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 37 &self.instance_pre 38 } 39 /// Instantiates a new instance of [`TheWorld`] within the 40 /// `store` provided. 41 /// 42 /// This function will use `self` as the pre-instantiated 43 /// instance to perform instantiation. Afterwards the preloaded 44 /// indices in `self` are used to lookup all exports on the 45 /// resulting instance. 46 pub async fn instantiate_async( 47 &self, 48 mut store: impl wasmtime::AsContextMut<Data = _T>, 49 ) -> wasmtime::Result<TheWorld> 50 where 51 _T: Send, 52 { 53 let mut store = store.as_context_mut(); 54 let instance = self.instance_pre.instantiate_async(&mut store).await?; 55 self.indices.load(&mut store, &instance) 56 } 57 } 58 /// Auto-generated bindings for index of the exports of 59 /// `the-world`. 60 /// 61 /// This is an implementation detail of [`TheWorldPre`] and can 62 /// be constructed if needed as well. 63 /// 64 /// For more information see [`TheWorld`] as well. 65 #[derive(Clone)] 66 pub struct TheWorldIndices { 67 interface0: exports::foo::foo::simple::GuestIndices, 68 } 69 /// Auto-generated bindings for an instance a component which 70 /// implements the world `the-world`. 71 /// 72 /// This structure can be created through a number of means 73 /// depending on your requirements and what you have on hand: 74 /// 75 /// * The most convenient way is to use 76 /// [`TheWorld::instantiate_async`] which only needs a 77 /// [`Store`], [`Component`], and [`Linker`]. 78 /// 79 /// * Alternatively you can create a [`TheWorldPre`] ahead of 80 /// time with a [`Component`] to front-load string lookups 81 /// of exports once instead of per-instantiation. This 82 /// method then uses [`TheWorldPre::instantiate_async`] to 83 /// create a [`TheWorld`]. 84 /// 85 /// * If you've instantiated the instance yourself already 86 /// then you can use [`TheWorld::new`]. 87 /// 88 /// These methods are all equivalent to one another and move 89 /// around the tradeoff of what work is performed when. 90 /// 91 /// [`Store`]: wasmtime::Store 92 /// [`Component`]: wasmtime::component::Component 93 /// [`Linker`]: wasmtime::component::Linker 94 pub struct TheWorld { 95 interface0: exports::foo::foo::simple::Guest, 96 } 97 const _: () = { 98 #[allow(unused_imports)] 99 use wasmtime::component::__internal::anyhow; 100 impl TheWorldIndices { 101 /// Creates a new copy of `TheWorldIndices` bindings which can then 102 /// be used to instantiate into a particular store. 103 /// 104 /// This method may fail if the component does not have the 105 /// required exports. 106 pub fn new<_T>( 107 _instance_pre: &wasmtime::component::InstancePre<_T>, 108 ) -> wasmtime::Result<Self> { 109 let _component = _instance_pre.component(); 110 let _instance_type = _instance_pre.instance_type(); 111 let interface0 = exports::foo::foo::simple::GuestIndices::new( 112 _instance_pre, 113 )?; 114 Ok(TheWorldIndices { interface0 }) 115 } 116 /// Uses the indices stored in `self` to load an instance 117 /// of [`TheWorld`] from the instance provided. 118 /// 119 /// Note that at this time this method will additionally 120 /// perform type-checks of all exports. 121 pub fn load( 122 &self, 123 mut store: impl wasmtime::AsContextMut, 124 instance: &wasmtime::component::Instance, 125 ) -> wasmtime::Result<TheWorld> { 126 let _ = &mut store; 127 let _instance = instance; 128 let interface0 = self.interface0.load(&mut store, &_instance)?; 129 Ok(TheWorld { interface0 }) 130 } 131 } 132 impl TheWorld { 133 /// Convenience wrapper around [`TheWorldPre::new`] and 134 /// [`TheWorldPre::instantiate_async`]. 135 pub async fn instantiate_async<_T>( 136 store: impl wasmtime::AsContextMut<Data = _T>, 137 component: &wasmtime::component::Component, 138 linker: &wasmtime::component::Linker<_T>, 139 ) -> wasmtime::Result<TheWorld> 140 where 141 _T: Send, 142 { 143 let pre = linker.instantiate_pre(component)?; 144 TheWorldPre::new(pre)?.instantiate_async(store).await 145 } 146 /// Convenience wrapper around [`TheWorldIndices::new`] and 147 /// [`TheWorldIndices::load`]. 148 pub fn new( 149 mut store: impl wasmtime::AsContextMut, 150 instance: &wasmtime::component::Instance, 151 ) -> wasmtime::Result<TheWorld> { 152 let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; 153 indices.load(&mut store, instance) 154 } 155 pub fn add_to_linker<T, D>( 156 linker: &mut wasmtime::component::Linker<T>, 157 get: fn(&mut T) -> D::Data<'_>, 158 ) -> wasmtime::Result<()> 159 where 160 D: wasmtime::component::HasData, 161 for<'a> D::Data<'a>: foo::foo::simple::Host + Send, 162 T: Send + 'static, 163 { 164 foo::foo::simple::add_to_linker::<T, D>(linker, get)?; 165 Ok(()) 166 } 167 pub fn foo_foo_simple(&self) -> &exports::foo::foo::simple::Guest { 168 &self.interface0 169 } 170 } 171 }; 172 pub mod foo { 173 pub mod foo { 174 #[allow(clippy::all)] 175 pub mod simple { 176 #[allow(unused_imports)] 177 use wasmtime::component::__internal::{anyhow, Box}; 178 #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] 179 pub trait Host: Send { 180 async fn f1(&mut self) -> (); 181 async fn f2(&mut self, a: u32) -> (); 182 async fn f3(&mut self, a: u32, b: u32) -> (); 183 async fn f4(&mut self) -> u32; 184 async fn f5(&mut self) -> (u32, u32); 185 async fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32); 186 } 187 pub fn add_to_linker<T, D>( 188 linker: &mut wasmtime::component::Linker<T>, 189 host_getter: fn(&mut T) -> D::Data<'_>, 190 ) -> wasmtime::Result<()> 191 where 192 D: wasmtime::component::HasData, 193 for<'a> D::Data<'a>: Host + Send, 194 T: Send + 'static, 195 { 196 let mut inst = linker.instance("foo:foo/simple")?; 197 inst.func_wrap_async( 198 "f1", 199 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 200 wasmtime::component::__internal::Box::new(async move { 201 let host = &mut host_getter(caller.data_mut()); 202 let r = Host::f1(host).await; 203 Ok(r) 204 }) 205 }, 206 )?; 207 inst.func_wrap_async( 208 "f2", 209 move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (u32,)| { 210 wasmtime::component::__internal::Box::new(async move { 211 let host = &mut host_getter(caller.data_mut()); 212 let r = Host::f2(host, arg0).await; 213 Ok(r) 214 }) 215 }, 216 )?; 217 inst.func_wrap_async( 218 "f3", 219 move | 220 mut caller: wasmtime::StoreContextMut<'_, T>, 221 (arg0, arg1): (u32, u32)| 222 { 223 wasmtime::component::__internal::Box::new(async move { 224 let host = &mut host_getter(caller.data_mut()); 225 let r = Host::f3(host, arg0, arg1).await; 226 Ok(r) 227 }) 228 }, 229 )?; 230 inst.func_wrap_async( 231 "f4", 232 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 233 wasmtime::component::__internal::Box::new(async move { 234 let host = &mut host_getter(caller.data_mut()); 235 let r = Host::f4(host).await; 236 Ok((r,)) 237 }) 238 }, 239 )?; 240 inst.func_wrap_async( 241 "f5", 242 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 243 wasmtime::component::__internal::Box::new(async move { 244 let host = &mut host_getter(caller.data_mut()); 245 let r = Host::f5(host).await; 246 Ok((r,)) 247 }) 248 }, 249 )?; 250 inst.func_wrap_async( 251 "f6", 252 move | 253 mut caller: wasmtime::StoreContextMut<'_, T>, 254 (arg0, arg1, arg2): (u32, u32, u32)| 255 { 256 wasmtime::component::__internal::Box::new(async move { 257 let host = &mut host_getter(caller.data_mut()); 258 let r = Host::f6(host, arg0, arg1, arg2).await; 259 Ok((r,)) 260 }) 261 }, 262 )?; 263 Ok(()) 264 } 265 impl<_T: Host + ?Sized + Send> Host for &mut _T { 266 async fn f1(&mut self) -> () { 267 Host::f1(*self).await 268 } 269 async fn f2(&mut self, a: u32) -> () { 270 Host::f2(*self, a).await 271 } 272 async fn f3(&mut self, a: u32, b: u32) -> () { 273 Host::f3(*self, a, b).await 274 } 275 async fn f4(&mut self) -> u32 { 276 Host::f4(*self).await 277 } 278 async fn f5(&mut self) -> (u32, u32) { 279 Host::f5(*self).await 280 } 281 async fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32) { 282 Host::f6(*self, a, b, c).await 283 } 284 } 285 } 286 } 287 } 288 pub mod exports { 289 pub mod foo { 290 pub mod foo { 291 #[allow(clippy::all)] 292 pub mod simple { 293 #[allow(unused_imports)] 294 use wasmtime::component::__internal::{anyhow, Box}; 295 pub struct Guest { 296 f1: wasmtime::component::Func, 297 f2: wasmtime::component::Func, 298 f3: wasmtime::component::Func, 299 f4: wasmtime::component::Func, 300 f5: wasmtime::component::Func, 301 f6: wasmtime::component::Func, 302 } 303 #[derive(Clone)] 304 pub struct GuestIndices { 305 f1: wasmtime::component::ComponentExportIndex, 306 f2: wasmtime::component::ComponentExportIndex, 307 f3: wasmtime::component::ComponentExportIndex, 308 f4: wasmtime::component::ComponentExportIndex, 309 f5: wasmtime::component::ComponentExportIndex, 310 f6: wasmtime::component::ComponentExportIndex, 311 } 312 impl GuestIndices { 313 /// Constructor for [`GuestIndices`] which takes a 314 /// [`Component`](wasmtime::component::Component) as input and can be executed 315 /// before instantiation. 316 /// 317 /// This constructor can be used to front-load string lookups to find exports 318 /// within a component. 319 pub fn new<_T>( 320 _instance_pre: &wasmtime::component::InstancePre<_T>, 321 ) -> wasmtime::Result<GuestIndices> { 322 let instance = _instance_pre 323 .component() 324 .get_export_index(None, "foo:foo/simple") 325 .ok_or_else(|| { 326 anyhow::anyhow!( 327 "no exported instance named `foo:foo/simple`" 328 ) 329 })?; 330 let mut lookup = move |name| { 331 _instance_pre 332 .component() 333 .get_export_index(Some(&instance), name) 334 .ok_or_else(|| { 335 anyhow::anyhow!( 336 "instance export `foo:foo/simple` does \ 337 not have export `{name}`" 338 ) 339 }) 340 }; 341 let _ = &mut lookup; 342 let f1 = lookup("f1")?; 343 let f2 = lookup("f2")?; 344 let f3 = lookup("f3")?; 345 let f4 = lookup("f4")?; 346 let f5 = lookup("f5")?; 347 let f6 = lookup("f6")?; 348 Ok(GuestIndices { 349 f1, 350 f2, 351 f3, 352 f4, 353 f5, 354 f6, 355 }) 356 } 357 pub fn load( 358 &self, 359 mut store: impl wasmtime::AsContextMut, 360 instance: &wasmtime::component::Instance, 361 ) -> wasmtime::Result<Guest> { 362 let _instance = instance; 363 let _instance_pre = _instance.instance_pre(&store); 364 let _instance_type = _instance_pre.instance_type(); 365 let mut store = store.as_context_mut(); 366 let _ = &mut store; 367 let f1 = *_instance 368 .get_typed_func::<(), ()>(&mut store, &self.f1)? 369 .func(); 370 let f2 = *_instance 371 .get_typed_func::<(u32,), ()>(&mut store, &self.f2)? 372 .func(); 373 let f3 = *_instance 374 .get_typed_func::<(u32, u32), ()>(&mut store, &self.f3)? 375 .func(); 376 let f4 = *_instance 377 .get_typed_func::<(), (u32,)>(&mut store, &self.f4)? 378 .func(); 379 let f5 = *_instance 380 .get_typed_func::<(), ((u32, u32),)>(&mut store, &self.f5)? 381 .func(); 382 let f6 = *_instance 383 .get_typed_func::< 384 (u32, u32, u32), 385 ((u32, u32, u32),), 386 >(&mut store, &self.f6)? 387 .func(); 388 Ok(Guest { f1, f2, f3, f4, f5, f6 }) 389 } 390 } 391 impl Guest { 392 pub async fn call_f1<S: wasmtime::AsContextMut>( 393 &self, 394 mut store: S, 395 ) -> wasmtime::Result<()> 396 where 397 <S as wasmtime::AsContext>::Data: Send, 398 { 399 let callee = unsafe { 400 wasmtime::component::TypedFunc::< 401 (), 402 (), 403 >::new_unchecked(self.f1) 404 }; 405 let () = callee.call_async(store.as_context_mut(), ()).await?; 406 callee.post_return_async(store.as_context_mut()).await?; 407 Ok(()) 408 } 409 pub async fn call_f2<S: wasmtime::AsContextMut>( 410 &self, 411 mut store: S, 412 arg0: u32, 413 ) -> wasmtime::Result<()> 414 where 415 <S as wasmtime::AsContext>::Data: Send, 416 { 417 let callee = unsafe { 418 wasmtime::component::TypedFunc::< 419 (u32,), 420 (), 421 >::new_unchecked(self.f2) 422 }; 423 let () = callee 424 .call_async(store.as_context_mut(), (arg0,)) 425 .await?; 426 callee.post_return_async(store.as_context_mut()).await?; 427 Ok(()) 428 } 429 pub async fn call_f3<S: wasmtime::AsContextMut>( 430 &self, 431 mut store: S, 432 arg0: u32, 433 arg1: u32, 434 ) -> wasmtime::Result<()> 435 where 436 <S as wasmtime::AsContext>::Data: Send, 437 { 438 let callee = unsafe { 439 wasmtime::component::TypedFunc::< 440 (u32, u32), 441 (), 442 >::new_unchecked(self.f3) 443 }; 444 let () = callee 445 .call_async(store.as_context_mut(), (arg0, arg1)) 446 .await?; 447 callee.post_return_async(store.as_context_mut()).await?; 448 Ok(()) 449 } 450 pub async fn call_f4<S: wasmtime::AsContextMut>( 451 &self, 452 mut store: S, 453 ) -> wasmtime::Result<u32> 454 where 455 <S as wasmtime::AsContext>::Data: Send, 456 { 457 let callee = unsafe { 458 wasmtime::component::TypedFunc::< 459 (), 460 (u32,), 461 >::new_unchecked(self.f4) 462 }; 463 let (ret0,) = callee 464 .call_async(store.as_context_mut(), ()) 465 .await?; 466 callee.post_return_async(store.as_context_mut()).await?; 467 Ok(ret0) 468 } 469 pub async fn call_f5<S: wasmtime::AsContextMut>( 470 &self, 471 mut store: S, 472 ) -> wasmtime::Result<(u32, u32)> 473 where 474 <S as wasmtime::AsContext>::Data: Send, 475 { 476 let callee = unsafe { 477 wasmtime::component::TypedFunc::< 478 (), 479 ((u32, u32),), 480 >::new_unchecked(self.f5) 481 }; 482 let (ret0,) = callee 483 .call_async(store.as_context_mut(), ()) 484 .await?; 485 callee.post_return_async(store.as_context_mut()).await?; 486 Ok(ret0) 487 } 488 pub async fn call_f6<S: wasmtime::AsContextMut>( 489 &self, 490 mut store: S, 491 arg0: u32, 492 arg1: u32, 493 arg2: u32, 494 ) -> wasmtime::Result<(u32, u32, u32)> 495 where 496 <S as wasmtime::AsContext>::Data: Send, 497 { 498 let callee = unsafe { 499 wasmtime::component::TypedFunc::< 500 (u32, u32, u32), 501 ((u32, u32, u32),), 502 >::new_unchecked(self.f6) 503 }; 504 let (ret0,) = callee 505 .call_async(store.as_context_mut(), (arg0, arg1, arg2)) 506 .await?; 507 callee.post_return_async(store.as_context_mut()).await?; 508 Ok(ret0) 509 } 510 } 511 } 512 } 513 } 514 } 515