1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// component which implements the world `w`. 3 /// 4 /// This structure is created through [`WPre::new`] which 5 /// takes a [`InstancePre`](wasmtime::component::InstancePre) that 6 /// has been created through a [`Linker`](wasmtime::component::Linker). 7 pub struct WPre<T> { 8 instance_pre: wasmtime::component::InstancePre<T>, 9 interface0: exports::foo::foo::simple_export::GuestPre, 10 interface1: exports::foo::foo::export_using_import::GuestPre, 11 interface2: exports::foo::foo::export_using_export1::GuestPre, 12 interface3: exports::foo::foo::export_using_export2::GuestPre, 13 } 14 impl<T> Clone for WPre<T> { 15 fn clone(&self) -> Self { 16 Self { 17 instance_pre: self.instance_pre.clone(), 18 interface0: self.interface0.clone(), 19 interface1: self.interface1.clone(), 20 interface2: self.interface2.clone(), 21 interface3: self.interface3.clone(), 22 } 23 } 24 } 25 /// Auto-generated bindings for an instance a component which 26 /// implements the world `w`. 27 /// 28 /// This structure is created through either 29 /// [`W::instantiate_async`] or by first creating 30 /// a [`WPre`] followed by using 31 /// [`WPre::instantiate_async`]. 32 pub struct W { 33 interface0: exports::foo::foo::simple_export::Guest, 34 interface1: exports::foo::foo::export_using_import::Guest, 35 interface2: exports::foo::foo::export_using_export1::Guest, 36 interface3: exports::foo::foo::export_using_export2::Guest, 37 } 38 const _: () = { 39 #[allow(unused_imports)] 40 use wasmtime::component::__internal::anyhow; 41 impl<_T> WPre<_T> { 42 /// Creates a new copy of `WPre` bindings which can then 43 /// be used to instantiate into a particular store. 44 /// 45 /// This method may fail if the component behind `instance_pre` 46 /// does not have the required exports. 47 pub fn new( 48 instance_pre: wasmtime::component::InstancePre<_T>, 49 ) -> wasmtime::Result<Self> { 50 let _component = instance_pre.component(); 51 let interface0 = exports::foo::foo::simple_export::GuestPre::new( 52 _component, 53 )?; 54 let interface1 = exports::foo::foo::export_using_import::GuestPre::new( 55 _component, 56 )?; 57 let interface2 = exports::foo::foo::export_using_export1::GuestPre::new( 58 _component, 59 )?; 60 let interface3 = exports::foo::foo::export_using_export2::GuestPre::new( 61 _component, 62 )?; 63 Ok(WPre { 64 instance_pre, 65 interface0, 66 interface1, 67 interface2, 68 interface3, 69 }) 70 } 71 /// Instantiates a new instance of [`W`] within the 72 /// `store` provided. 73 /// 74 /// This function will use `self` as the pre-instantiated 75 /// instance to perform instantiation. Afterwards the preloaded 76 /// indices in `self` are used to lookup all exports on the 77 /// resulting instance. 78 pub async fn instantiate_async( 79 &self, 80 mut store: impl wasmtime::AsContextMut<Data = _T>, 81 ) -> wasmtime::Result<W> 82 where 83 _T: Send, 84 { 85 let mut store = store.as_context_mut(); 86 let _instance = self.instance_pre.instantiate_async(&mut store).await?; 87 let interface0 = self.interface0.load(&mut store, &_instance)?; 88 let interface1 = self.interface1.load(&mut store, &_instance)?; 89 let interface2 = self.interface2.load(&mut store, &_instance)?; 90 let interface3 = self.interface3.load(&mut store, &_instance)?; 91 Ok(W { 92 interface0, 93 interface1, 94 interface2, 95 interface3, 96 }) 97 } 98 pub fn engine(&self) -> &wasmtime::Engine { 99 self.instance_pre.engine() 100 } 101 pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 102 &self.instance_pre 103 } 104 } 105 impl W { 106 /// Convenience wrapper around [`WPre::new`] and 107 /// [`WPre::instantiate_async`]. 108 pub async fn instantiate_async<_T>( 109 mut store: impl wasmtime::AsContextMut<Data = _T>, 110 component: &wasmtime::component::Component, 111 linker: &wasmtime::component::Linker<_T>, 112 ) -> wasmtime::Result<W> 113 where 114 _T: Send, 115 { 116 let pre = linker.instantiate_pre(component)?; 117 WPre::new(pre)?.instantiate_async(store).await 118 } 119 pub fn add_to_linker<T, U>( 120 linker: &mut wasmtime::component::Linker<T>, 121 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 122 ) -> wasmtime::Result<()> 123 where 124 T: Send, 125 U: foo::foo::transitive_import::Host + Send, 126 { 127 foo::foo::transitive_import::add_to_linker(linker, get)?; 128 Ok(()) 129 } 130 pub fn foo_foo_simple_export(&self) -> &exports::foo::foo::simple_export::Guest { 131 &self.interface0 132 } 133 pub fn foo_foo_export_using_import( 134 &self, 135 ) -> &exports::foo::foo::export_using_import::Guest { 136 &self.interface1 137 } 138 pub fn foo_foo_export_using_export1( 139 &self, 140 ) -> &exports::foo::foo::export_using_export1::Guest { 141 &self.interface2 142 } 143 pub fn foo_foo_export_using_export2( 144 &self, 145 ) -> &exports::foo::foo::export_using_export2::Guest { 146 &self.interface3 147 } 148 } 149 }; 150 pub mod foo { 151 pub mod foo { 152 #[allow(clippy::all)] 153 pub mod transitive_import { 154 #[allow(unused_imports)] 155 use wasmtime::component::__internal::anyhow; 156 pub enum Y {} 157 #[wasmtime::component::__internal::async_trait] 158 pub trait HostY { 159 async fn drop( 160 &mut self, 161 rep: wasmtime::component::Resource<Y>, 162 ) -> wasmtime::Result<()>; 163 } 164 #[wasmtime::component::__internal::async_trait] 165 impl<_T: HostY + ?Sized + Send> HostY for &mut _T { 166 async fn drop( 167 &mut self, 168 rep: wasmtime::component::Resource<Y>, 169 ) -> wasmtime::Result<()> { 170 HostY::drop(*self, rep).await 171 } 172 } 173 #[wasmtime::component::__internal::async_trait] 174 pub trait Host: Send + HostY {} 175 pub trait GetHost< 176 T, 177 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 178 type Host: Host + Send; 179 } 180 impl<F, T, O> GetHost<T> for F 181 where 182 F: Fn(T) -> O + Send + Sync + Copy + 'static, 183 O: Host + Send, 184 { 185 type Host = O; 186 } 187 pub fn add_to_linker_get_host<T>( 188 linker: &mut wasmtime::component::Linker<T>, 189 host_getter: impl for<'a> GetHost<&'a mut T>, 190 ) -> wasmtime::Result<()> 191 where 192 T: Send, 193 { 194 let mut inst = linker.instance("foo:foo/transitive-import")?; 195 inst.resource_async( 196 "y", 197 wasmtime::component::ResourceType::host::<Y>(), 198 move |mut store, rep| { 199 std::boxed::Box::new(async move { 200 HostY::drop( 201 &mut host_getter(store.data_mut()), 202 wasmtime::component::Resource::new_own(rep), 203 ) 204 .await 205 }) 206 }, 207 )?; 208 Ok(()) 209 } 210 pub fn add_to_linker<T, U>( 211 linker: &mut wasmtime::component::Linker<T>, 212 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 213 ) -> wasmtime::Result<()> 214 where 215 U: Host + Send, 216 T: Send, 217 { 218 add_to_linker_get_host(linker, get) 219 } 220 #[wasmtime::component::__internal::async_trait] 221 impl<_T: Host + ?Sized + Send> Host for &mut _T {} 222 } 223 } 224 } 225 pub mod exports { 226 pub mod foo { 227 pub mod foo { 228 #[allow(clippy::all)] 229 pub mod simple_export { 230 #[allow(unused_imports)] 231 use wasmtime::component::__internal::anyhow; 232 pub type A = wasmtime::component::ResourceAny; 233 pub struct GuestA<'a> { 234 funcs: &'a Guest, 235 } 236 pub struct Guest { 237 constructor_a_constructor: wasmtime::component::Func, 238 static_a_static_a: wasmtime::component::Func, 239 method_a_method_a: wasmtime::component::Func, 240 } 241 #[derive(Clone)] 242 pub struct GuestPre { 243 constructor_a_constructor: wasmtime::component::ComponentExportIndex, 244 static_a_static_a: wasmtime::component::ComponentExportIndex, 245 method_a_method_a: wasmtime::component::ComponentExportIndex, 246 } 247 impl GuestPre { 248 pub fn new( 249 component: &wasmtime::component::Component, 250 ) -> wasmtime::Result<GuestPre> { 251 let _component = component; 252 let (_, instance) = component 253 .export_index(None, "foo:foo/simple-export") 254 .ok_or_else(|| { 255 anyhow::anyhow!( 256 "no exported instance named `foo:foo/simple-export`" 257 ) 258 })?; 259 let _lookup = |name: &str| { 260 _component 261 .export_index(Some(&instance), name) 262 .map(|p| p.1) 263 .ok_or_else(|| { 264 anyhow::anyhow!( 265 "instance export `foo:foo/simple-export` does \ 266 not have export `{name}`" 267 ) 268 }) 269 }; 270 let constructor_a_constructor = _lookup("[constructor]a")?; 271 let static_a_static_a = _lookup("[static]a.static-a")?; 272 let method_a_method_a = _lookup("[method]a.method-a")?; 273 Ok(GuestPre { 274 constructor_a_constructor, 275 static_a_static_a, 276 method_a_method_a, 277 }) 278 } 279 pub fn load( 280 &self, 281 mut store: impl wasmtime::AsContextMut, 282 instance: &wasmtime::component::Instance, 283 ) -> wasmtime::Result<Guest> { 284 let mut store = store.as_context_mut(); 285 let _ = &mut store; 286 let _instance = instance; 287 let constructor_a_constructor = *_instance 288 .get_typed_func::< 289 (), 290 (wasmtime::component::ResourceAny,), 291 >(&mut store, &self.constructor_a_constructor)? 292 .func(); 293 let static_a_static_a = *_instance 294 .get_typed_func::< 295 (), 296 (u32,), 297 >(&mut store, &self.static_a_static_a)? 298 .func(); 299 let method_a_method_a = *_instance 300 .get_typed_func::< 301 (wasmtime::component::ResourceAny,), 302 (u32,), 303 >(&mut store, &self.method_a_method_a)? 304 .func(); 305 Ok(Guest { 306 constructor_a_constructor, 307 static_a_static_a, 308 method_a_method_a, 309 }) 310 } 311 } 312 impl Guest { 313 pub fn a(&self) -> GuestA<'_> { 314 GuestA { funcs: self } 315 } 316 } 317 impl GuestA<'_> { 318 pub async fn call_constructor<S: wasmtime::AsContextMut>( 319 &self, 320 mut store: S, 321 ) -> wasmtime::Result<wasmtime::component::ResourceAny> 322 where 323 <S as wasmtime::AsContext>::Data: Send, 324 { 325 let callee = unsafe { 326 wasmtime::component::TypedFunc::< 327 (), 328 (wasmtime::component::ResourceAny,), 329 >::new_unchecked(self.funcs.constructor_a_constructor) 330 }; 331 let (ret0,) = callee 332 .call_async(store.as_context_mut(), ()) 333 .await?; 334 callee.post_return_async(store.as_context_mut()).await?; 335 Ok(ret0) 336 } 337 pub async fn call_static_a<S: wasmtime::AsContextMut>( 338 &self, 339 mut store: S, 340 ) -> wasmtime::Result<u32> 341 where 342 <S as wasmtime::AsContext>::Data: Send, 343 { 344 let callee = unsafe { 345 wasmtime::component::TypedFunc::< 346 (), 347 (u32,), 348 >::new_unchecked(self.funcs.static_a_static_a) 349 }; 350 let (ret0,) = callee 351 .call_async(store.as_context_mut(), ()) 352 .await?; 353 callee.post_return_async(store.as_context_mut()).await?; 354 Ok(ret0) 355 } 356 pub async fn call_method_a<S: wasmtime::AsContextMut>( 357 &self, 358 mut store: S, 359 arg0: wasmtime::component::ResourceAny, 360 ) -> wasmtime::Result<u32> 361 where 362 <S as wasmtime::AsContext>::Data: Send, 363 { 364 let callee = unsafe { 365 wasmtime::component::TypedFunc::< 366 (wasmtime::component::ResourceAny,), 367 (u32,), 368 >::new_unchecked(self.funcs.method_a_method_a) 369 }; 370 let (ret0,) = callee 371 .call_async(store.as_context_mut(), (arg0,)) 372 .await?; 373 callee.post_return_async(store.as_context_mut()).await?; 374 Ok(ret0) 375 } 376 } 377 } 378 #[allow(clippy::all)] 379 pub mod export_using_import { 380 #[allow(unused_imports)] 381 use wasmtime::component::__internal::anyhow; 382 pub type Y = super::super::super::super::foo::foo::transitive_import::Y; 383 pub type A = wasmtime::component::ResourceAny; 384 pub struct GuestA<'a> { 385 funcs: &'a Guest, 386 } 387 pub struct Guest { 388 constructor_a_constructor: wasmtime::component::Func, 389 static_a_static_a: wasmtime::component::Func, 390 method_a_method_a: wasmtime::component::Func, 391 } 392 #[derive(Clone)] 393 pub struct GuestPre { 394 constructor_a_constructor: wasmtime::component::ComponentExportIndex, 395 static_a_static_a: wasmtime::component::ComponentExportIndex, 396 method_a_method_a: wasmtime::component::ComponentExportIndex, 397 } 398 impl GuestPre { 399 pub fn new( 400 component: &wasmtime::component::Component, 401 ) -> wasmtime::Result<GuestPre> { 402 let _component = component; 403 let (_, instance) = component 404 .export_index(None, "foo:foo/export-using-import") 405 .ok_or_else(|| { 406 anyhow::anyhow!( 407 "no exported instance named `foo:foo/export-using-import`" 408 ) 409 })?; 410 let _lookup = |name: &str| { 411 _component 412 .export_index(Some(&instance), name) 413 .map(|p| p.1) 414 .ok_or_else(|| { 415 anyhow::anyhow!( 416 "instance export `foo:foo/export-using-import` does \ 417 not have export `{name}`" 418 ) 419 }) 420 }; 421 let constructor_a_constructor = _lookup("[constructor]a")?; 422 let static_a_static_a = _lookup("[static]a.static-a")?; 423 let method_a_method_a = _lookup("[method]a.method-a")?; 424 Ok(GuestPre { 425 constructor_a_constructor, 426 static_a_static_a, 427 method_a_method_a, 428 }) 429 } 430 pub fn load( 431 &self, 432 mut store: impl wasmtime::AsContextMut, 433 instance: &wasmtime::component::Instance, 434 ) -> wasmtime::Result<Guest> { 435 let mut store = store.as_context_mut(); 436 let _ = &mut store; 437 let _instance = instance; 438 let constructor_a_constructor = *_instance 439 .get_typed_func::< 440 (wasmtime::component::Resource<Y>,), 441 (wasmtime::component::ResourceAny,), 442 >(&mut store, &self.constructor_a_constructor)? 443 .func(); 444 let static_a_static_a = *_instance 445 .get_typed_func::< 446 (), 447 (wasmtime::component::Resource<Y>,), 448 >(&mut store, &self.static_a_static_a)? 449 .func(); 450 let method_a_method_a = *_instance 451 .get_typed_func::< 452 ( 453 wasmtime::component::ResourceAny, 454 wasmtime::component::Resource<Y>, 455 ), 456 (wasmtime::component::Resource<Y>,), 457 >(&mut store, &self.method_a_method_a)? 458 .func(); 459 Ok(Guest { 460 constructor_a_constructor, 461 static_a_static_a, 462 method_a_method_a, 463 }) 464 } 465 } 466 impl Guest { 467 pub fn a(&self) -> GuestA<'_> { 468 GuestA { funcs: self } 469 } 470 } 471 impl GuestA<'_> { 472 pub async fn call_constructor<S: wasmtime::AsContextMut>( 473 &self, 474 mut store: S, 475 arg0: wasmtime::component::Resource<Y>, 476 ) -> wasmtime::Result<wasmtime::component::ResourceAny> 477 where 478 <S as wasmtime::AsContext>::Data: Send, 479 { 480 let callee = unsafe { 481 wasmtime::component::TypedFunc::< 482 (wasmtime::component::Resource<Y>,), 483 (wasmtime::component::ResourceAny,), 484 >::new_unchecked(self.funcs.constructor_a_constructor) 485 }; 486 let (ret0,) = callee 487 .call_async(store.as_context_mut(), (arg0,)) 488 .await?; 489 callee.post_return_async(store.as_context_mut()).await?; 490 Ok(ret0) 491 } 492 pub async fn call_static_a<S: wasmtime::AsContextMut>( 493 &self, 494 mut store: S, 495 ) -> wasmtime::Result<wasmtime::component::Resource<Y>> 496 where 497 <S as wasmtime::AsContext>::Data: Send, 498 { 499 let callee = unsafe { 500 wasmtime::component::TypedFunc::< 501 (), 502 (wasmtime::component::Resource<Y>,), 503 >::new_unchecked(self.funcs.static_a_static_a) 504 }; 505 let (ret0,) = callee 506 .call_async(store.as_context_mut(), ()) 507 .await?; 508 callee.post_return_async(store.as_context_mut()).await?; 509 Ok(ret0) 510 } 511 pub async fn call_method_a<S: wasmtime::AsContextMut>( 512 &self, 513 mut store: S, 514 arg0: wasmtime::component::ResourceAny, 515 arg1: wasmtime::component::Resource<Y>, 516 ) -> wasmtime::Result<wasmtime::component::Resource<Y>> 517 where 518 <S as wasmtime::AsContext>::Data: Send, 519 { 520 let callee = unsafe { 521 wasmtime::component::TypedFunc::< 522 ( 523 wasmtime::component::ResourceAny, 524 wasmtime::component::Resource<Y>, 525 ), 526 (wasmtime::component::Resource<Y>,), 527 >::new_unchecked(self.funcs.method_a_method_a) 528 }; 529 let (ret0,) = callee 530 .call_async(store.as_context_mut(), (arg0, arg1)) 531 .await?; 532 callee.post_return_async(store.as_context_mut()).await?; 533 Ok(ret0) 534 } 535 } 536 } 537 #[allow(clippy::all)] 538 pub mod export_using_export1 { 539 #[allow(unused_imports)] 540 use wasmtime::component::__internal::anyhow; 541 pub type A = wasmtime::component::ResourceAny; 542 pub struct GuestA<'a> { 543 funcs: &'a Guest, 544 } 545 pub struct Guest { 546 constructor_a_constructor: wasmtime::component::Func, 547 } 548 #[derive(Clone)] 549 pub struct GuestPre { 550 constructor_a_constructor: wasmtime::component::ComponentExportIndex, 551 } 552 impl GuestPre { 553 pub fn new( 554 component: &wasmtime::component::Component, 555 ) -> wasmtime::Result<GuestPre> { 556 let _component = component; 557 let (_, instance) = component 558 .export_index(None, "foo:foo/export-using-export1") 559 .ok_or_else(|| { 560 anyhow::anyhow!( 561 "no exported instance named `foo:foo/export-using-export1`" 562 ) 563 })?; 564 let _lookup = |name: &str| { 565 _component 566 .export_index(Some(&instance), name) 567 .map(|p| p.1) 568 .ok_or_else(|| { 569 anyhow::anyhow!( 570 "instance export `foo:foo/export-using-export1` does \ 571 not have export `{name}`" 572 ) 573 }) 574 }; 575 let constructor_a_constructor = _lookup("[constructor]a")?; 576 Ok(GuestPre { 577 constructor_a_constructor, 578 }) 579 } 580 pub fn load( 581 &self, 582 mut store: impl wasmtime::AsContextMut, 583 instance: &wasmtime::component::Instance, 584 ) -> wasmtime::Result<Guest> { 585 let mut store = store.as_context_mut(); 586 let _ = &mut store; 587 let _instance = instance; 588 let constructor_a_constructor = *_instance 589 .get_typed_func::< 590 (), 591 (wasmtime::component::ResourceAny,), 592 >(&mut store, &self.constructor_a_constructor)? 593 .func(); 594 Ok(Guest { constructor_a_constructor }) 595 } 596 } 597 impl Guest { 598 pub fn a(&self) -> GuestA<'_> { 599 GuestA { funcs: self } 600 } 601 } 602 impl GuestA<'_> { 603 pub async fn call_constructor<S: wasmtime::AsContextMut>( 604 &self, 605 mut store: S, 606 ) -> wasmtime::Result<wasmtime::component::ResourceAny> 607 where 608 <S as wasmtime::AsContext>::Data: Send, 609 { 610 let callee = unsafe { 611 wasmtime::component::TypedFunc::< 612 (), 613 (wasmtime::component::ResourceAny,), 614 >::new_unchecked(self.funcs.constructor_a_constructor) 615 }; 616 let (ret0,) = callee 617 .call_async(store.as_context_mut(), ()) 618 .await?; 619 callee.post_return_async(store.as_context_mut()).await?; 620 Ok(ret0) 621 } 622 } 623 } 624 #[allow(clippy::all)] 625 pub mod export_using_export2 { 626 #[allow(unused_imports)] 627 use wasmtime::component::__internal::anyhow; 628 pub type A = super::super::super::super::exports::foo::foo::export_using_export1::A; 629 pub type B = wasmtime::component::ResourceAny; 630 pub struct GuestB<'a> { 631 funcs: &'a Guest, 632 } 633 pub struct Guest { 634 constructor_b_constructor: wasmtime::component::Func, 635 } 636 #[derive(Clone)] 637 pub struct GuestPre { 638 constructor_b_constructor: wasmtime::component::ComponentExportIndex, 639 } 640 impl GuestPre { 641 pub fn new( 642 component: &wasmtime::component::Component, 643 ) -> wasmtime::Result<GuestPre> { 644 let _component = component; 645 let (_, instance) = component 646 .export_index(None, "foo:foo/export-using-export2") 647 .ok_or_else(|| { 648 anyhow::anyhow!( 649 "no exported instance named `foo:foo/export-using-export2`" 650 ) 651 })?; 652 let _lookup = |name: &str| { 653 _component 654 .export_index(Some(&instance), name) 655 .map(|p| p.1) 656 .ok_or_else(|| { 657 anyhow::anyhow!( 658 "instance export `foo:foo/export-using-export2` does \ 659 not have export `{name}`" 660 ) 661 }) 662 }; 663 let constructor_b_constructor = _lookup("[constructor]b")?; 664 Ok(GuestPre { 665 constructor_b_constructor, 666 }) 667 } 668 pub fn load( 669 &self, 670 mut store: impl wasmtime::AsContextMut, 671 instance: &wasmtime::component::Instance, 672 ) -> wasmtime::Result<Guest> { 673 let mut store = store.as_context_mut(); 674 let _ = &mut store; 675 let _instance = instance; 676 let constructor_b_constructor = *_instance 677 .get_typed_func::< 678 (wasmtime::component::ResourceAny,), 679 (wasmtime::component::ResourceAny,), 680 >(&mut store, &self.constructor_b_constructor)? 681 .func(); 682 Ok(Guest { constructor_b_constructor }) 683 } 684 } 685 impl Guest { 686 pub fn b(&self) -> GuestB<'_> { 687 GuestB { funcs: self } 688 } 689 } 690 impl GuestB<'_> { 691 pub async fn call_constructor<S: wasmtime::AsContextMut>( 692 &self, 693 mut store: S, 694 arg0: wasmtime::component::ResourceAny, 695 ) -> wasmtime::Result<wasmtime::component::ResourceAny> 696 where 697 <S as wasmtime::AsContext>::Data: Send, 698 { 699 let callee = unsafe { 700 wasmtime::component::TypedFunc::< 701 (wasmtime::component::ResourceAny,), 702 (wasmtime::component::ResourceAny,), 703 >::new_unchecked(self.funcs.constructor_b_constructor) 704 }; 705 let (ret0,) = callee 706 .call_async(store.as_context_mut(), (arg0,)) 707 .await?; 708 callee.post_return_async(store.as_context_mut()).await?; 709 Ok(ret0) 710 } 711 } 712 } 713 } 714 } 715 } 716