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> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: TheWorldIndices, 12 } 13 impl<T> 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> 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.component())?; 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 + 'static, 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::strings::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 /// * You can also access the guts of instantiation through 89 /// [`TheWorldIndices::new_instance`] followed 90 /// by [`TheWorldIndices::load`] to crate an instance of this 91 /// type. 92 /// 93 /// These methods are all equivalent to one another and move 94 /// around the tradeoff of what work is performed when. 95 /// 96 /// [`Store`]: wasmtime::Store 97 /// [`Component`]: wasmtime::component::Component 98 /// [`Linker`]: wasmtime::component::Linker 99 pub struct TheWorld { 100 interface0: exports::foo::foo::strings::Guest, 101 } 102 const _: () = { 103 #[allow(unused_imports)] 104 use wasmtime::component::__internal::anyhow; 105 impl TheWorldIndices { 106 /// Creates a new copy of `TheWorldIndices` bindings which can then 107 /// be used to instantiate into a particular store. 108 /// 109 /// This method may fail if the component does not have the 110 /// required exports. 111 pub fn new( 112 component: &wasmtime::component::Component, 113 ) -> wasmtime::Result<Self> { 114 let _component = component; 115 let interface0 = exports::foo::foo::strings::GuestIndices::new(_component)?; 116 Ok(TheWorldIndices { interface0 }) 117 } 118 /// Creates a new instance of [`TheWorldIndices`] from an 119 /// instantiated component. 120 /// 121 /// This method of creating a [`TheWorld`] will perform string 122 /// lookups for all exports when this method is called. This 123 /// will only succeed if the provided instance matches the 124 /// requirements of [`TheWorld`]. 125 pub fn new_instance( 126 mut store: impl wasmtime::AsContextMut, 127 instance: &wasmtime::component::Instance, 128 ) -> wasmtime::Result<Self> { 129 let _instance = instance; 130 let interface0 = exports::foo::foo::strings::GuestIndices::new_instance( 131 &mut store, 132 _instance, 133 )?; 134 Ok(TheWorldIndices { interface0 }) 135 } 136 /// Uses the indices stored in `self` to load an instance 137 /// of [`TheWorld`] from the instance provided. 138 /// 139 /// Note that at this time this method will additionally 140 /// perform type-checks of all exports. 141 pub fn load( 142 &self, 143 mut store: impl wasmtime::AsContextMut, 144 instance: &wasmtime::component::Instance, 145 ) -> wasmtime::Result<TheWorld> { 146 let _instance = instance; 147 let interface0 = self.interface0.load(&mut store, &_instance)?; 148 Ok(TheWorld { interface0 }) 149 } 150 } 151 impl TheWorld { 152 /// Convenience wrapper around [`TheWorldPre::new`] and 153 /// [`TheWorldPre::instantiate_async`]. 154 pub async fn instantiate_async<_T>( 155 mut store: impl wasmtime::AsContextMut<Data = _T>, 156 component: &wasmtime::component::Component, 157 linker: &wasmtime::component::Linker<_T>, 158 ) -> wasmtime::Result<TheWorld> 159 where 160 _T: Send + 'static, 161 { 162 let pre = linker.instantiate_pre(component)?; 163 TheWorldPre::new(pre)?.instantiate_async(store).await 164 } 165 /// Convenience wrapper around [`TheWorldIndices::new_instance`] and 166 /// [`TheWorldIndices::load`]. 167 pub fn new( 168 mut store: impl wasmtime::AsContextMut, 169 instance: &wasmtime::component::Instance, 170 ) -> wasmtime::Result<TheWorld> { 171 let indices = TheWorldIndices::new_instance(&mut store, instance)?; 172 indices.load(store, instance) 173 } 174 pub fn add_to_linker<T, U>( 175 linker: &mut wasmtime::component::Linker<T>, 176 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 177 ) -> wasmtime::Result<()> 178 where 179 T: Send + foo::foo::strings::Host<Data = T> + 'static, 180 U: Send + foo::foo::strings::Host<Data = T>, 181 { 182 foo::foo::strings::add_to_linker(linker, get)?; 183 Ok(()) 184 } 185 pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { 186 &self.interface0 187 } 188 } 189 }; 190 pub mod foo { 191 pub mod foo { 192 #[allow(clippy::all)] 193 pub mod strings { 194 #[allow(unused_imports)] 195 use wasmtime::component::__internal::{anyhow, Box}; 196 pub trait Host { 197 type Data; 198 fn a( 199 store: wasmtime::StoreContextMut<'_, Self::Data>, 200 x: wasmtime::component::__internal::String, 201 ) -> impl ::core::future::Future< 202 Output = impl FnOnce( 203 wasmtime::StoreContextMut<'_, Self::Data>, 204 ) -> () + Send + Sync + 'static, 205 > + Send + Sync + 'static 206 where 207 Self: Sized; 208 fn b( 209 store: wasmtime::StoreContextMut<'_, Self::Data>, 210 ) -> impl ::core::future::Future< 211 Output = impl FnOnce( 212 wasmtime::StoreContextMut<'_, Self::Data>, 213 ) -> wasmtime::component::__internal::String + Send + Sync + 'static, 214 > + Send + Sync + 'static 215 where 216 Self: Sized; 217 fn c( 218 store: wasmtime::StoreContextMut<'_, Self::Data>, 219 a: wasmtime::component::__internal::String, 220 b: wasmtime::component::__internal::String, 221 ) -> impl ::core::future::Future< 222 Output = impl FnOnce( 223 wasmtime::StoreContextMut<'_, Self::Data>, 224 ) -> wasmtime::component::__internal::String + Send + Sync + 'static, 225 > + Send + Sync + 'static 226 where 227 Self: Sized; 228 } 229 pub trait GetHost< 230 T, 231 D, 232 >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static { 233 type Host: Host<Data = D> + Send; 234 } 235 impl<F, T, D, O> GetHost<T, D> for F 236 where 237 F: Fn(T) -> O + Send + Sync + Copy + 'static, 238 O: Host<Data = D> + Send, 239 { 240 type Host = O; 241 } 242 pub fn add_to_linker_get_host< 243 T, 244 G: for<'a> GetHost<&'a mut T, T, Host: Host<Data = T> + Send>, 245 >( 246 linker: &mut wasmtime::component::Linker<T>, 247 host_getter: G, 248 ) -> wasmtime::Result<()> 249 where 250 T: Send + 'static, 251 { 252 let mut inst = linker.instance("foo:foo/strings")?; 253 inst.func_wrap_concurrent( 254 "a", 255 move | 256 mut caller: wasmtime::StoreContextMut<'_, T>, 257 (arg0,): (wasmtime::component::__internal::String,)| 258 { 259 let host = caller; 260 let r = <G::Host as Host>::a(host, arg0); 261 Box::pin(async move { 262 let fun = r.await; 263 Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { 264 let r = fun(caller); 265 Ok(r) 266 }) 267 as Box< 268 dyn FnOnce( 269 wasmtime::StoreContextMut<'_, T>, 270 ) -> wasmtime::Result<()> + Send + Sync, 271 > 272 }) 273 as ::core::pin::Pin< 274 Box< 275 dyn ::core::future::Future< 276 Output = Box< 277 dyn FnOnce( 278 wasmtime::StoreContextMut<'_, T>, 279 ) -> wasmtime::Result<()> + Send + Sync, 280 >, 281 > + Send + Sync + 'static, 282 >, 283 > 284 }, 285 )?; 286 inst.func_wrap_concurrent( 287 "b", 288 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 289 let host = caller; 290 let r = <G::Host as Host>::b(host); 291 Box::pin(async move { 292 let fun = r.await; 293 Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { 294 let r = fun(caller); 295 Ok((r,)) 296 }) 297 as Box< 298 dyn FnOnce( 299 wasmtime::StoreContextMut<'_, T>, 300 ) -> wasmtime::Result< 301 (wasmtime::component::__internal::String,), 302 > + Send + Sync, 303 > 304 }) 305 as ::core::pin::Pin< 306 Box< 307 dyn ::core::future::Future< 308 Output = Box< 309 dyn FnOnce( 310 wasmtime::StoreContextMut<'_, T>, 311 ) -> wasmtime::Result< 312 (wasmtime::component::__internal::String,), 313 > + Send + Sync, 314 >, 315 > + Send + Sync + 'static, 316 >, 317 > 318 }, 319 )?; 320 inst.func_wrap_concurrent( 321 "c", 322 move | 323 mut caller: wasmtime::StoreContextMut<'_, T>, 324 ( 325 arg0, 326 arg1, 327 ): ( 328 wasmtime::component::__internal::String, 329 wasmtime::component::__internal::String, 330 )| 331 { 332 let host = caller; 333 let r = <G::Host as Host>::c(host, arg0, arg1); 334 Box::pin(async move { 335 let fun = r.await; 336 Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { 337 let r = fun(caller); 338 Ok((r,)) 339 }) 340 as Box< 341 dyn FnOnce( 342 wasmtime::StoreContextMut<'_, T>, 343 ) -> wasmtime::Result< 344 (wasmtime::component::__internal::String,), 345 > + Send + Sync, 346 > 347 }) 348 as ::core::pin::Pin< 349 Box< 350 dyn ::core::future::Future< 351 Output = Box< 352 dyn FnOnce( 353 wasmtime::StoreContextMut<'_, T>, 354 ) -> wasmtime::Result< 355 (wasmtime::component::__internal::String,), 356 > + Send + Sync, 357 >, 358 > + Send + Sync + 'static, 359 >, 360 > 361 }, 362 )?; 363 Ok(()) 364 } 365 pub fn add_to_linker<T, U>( 366 linker: &mut wasmtime::component::Linker<T>, 367 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 368 ) -> wasmtime::Result<()> 369 where 370 U: Host<Data = T> + Send, 371 T: Send + 'static, 372 { 373 add_to_linker_get_host(linker, get) 374 } 375 impl<_T: Host> Host for &mut _T { 376 type Data = _T::Data; 377 fn a( 378 store: wasmtime::StoreContextMut<'_, Self::Data>, 379 x: wasmtime::component::__internal::String, 380 ) -> impl ::core::future::Future< 381 Output = impl FnOnce( 382 wasmtime::StoreContextMut<'_, Self::Data>, 383 ) -> () + Send + Sync + 'static, 384 > + Send + Sync + 'static 385 where 386 Self: Sized, 387 { 388 <_T as Host>::a(store, x) 389 } 390 fn b( 391 store: wasmtime::StoreContextMut<'_, Self::Data>, 392 ) -> impl ::core::future::Future< 393 Output = impl FnOnce( 394 wasmtime::StoreContextMut<'_, Self::Data>, 395 ) -> wasmtime::component::__internal::String + Send + Sync + 'static, 396 > + Send + Sync + 'static 397 where 398 Self: Sized, 399 { 400 <_T as Host>::b(store) 401 } 402 fn c( 403 store: wasmtime::StoreContextMut<'_, Self::Data>, 404 a: wasmtime::component::__internal::String, 405 b: wasmtime::component::__internal::String, 406 ) -> impl ::core::future::Future< 407 Output = impl FnOnce( 408 wasmtime::StoreContextMut<'_, Self::Data>, 409 ) -> wasmtime::component::__internal::String + Send + Sync + 'static, 410 > + Send + Sync + 'static 411 where 412 Self: Sized, 413 { 414 <_T as Host>::c(store, a, b) 415 } 416 } 417 } 418 } 419 } 420 pub mod exports { 421 pub mod foo { 422 pub mod foo { 423 #[allow(clippy::all)] 424 pub mod strings { 425 #[allow(unused_imports)] 426 use wasmtime::component::__internal::{anyhow, Box}; 427 pub struct Guest { 428 a: wasmtime::component::Func, 429 b: wasmtime::component::Func, 430 c: wasmtime::component::Func, 431 } 432 #[derive(Clone)] 433 pub struct GuestIndices { 434 a: wasmtime::component::ComponentExportIndex, 435 b: wasmtime::component::ComponentExportIndex, 436 c: wasmtime::component::ComponentExportIndex, 437 } 438 impl GuestIndices { 439 /// Constructor for [`GuestIndices`] which takes a 440 /// [`Component`](wasmtime::component::Component) as input and can be executed 441 /// before instantiation. 442 /// 443 /// This constructor can be used to front-load string lookups to find exports 444 /// within a component. 445 pub fn new( 446 component: &wasmtime::component::Component, 447 ) -> wasmtime::Result<GuestIndices> { 448 let (_, instance) = component 449 .export_index(None, "foo:foo/strings") 450 .ok_or_else(|| { 451 anyhow::anyhow!( 452 "no exported instance named `foo:foo/strings`" 453 ) 454 })?; 455 Self::_new(|name| { 456 component.export_index(Some(&instance), name).map(|p| p.1) 457 }) 458 } 459 /// This constructor is similar to [`GuestIndices::new`] except that it 460 /// performs string lookups after instantiation time. 461 pub fn new_instance( 462 mut store: impl wasmtime::AsContextMut, 463 instance: &wasmtime::component::Instance, 464 ) -> wasmtime::Result<GuestIndices> { 465 let instance_export = instance 466 .get_export(&mut store, None, "foo:foo/strings") 467 .ok_or_else(|| { 468 anyhow::anyhow!( 469 "no exported instance named `foo:foo/strings`" 470 ) 471 })?; 472 Self::_new(|name| { 473 instance.get_export(&mut store, Some(&instance_export), name) 474 }) 475 } 476 fn _new( 477 mut lookup: impl FnMut( 478 &str, 479 ) -> Option<wasmtime::component::ComponentExportIndex>, 480 ) -> wasmtime::Result<GuestIndices> { 481 let mut lookup = move |name| { 482 lookup(name) 483 .ok_or_else(|| { 484 anyhow::anyhow!( 485 "instance export `foo:foo/strings` does \ 486 not have export `{name}`" 487 ) 488 }) 489 }; 490 let _ = &mut lookup; 491 let a = lookup("a")?; 492 let b = lookup("b")?; 493 let c = lookup("c")?; 494 Ok(GuestIndices { a, b, c }) 495 } 496 pub fn load( 497 &self, 498 mut store: impl wasmtime::AsContextMut, 499 instance: &wasmtime::component::Instance, 500 ) -> wasmtime::Result<Guest> { 501 let mut store = store.as_context_mut(); 502 let _ = &mut store; 503 let _instance = instance; 504 let a = *_instance 505 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 506 .func(); 507 let b = *_instance 508 .get_typed_func::< 509 (), 510 (wasmtime::component::__internal::String,), 511 >(&mut store, &self.b)? 512 .func(); 513 let c = *_instance 514 .get_typed_func::< 515 (&str, &str), 516 (wasmtime::component::__internal::String,), 517 >(&mut store, &self.c)? 518 .func(); 519 Ok(Guest { a, b, c }) 520 } 521 } 522 impl Guest { 523 pub async fn call_a<S: wasmtime::AsContextMut>( 524 &self, 525 mut store: S, 526 arg0: wasmtime::component::__internal::String, 527 ) -> wasmtime::Result<wasmtime::component::Promise<()>> 528 where 529 <S as wasmtime::AsContext>::Data: Send + 'static, 530 { 531 let callee = unsafe { 532 wasmtime::component::TypedFunc::< 533 (wasmtime::component::__internal::String,), 534 (), 535 >::new_unchecked(self.a) 536 }; 537 let promise = callee 538 .call_concurrent(store.as_context_mut(), (arg0,)) 539 .await?; 540 Ok(promise) 541 } 542 pub async fn call_b<S: wasmtime::AsContextMut>( 543 &self, 544 mut store: S, 545 ) -> wasmtime::Result< 546 wasmtime::component::Promise< 547 wasmtime::component::__internal::String, 548 >, 549 > 550 where 551 <S as wasmtime::AsContext>::Data: Send + 'static, 552 { 553 let callee = unsafe { 554 wasmtime::component::TypedFunc::< 555 (), 556 (wasmtime::component::__internal::String,), 557 >::new_unchecked(self.b) 558 }; 559 let promise = callee 560 .call_concurrent(store.as_context_mut(), ()) 561 .await?; 562 Ok(promise.map(|(v,)| v)) 563 } 564 pub async fn call_c<S: wasmtime::AsContextMut>( 565 &self, 566 mut store: S, 567 arg0: wasmtime::component::__internal::String, 568 arg1: wasmtime::component::__internal::String, 569 ) -> wasmtime::Result< 570 wasmtime::component::Promise< 571 wasmtime::component::__internal::String, 572 >, 573 > 574 where 575 <S as wasmtime::AsContext>::Data: Send + 'static, 576 { 577 let callee = unsafe { 578 wasmtime::component::TypedFunc::< 579 ( 580 wasmtime::component::__internal::String, 581 wasmtime::component::__internal::String, 582 ), 583 (wasmtime::component::__internal::String,), 584 >::new_unchecked(self.c) 585 }; 586 let promise = callee 587 .call_concurrent(store.as_context_mut(), (arg0, arg1)) 588 .await?; 589 Ok(promise.map(|(v,)| v)) 590 } 591 } 592 } 593 } 594 } 595 } 596