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 /// 8 /// For more information see [`W`] as well. 9 pub struct WPre<T: 'static> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: WIndices, 12 } 13 impl<T: 'static> Clone for WPre<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> WPre<_T> { 22 /// Creates a new copy of `WPre` 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 = WIndices::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 [`W`] 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 fn instantiate( 47 &self, 48 mut store: impl wasmtime::AsContextMut<Data = _T>, 49 ) -> wasmtime::Result<W> { 50 let mut store = store.as_context_mut(); 51 let instance = self.instance_pre.instantiate(&mut store)?; 52 self.indices.load(&mut store, &instance) 53 } 54 } 55 impl<_T: Send + 'static> WPre<_T> { 56 /// Same as [`Self::instantiate`], except with `async`. 57 pub async fn instantiate_async( 58 &self, 59 mut store: impl wasmtime::AsContextMut<Data = _T>, 60 ) -> wasmtime::Result<W> { 61 let mut store = store.as_context_mut(); 62 let instance = self.instance_pre.instantiate_async(&mut store).await?; 63 self.indices.load(&mut store, &instance) 64 } 65 } 66 /// Auto-generated bindings for index of the exports of 67 /// `w`. 68 /// 69 /// This is an implementation detail of [`WPre`] and can 70 /// be constructed if needed as well. 71 /// 72 /// For more information see [`W`] as well. 73 #[derive(Clone)] 74 pub struct WIndices { 75 interface0: exports::foo::foo::simple_export::GuestIndices, 76 interface1: exports::foo::foo::export_using_import::GuestIndices, 77 interface2: exports::foo::foo::export_using_export1::GuestIndices, 78 interface3: exports::foo::foo::export_using_export2::GuestIndices, 79 } 80 /// Auto-generated bindings for an instance a component which 81 /// implements the world `w`. 82 /// 83 /// This structure can be created through a number of means 84 /// depending on your requirements and what you have on hand: 85 /// 86 /// * The most convenient way is to use 87 /// [`W::instantiate`] which only needs a 88 /// [`Store`], [`Component`], and [`Linker`]. 89 /// 90 /// * Alternatively you can create a [`WPre`] ahead of 91 /// time with a [`Component`] to front-load string lookups 92 /// of exports once instead of per-instantiation. This 93 /// method then uses [`WPre::instantiate`] to 94 /// create a [`W`]. 95 /// 96 /// * If you've instantiated the instance yourself already 97 /// then you can use [`W::new`]. 98 /// 99 /// These methods are all equivalent to one another and move 100 /// around the tradeoff of what work is performed when. 101 /// 102 /// [`Store`]: wasmtime::Store 103 /// [`Component`]: wasmtime::component::Component 104 /// [`Linker`]: wasmtime::component::Linker 105 pub struct W { 106 interface0: exports::foo::foo::simple_export::Guest, 107 interface1: exports::foo::foo::export_using_import::Guest, 108 interface2: exports::foo::foo::export_using_export1::Guest, 109 interface3: exports::foo::foo::export_using_export2::Guest, 110 } 111 const _: () = { 112 impl WIndices { 113 /// Creates a new copy of `WIndices` bindings which can then 114 /// be used to instantiate into a particular store. 115 /// 116 /// This method may fail if the component does not have the 117 /// required exports. 118 pub fn new<_T>( 119 _instance_pre: &wasmtime::component::InstancePre<_T>, 120 ) -> wasmtime::Result<Self> { 121 let _component = _instance_pre.component(); 122 let _instance_type = _instance_pre.instance_type(); 123 let interface0 = exports::foo::foo::simple_export::GuestIndices::new( 124 _instance_pre, 125 )?; 126 let interface1 = exports::foo::foo::export_using_import::GuestIndices::new( 127 _instance_pre, 128 )?; 129 let interface2 = exports::foo::foo::export_using_export1::GuestIndices::new( 130 _instance_pre, 131 )?; 132 let interface3 = exports::foo::foo::export_using_export2::GuestIndices::new( 133 _instance_pre, 134 )?; 135 Ok(WIndices { 136 interface0, 137 interface1, 138 interface2, 139 interface3, 140 }) 141 } 142 /// Uses the indices stored in `self` to load an instance 143 /// of [`W`] from the instance provided. 144 /// 145 /// Note that at this time this method will additionally 146 /// perform type-checks of all exports. 147 pub fn load( 148 &self, 149 mut store: impl wasmtime::AsContextMut, 150 instance: &wasmtime::component::Instance, 151 ) -> wasmtime::Result<W> { 152 let _ = &mut store; 153 let _instance = instance; 154 let interface0 = self.interface0.load(&mut store, &_instance)?; 155 let interface1 = self.interface1.load(&mut store, &_instance)?; 156 let interface2 = self.interface2.load(&mut store, &_instance)?; 157 let interface3 = self.interface3.load(&mut store, &_instance)?; 158 Ok(W { 159 interface0, 160 interface1, 161 interface2, 162 interface3, 163 }) 164 } 165 } 166 impl W { 167 /// Convenience wrapper around [`WPre::new`] and 168 /// [`WPre::instantiate`]. 169 pub fn instantiate<_T>( 170 store: impl wasmtime::AsContextMut<Data = _T>, 171 component: &wasmtime::component::Component, 172 linker: &wasmtime::component::Linker<_T>, 173 ) -> wasmtime::Result<W> { 174 let pre = linker.instantiate_pre(component)?; 175 WPre::new(pre)?.instantiate(store) 176 } 177 /// Convenience wrapper around [`WIndices::new`] and 178 /// [`WIndices::load`]. 179 pub fn new( 180 mut store: impl wasmtime::AsContextMut, 181 instance: &wasmtime::component::Instance, 182 ) -> wasmtime::Result<W> { 183 let indices = WIndices::new(&instance.instance_pre(&store))?; 184 indices.load(&mut store, instance) 185 } 186 /// Convenience wrapper around [`WPre::new`] and 187 /// [`WPre::instantiate_async`]. 188 pub async fn instantiate_async<_T>( 189 store: impl wasmtime::AsContextMut<Data = _T>, 190 component: &wasmtime::component::Component, 191 linker: &wasmtime::component::Linker<_T>, 192 ) -> wasmtime::Result<W> 193 where 194 _T: Send, 195 { 196 let pre = linker.instantiate_pre(component)?; 197 WPre::new(pre)?.instantiate_async(store).await 198 } 199 pub fn add_to_linker<T, D>( 200 linker: &mut wasmtime::component::Linker<T>, 201 host_getter: fn(&mut T) -> D::Data<'_>, 202 ) -> wasmtime::Result<()> 203 where 204 D: foo::foo::transitive_import::HostWithStore + Send, 205 for<'a> D::Data<'a>: foo::foo::transitive_import::Host + Send, 206 T: 'static + Send, 207 { 208 foo::foo::transitive_import::add_to_linker::<T, D>(linker, host_getter)?; 209 Ok(()) 210 } 211 pub fn foo_foo_simple_export(&self) -> &exports::foo::foo::simple_export::Guest { 212 &self.interface0 213 } 214 pub fn foo_foo_export_using_import( 215 &self, 216 ) -> &exports::foo::foo::export_using_import::Guest { 217 &self.interface1 218 } 219 pub fn foo_foo_export_using_export1( 220 &self, 221 ) -> &exports::foo::foo::export_using_export1::Guest { 222 &self.interface2 223 } 224 pub fn foo_foo_export_using_export2( 225 &self, 226 ) -> &exports::foo::foo::export_using_export2::Guest { 227 &self.interface3 228 } 229 } 230 }; 231 pub mod foo { 232 pub mod foo { 233 #[allow(clippy::all)] 234 pub mod transitive_import { 235 #[allow(unused_imports)] 236 use wasmtime::component::__internal::Box; 237 pub enum Y {} 238 pub trait HostYWithStore: wasmtime::component::HasData { 239 fn drop<T>( 240 accessor: &wasmtime::component::Accessor<T, Self>, 241 rep: wasmtime::component::Resource<Y>, 242 ) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send 243 where 244 Self: Sized; 245 } 246 pub trait HostY {} 247 impl<_T: HostY + ?Sized + Send> HostY for &mut _T {} 248 pub trait HostWithStore: wasmtime::component::HasData + HostYWithStore + Send {} 249 impl<_T: ?Sized> HostWithStore for _T 250 where 251 _T: wasmtime::component::HasData + HostYWithStore + Send, 252 {} 253 pub trait Host: HostY + Send {} 254 impl<_T: Host + ?Sized + Send> Host for &mut _T {} 255 pub fn add_to_linker<T, D>( 256 linker: &mut wasmtime::component::Linker<T>, 257 host_getter: fn(&mut T) -> D::Data<'_>, 258 ) -> wasmtime::Result<()> 259 where 260 D: HostWithStore, 261 for<'a> D::Data<'a>: Host, 262 T: 'static + Send, 263 { 264 let mut inst = linker.instance("foo:foo/transitive-import")?; 265 inst.resource_concurrent( 266 "y", 267 wasmtime::component::ResourceType::host::<Y>(), 268 move |caller: &wasmtime::component::Accessor<T>, rep| { 269 wasmtime::component::__internal::Box::pin(async move { 270 let accessor = &caller.with_getter(host_getter); 271 HostYWithStore::drop( 272 accessor, 273 wasmtime::component::Resource::new_own(rep), 274 ) 275 .await 276 }) 277 }, 278 )?; 279 Ok(()) 280 } 281 } 282 } 283 } 284 pub mod exports { 285 pub mod foo { 286 pub mod foo { 287 #[allow(clippy::all)] 288 pub mod simple_export { 289 #[allow(unused_imports)] 290 use wasmtime::component::__internal::Box; 291 pub type A = wasmtime::component::ResourceAny; 292 pub struct GuestA<'a> { 293 funcs: &'a Guest, 294 } 295 #[derive(Clone)] 296 pub struct Guest { 297 constructor_a_constructor: wasmtime::component::Func, 298 static_a_static_a: wasmtime::component::Func, 299 method_a_method_a: wasmtime::component::Func, 300 } 301 #[derive(Clone)] 302 pub struct GuestIndices { 303 constructor_a_constructor: wasmtime::component::ComponentExportIndex, 304 static_a_static_a: wasmtime::component::ComponentExportIndex, 305 method_a_method_a: wasmtime::component::ComponentExportIndex, 306 } 307 impl GuestIndices { 308 /// Constructor for [`GuestIndices`] which takes a 309 /// [`Component`](wasmtime::component::Component) as input and can be executed 310 /// before instantiation. 311 /// 312 /// This constructor can be used to front-load string lookups to find exports 313 /// within a component. 314 pub fn new<_T>( 315 _instance_pre: &wasmtime::component::InstancePre<_T>, 316 ) -> wasmtime::Result<GuestIndices> { 317 let instance = _instance_pre 318 .component() 319 .get_export_index(None, "foo:foo/simple-export") 320 .ok_or_else(|| { 321 wasmtime::format_err!( 322 "no exported instance named `foo:foo/simple-export`" 323 ) 324 })?; 325 let mut lookup = move |name| { 326 _instance_pre 327 .component() 328 .get_export_index(Some(&instance), name) 329 .ok_or_else(|| { 330 wasmtime::format_err!( 331 "instance export `foo:foo/simple-export` does \ 332 not have export `{name}`" 333 ) 334 }) 335 }; 336 let _ = &mut lookup; 337 let constructor_a_constructor = lookup("[constructor]a")?; 338 let static_a_static_a = lookup("[static]a.static-a")?; 339 let method_a_method_a = lookup("[method]a.method-a")?; 340 Ok(GuestIndices { 341 constructor_a_constructor, 342 static_a_static_a, 343 method_a_method_a, 344 }) 345 } 346 pub fn load( 347 &self, 348 mut store: impl wasmtime::AsContextMut, 349 instance: &wasmtime::component::Instance, 350 ) -> wasmtime::Result<Guest> { 351 let _instance = instance; 352 let _instance_pre = _instance.instance_pre(&store); 353 let _instance_type = _instance_pre.instance_type(); 354 let mut store = store.as_context_mut(); 355 let _ = &mut store; 356 let constructor_a_constructor = *_instance 357 .get_typed_func::< 358 (), 359 (wasmtime::component::ResourceAny,), 360 >(&mut store, &self.constructor_a_constructor)? 361 .func(); 362 let static_a_static_a = *_instance 363 .get_typed_func::< 364 (), 365 (u32,), 366 >(&mut store, &self.static_a_static_a)? 367 .func(); 368 let method_a_method_a = *_instance 369 .get_typed_func::< 370 (wasmtime::component::ResourceAny,), 371 (u32,), 372 >(&mut store, &self.method_a_method_a)? 373 .func(); 374 Ok(Guest { 375 constructor_a_constructor, 376 static_a_static_a, 377 method_a_method_a, 378 }) 379 } 380 } 381 impl Guest { 382 pub fn a(&self) -> GuestA<'_> { 383 GuestA { funcs: self } 384 } 385 } 386 impl GuestA<'_> { 387 pub async fn call_constructor<_T, _D>( 388 &self, 389 accessor: &wasmtime::component::Accessor<_T, _D>, 390 ) -> wasmtime::Result<wasmtime::component::ResourceAny> 391 where 392 _T: Send, 393 _D: wasmtime::component::HasData, 394 { 395 let callee = unsafe { 396 wasmtime::component::TypedFunc::< 397 (), 398 (wasmtime::component::ResourceAny,), 399 >::new_unchecked(self.funcs.constructor_a_constructor) 400 }; 401 let ((ret0,), _) = callee.call_concurrent(accessor, ()).await?; 402 Ok(ret0) 403 } 404 pub async fn call_static_a<_T, _D>( 405 &self, 406 accessor: &wasmtime::component::Accessor<_T, _D>, 407 ) -> wasmtime::Result<u32> 408 where 409 _T: Send, 410 _D: wasmtime::component::HasData, 411 { 412 let callee = unsafe { 413 wasmtime::component::TypedFunc::< 414 (), 415 (u32,), 416 >::new_unchecked(self.funcs.static_a_static_a) 417 }; 418 let ((ret0,), _) = callee.call_concurrent(accessor, ()).await?; 419 Ok(ret0) 420 } 421 pub async fn call_method_a<_T, _D>( 422 &self, 423 accessor: &wasmtime::component::Accessor<_T, _D>, 424 arg0: wasmtime::component::ResourceAny, 425 ) -> wasmtime::Result<u32> 426 where 427 _T: Send, 428 _D: wasmtime::component::HasData, 429 { 430 let callee = unsafe { 431 wasmtime::component::TypedFunc::< 432 (wasmtime::component::ResourceAny,), 433 (u32,), 434 >::new_unchecked(self.funcs.method_a_method_a) 435 }; 436 let ((ret0,), _) = callee 437 .call_concurrent(accessor, (arg0,)) 438 .await?; 439 Ok(ret0) 440 } 441 } 442 } 443 #[allow(clippy::all)] 444 pub mod export_using_import { 445 #[allow(unused_imports)] 446 use wasmtime::component::__internal::Box; 447 pub type Y = super::super::super::super::foo::foo::transitive_import::Y; 448 pub type A = wasmtime::component::ResourceAny; 449 pub struct GuestA<'a> { 450 funcs: &'a Guest, 451 } 452 #[derive(Clone)] 453 pub struct Guest { 454 constructor_a_constructor: wasmtime::component::Func, 455 static_a_static_a: wasmtime::component::Func, 456 method_a_method_a: wasmtime::component::Func, 457 } 458 #[derive(Clone)] 459 pub struct GuestIndices { 460 constructor_a_constructor: wasmtime::component::ComponentExportIndex, 461 static_a_static_a: wasmtime::component::ComponentExportIndex, 462 method_a_method_a: wasmtime::component::ComponentExportIndex, 463 } 464 impl GuestIndices { 465 /// Constructor for [`GuestIndices`] which takes a 466 /// [`Component`](wasmtime::component::Component) as input and can be executed 467 /// before instantiation. 468 /// 469 /// This constructor can be used to front-load string lookups to find exports 470 /// within a component. 471 pub fn new<_T>( 472 _instance_pre: &wasmtime::component::InstancePre<_T>, 473 ) -> wasmtime::Result<GuestIndices> { 474 let instance = _instance_pre 475 .component() 476 .get_export_index(None, "foo:foo/export-using-import") 477 .ok_or_else(|| { 478 wasmtime::format_err!( 479 "no exported instance named `foo:foo/export-using-import`" 480 ) 481 })?; 482 let mut lookup = move |name| { 483 _instance_pre 484 .component() 485 .get_export_index(Some(&instance), name) 486 .ok_or_else(|| { 487 wasmtime::format_err!( 488 "instance export `foo:foo/export-using-import` does \ 489 not have export `{name}`" 490 ) 491 }) 492 }; 493 let _ = &mut lookup; 494 let constructor_a_constructor = lookup("[constructor]a")?; 495 let static_a_static_a = lookup("[static]a.static-a")?; 496 let method_a_method_a = lookup("[method]a.method-a")?; 497 Ok(GuestIndices { 498 constructor_a_constructor, 499 static_a_static_a, 500 method_a_method_a, 501 }) 502 } 503 pub fn load( 504 &self, 505 mut store: impl wasmtime::AsContextMut, 506 instance: &wasmtime::component::Instance, 507 ) -> wasmtime::Result<Guest> { 508 let _instance = instance; 509 let _instance_pre = _instance.instance_pre(&store); 510 let _instance_type = _instance_pre.instance_type(); 511 let mut store = store.as_context_mut(); 512 let _ = &mut store; 513 let constructor_a_constructor = *_instance 514 .get_typed_func::< 515 (wasmtime::component::Resource<Y>,), 516 (wasmtime::component::ResourceAny,), 517 >(&mut store, &self.constructor_a_constructor)? 518 .func(); 519 let static_a_static_a = *_instance 520 .get_typed_func::< 521 (), 522 (wasmtime::component::Resource<Y>,), 523 >(&mut store, &self.static_a_static_a)? 524 .func(); 525 let method_a_method_a = *_instance 526 .get_typed_func::< 527 ( 528 wasmtime::component::ResourceAny, 529 wasmtime::component::Resource<Y>, 530 ), 531 (wasmtime::component::Resource<Y>,), 532 >(&mut store, &self.method_a_method_a)? 533 .func(); 534 Ok(Guest { 535 constructor_a_constructor, 536 static_a_static_a, 537 method_a_method_a, 538 }) 539 } 540 } 541 impl Guest { 542 pub fn a(&self) -> GuestA<'_> { 543 GuestA { funcs: self } 544 } 545 } 546 impl GuestA<'_> { 547 pub async fn call_constructor<_T, _D>( 548 &self, 549 accessor: &wasmtime::component::Accessor<_T, _D>, 550 arg0: wasmtime::component::Resource<Y>, 551 ) -> wasmtime::Result<wasmtime::component::ResourceAny> 552 where 553 _T: Send, 554 _D: wasmtime::component::HasData, 555 { 556 let callee = unsafe { 557 wasmtime::component::TypedFunc::< 558 (wasmtime::component::Resource<Y>,), 559 (wasmtime::component::ResourceAny,), 560 >::new_unchecked(self.funcs.constructor_a_constructor) 561 }; 562 let ((ret0,), _) = callee 563 .call_concurrent(accessor, (arg0,)) 564 .await?; 565 Ok(ret0) 566 } 567 pub async fn call_static_a<_T, _D>( 568 &self, 569 accessor: &wasmtime::component::Accessor<_T, _D>, 570 ) -> wasmtime::Result<wasmtime::component::Resource<Y>> 571 where 572 _T: Send, 573 _D: wasmtime::component::HasData, 574 { 575 let callee = unsafe { 576 wasmtime::component::TypedFunc::< 577 (), 578 (wasmtime::component::Resource<Y>,), 579 >::new_unchecked(self.funcs.static_a_static_a) 580 }; 581 let ((ret0,), _) = callee.call_concurrent(accessor, ()).await?; 582 Ok(ret0) 583 } 584 pub async fn call_method_a<_T, _D>( 585 &self, 586 accessor: &wasmtime::component::Accessor<_T, _D>, 587 arg0: wasmtime::component::ResourceAny, 588 arg1: wasmtime::component::Resource<Y>, 589 ) -> wasmtime::Result<wasmtime::component::Resource<Y>> 590 where 591 _T: Send, 592 _D: wasmtime::component::HasData, 593 { 594 let callee = unsafe { 595 wasmtime::component::TypedFunc::< 596 ( 597 wasmtime::component::ResourceAny, 598 wasmtime::component::Resource<Y>, 599 ), 600 (wasmtime::component::Resource<Y>,), 601 >::new_unchecked(self.funcs.method_a_method_a) 602 }; 603 let ((ret0,), _) = callee 604 .call_concurrent(accessor, (arg0, arg1)) 605 .await?; 606 Ok(ret0) 607 } 608 } 609 } 610 #[allow(clippy::all)] 611 pub mod export_using_export1 { 612 #[allow(unused_imports)] 613 use wasmtime::component::__internal::Box; 614 pub type A = wasmtime::component::ResourceAny; 615 pub struct GuestA<'a> { 616 funcs: &'a Guest, 617 } 618 #[derive(Clone)] 619 pub struct Guest { 620 constructor_a_constructor: wasmtime::component::Func, 621 } 622 #[derive(Clone)] 623 pub struct GuestIndices { 624 constructor_a_constructor: wasmtime::component::ComponentExportIndex, 625 } 626 impl GuestIndices { 627 /// Constructor for [`GuestIndices`] which takes a 628 /// [`Component`](wasmtime::component::Component) as input and can be executed 629 /// before instantiation. 630 /// 631 /// This constructor can be used to front-load string lookups to find exports 632 /// within a component. 633 pub fn new<_T>( 634 _instance_pre: &wasmtime::component::InstancePre<_T>, 635 ) -> wasmtime::Result<GuestIndices> { 636 let instance = _instance_pre 637 .component() 638 .get_export_index(None, "foo:foo/export-using-export1") 639 .ok_or_else(|| { 640 wasmtime::format_err!( 641 "no exported instance named `foo:foo/export-using-export1`" 642 ) 643 })?; 644 let mut lookup = move |name| { 645 _instance_pre 646 .component() 647 .get_export_index(Some(&instance), name) 648 .ok_or_else(|| { 649 wasmtime::format_err!( 650 "instance export `foo:foo/export-using-export1` does \ 651 not have export `{name}`" 652 ) 653 }) 654 }; 655 let _ = &mut lookup; 656 let constructor_a_constructor = lookup("[constructor]a")?; 657 Ok(GuestIndices { 658 constructor_a_constructor, 659 }) 660 } 661 pub fn load( 662 &self, 663 mut store: impl wasmtime::AsContextMut, 664 instance: &wasmtime::component::Instance, 665 ) -> wasmtime::Result<Guest> { 666 let _instance = instance; 667 let _instance_pre = _instance.instance_pre(&store); 668 let _instance_type = _instance_pre.instance_type(); 669 let mut store = store.as_context_mut(); 670 let _ = &mut store; 671 let constructor_a_constructor = *_instance 672 .get_typed_func::< 673 (), 674 (wasmtime::component::ResourceAny,), 675 >(&mut store, &self.constructor_a_constructor)? 676 .func(); 677 Ok(Guest { constructor_a_constructor }) 678 } 679 } 680 impl Guest { 681 pub fn a(&self) -> GuestA<'_> { 682 GuestA { funcs: self } 683 } 684 } 685 impl GuestA<'_> { 686 pub async fn call_constructor<_T, _D>( 687 &self, 688 accessor: &wasmtime::component::Accessor<_T, _D>, 689 ) -> wasmtime::Result<wasmtime::component::ResourceAny> 690 where 691 _T: Send, 692 _D: wasmtime::component::HasData, 693 { 694 let callee = unsafe { 695 wasmtime::component::TypedFunc::< 696 (), 697 (wasmtime::component::ResourceAny,), 698 >::new_unchecked(self.funcs.constructor_a_constructor) 699 }; 700 let ((ret0,), _) = callee.call_concurrent(accessor, ()).await?; 701 Ok(ret0) 702 } 703 } 704 } 705 #[allow(clippy::all)] 706 pub mod export_using_export2 { 707 #[allow(unused_imports)] 708 use wasmtime::component::__internal::Box; 709 pub type A = super::super::super::super::exports::foo::foo::export_using_export1::A; 710 pub type B = wasmtime::component::ResourceAny; 711 pub struct GuestB<'a> { 712 funcs: &'a Guest, 713 } 714 #[derive(Clone)] 715 pub struct Guest { 716 constructor_b_constructor: wasmtime::component::Func, 717 } 718 #[derive(Clone)] 719 pub struct GuestIndices { 720 constructor_b_constructor: wasmtime::component::ComponentExportIndex, 721 } 722 impl GuestIndices { 723 /// Constructor for [`GuestIndices`] which takes a 724 /// [`Component`](wasmtime::component::Component) as input and can be executed 725 /// before instantiation. 726 /// 727 /// This constructor can be used to front-load string lookups to find exports 728 /// within a component. 729 pub fn new<_T>( 730 _instance_pre: &wasmtime::component::InstancePre<_T>, 731 ) -> wasmtime::Result<GuestIndices> { 732 let instance = _instance_pre 733 .component() 734 .get_export_index(None, "foo:foo/export-using-export2") 735 .ok_or_else(|| { 736 wasmtime::format_err!( 737 "no exported instance named `foo:foo/export-using-export2`" 738 ) 739 })?; 740 let mut lookup = move |name| { 741 _instance_pre 742 .component() 743 .get_export_index(Some(&instance), name) 744 .ok_or_else(|| { 745 wasmtime::format_err!( 746 "instance export `foo:foo/export-using-export2` does \ 747 not have export `{name}`" 748 ) 749 }) 750 }; 751 let _ = &mut lookup; 752 let constructor_b_constructor = lookup("[constructor]b")?; 753 Ok(GuestIndices { 754 constructor_b_constructor, 755 }) 756 } 757 pub fn load( 758 &self, 759 mut store: impl wasmtime::AsContextMut, 760 instance: &wasmtime::component::Instance, 761 ) -> wasmtime::Result<Guest> { 762 let _instance = instance; 763 let _instance_pre = _instance.instance_pre(&store); 764 let _instance_type = _instance_pre.instance_type(); 765 let mut store = store.as_context_mut(); 766 let _ = &mut store; 767 let constructor_b_constructor = *_instance 768 .get_typed_func::< 769 (wasmtime::component::ResourceAny,), 770 (wasmtime::component::ResourceAny,), 771 >(&mut store, &self.constructor_b_constructor)? 772 .func(); 773 Ok(Guest { constructor_b_constructor }) 774 } 775 } 776 impl Guest { 777 pub fn b(&self) -> GuestB<'_> { 778 GuestB { funcs: self } 779 } 780 } 781 impl GuestB<'_> { 782 pub async fn call_constructor<_T, _D>( 783 &self, 784 accessor: &wasmtime::component::Accessor<_T, _D>, 785 arg0: wasmtime::component::ResourceAny, 786 ) -> wasmtime::Result<wasmtime::component::ResourceAny> 787 where 788 _T: Send, 789 _D: wasmtime::component::HasData, 790 { 791 let callee = unsafe { 792 wasmtime::component::TypedFunc::< 793 (wasmtime::component::ResourceAny,), 794 (wasmtime::component::ResourceAny,), 795 >::new_unchecked(self.funcs.constructor_b_constructor) 796 }; 797 let ((ret0,), _) = callee 798 .call_concurrent(accessor, (arg0,)) 799 .await?; 800 Ok(ret0) 801 } 802 } 803 } 804 } 805 } 806 } 807