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 + '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 /// 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::strings::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::strings::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 + 'static, 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, U>( 156 linker: &mut wasmtime::component::Linker<T>, 157 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 158 ) -> wasmtime::Result<()> 159 where 160 T: 'static, 161 T: Send + foo::foo::strings::Host<Data = T>, 162 U: Send + foo::foo::strings::Host<Data = T>, 163 { 164 foo::foo::strings::add_to_linker(linker, get)?; 165 Ok(()) 166 } 167 pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { 168 &self.interface0 169 } 170 } 171 }; 172 pub mod foo { 173 pub mod foo { 174 #[allow(clippy::all)] 175 pub mod strings { 176 #[allow(unused_imports)] 177 use wasmtime::component::__internal::{anyhow, Box}; 178 pub trait Host { 179 type Data; 180 fn a( 181 store: wasmtime::StoreContextMut<'_, Self::Data>, 182 x: wasmtime::component::__internal::String, 183 ) -> impl ::core::future::Future< 184 Output = impl FnOnce( 185 wasmtime::StoreContextMut<'_, Self::Data>, 186 ) -> () + Send + Sync + 'static, 187 > + Send + Sync + 'static 188 where 189 Self: Sized; 190 fn b( 191 store: wasmtime::StoreContextMut<'_, Self::Data>, 192 ) -> impl ::core::future::Future< 193 Output = impl FnOnce( 194 wasmtime::StoreContextMut<'_, Self::Data>, 195 ) -> wasmtime::component::__internal::String + Send + Sync + 'static, 196 > + Send + Sync + 'static 197 where 198 Self: Sized; 199 fn c( 200 store: wasmtime::StoreContextMut<'_, Self::Data>, 201 a: wasmtime::component::__internal::String, 202 b: wasmtime::component::__internal::String, 203 ) -> impl ::core::future::Future< 204 Output = impl FnOnce( 205 wasmtime::StoreContextMut<'_, Self::Data>, 206 ) -> wasmtime::component::__internal::String + Send + Sync + 'static, 207 > + Send + Sync + 'static 208 where 209 Self: Sized; 210 } 211 pub fn add_to_linker_get_host<T, G>( 212 linker: &mut wasmtime::component::Linker<T>, 213 host_getter: G, 214 ) -> wasmtime::Result<()> 215 where 216 T: 'static, 217 G: for<'a> wasmtime::component::GetHost< 218 &'a mut T, 219 Host: Host<Data = T> + Send, 220 >, 221 T: Send + 'static, 222 { 223 let mut inst = linker.instance("foo:foo/strings")?; 224 inst.func_wrap_concurrent( 225 "a", 226 move | 227 mut caller: wasmtime::StoreContextMut<'_, T>, 228 (arg0,): (wasmtime::component::__internal::String,)| 229 { 230 let host = caller; 231 let r = <G::Host as Host>::a(host, arg0); 232 Box::pin(async move { 233 let fun = r.await; 234 Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { 235 let r = fun(caller); 236 Ok(r) 237 }) 238 as Box< 239 dyn FnOnce( 240 wasmtime::StoreContextMut<'_, T>, 241 ) -> wasmtime::Result<()> + Send + Sync, 242 > 243 }) 244 as ::core::pin::Pin< 245 Box< 246 dyn ::core::future::Future< 247 Output = Box< 248 dyn FnOnce( 249 wasmtime::StoreContextMut<'_, T>, 250 ) -> wasmtime::Result<()> + Send + Sync, 251 >, 252 > + Send + Sync + 'static, 253 >, 254 > 255 }, 256 )?; 257 inst.func_wrap_concurrent( 258 "b", 259 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 260 let host = caller; 261 let r = <G::Host as Host>::b(host); 262 Box::pin(async move { 263 let fun = r.await; 264 Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { 265 let r = fun(caller); 266 Ok((r,)) 267 }) 268 as Box< 269 dyn FnOnce( 270 wasmtime::StoreContextMut<'_, T>, 271 ) -> wasmtime::Result< 272 (wasmtime::component::__internal::String,), 273 > + Send + Sync, 274 > 275 }) 276 as ::core::pin::Pin< 277 Box< 278 dyn ::core::future::Future< 279 Output = Box< 280 dyn FnOnce( 281 wasmtime::StoreContextMut<'_, T>, 282 ) -> wasmtime::Result< 283 (wasmtime::component::__internal::String,), 284 > + Send + Sync, 285 >, 286 > + Send + Sync + 'static, 287 >, 288 > 289 }, 290 )?; 291 inst.func_wrap_concurrent( 292 "c", 293 move | 294 mut caller: wasmtime::StoreContextMut<'_, T>, 295 ( 296 arg0, 297 arg1, 298 ): ( 299 wasmtime::component::__internal::String, 300 wasmtime::component::__internal::String, 301 )| 302 { 303 let host = caller; 304 let r = <G::Host as Host>::c(host, arg0, arg1); 305 Box::pin(async move { 306 let fun = r.await; 307 Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { 308 let r = fun(caller); 309 Ok((r,)) 310 }) 311 as Box< 312 dyn FnOnce( 313 wasmtime::StoreContextMut<'_, T>, 314 ) -> wasmtime::Result< 315 (wasmtime::component::__internal::String,), 316 > + Send + Sync, 317 > 318 }) 319 as ::core::pin::Pin< 320 Box< 321 dyn ::core::future::Future< 322 Output = Box< 323 dyn FnOnce( 324 wasmtime::StoreContextMut<'_, T>, 325 ) -> wasmtime::Result< 326 (wasmtime::component::__internal::String,), 327 > + Send + Sync, 328 >, 329 > + Send + Sync + 'static, 330 >, 331 > 332 }, 333 )?; 334 Ok(()) 335 } 336 pub fn add_to_linker<T, U>( 337 linker: &mut wasmtime::component::Linker<T>, 338 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 339 ) -> wasmtime::Result<()> 340 where 341 T: 'static, 342 U: Host<Data = T> + Send, 343 T: Send + 'static, 344 { 345 add_to_linker_get_host(linker, get) 346 } 347 impl<_T: Host> Host for &mut _T { 348 type Data = _T::Data; 349 fn a( 350 store: wasmtime::StoreContextMut<'_, Self::Data>, 351 x: wasmtime::component::__internal::String, 352 ) -> impl ::core::future::Future< 353 Output = impl FnOnce( 354 wasmtime::StoreContextMut<'_, Self::Data>, 355 ) -> () + Send + Sync + 'static, 356 > + Send + Sync + 'static 357 where 358 Self: Sized, 359 { 360 <_T as Host>::a(store, x) 361 } 362 fn b( 363 store: wasmtime::StoreContextMut<'_, Self::Data>, 364 ) -> impl ::core::future::Future< 365 Output = impl FnOnce( 366 wasmtime::StoreContextMut<'_, Self::Data>, 367 ) -> wasmtime::component::__internal::String + Send + Sync + 'static, 368 > + Send + Sync + 'static 369 where 370 Self: Sized, 371 { 372 <_T as Host>::b(store) 373 } 374 fn c( 375 store: wasmtime::StoreContextMut<'_, Self::Data>, 376 a: wasmtime::component::__internal::String, 377 b: wasmtime::component::__internal::String, 378 ) -> impl ::core::future::Future< 379 Output = impl FnOnce( 380 wasmtime::StoreContextMut<'_, Self::Data>, 381 ) -> wasmtime::component::__internal::String + Send + Sync + 'static, 382 > + Send + Sync + 'static 383 where 384 Self: Sized, 385 { 386 <_T as Host>::c(store, a, b) 387 } 388 } 389 } 390 } 391 } 392 pub mod exports { 393 pub mod foo { 394 pub mod foo { 395 #[allow(clippy::all)] 396 pub mod strings { 397 #[allow(unused_imports)] 398 use wasmtime::component::__internal::{anyhow, Box}; 399 pub struct Guest { 400 a: wasmtime::component::Func, 401 b: wasmtime::component::Func, 402 c: wasmtime::component::Func, 403 } 404 #[derive(Clone)] 405 pub struct GuestIndices { 406 a: wasmtime::component::ComponentExportIndex, 407 b: wasmtime::component::ComponentExportIndex, 408 c: wasmtime::component::ComponentExportIndex, 409 } 410 impl GuestIndices { 411 /// Constructor for [`GuestIndices`] which takes a 412 /// [`Component`](wasmtime::component::Component) as input and can be executed 413 /// before instantiation. 414 /// 415 /// This constructor can be used to front-load string lookups to find exports 416 /// within a component. 417 pub fn new<_T>( 418 _instance_pre: &wasmtime::component::InstancePre<_T>, 419 ) -> wasmtime::Result<GuestIndices> { 420 let instance = _instance_pre 421 .component() 422 .get_export_index(None, "foo:foo/strings") 423 .ok_or_else(|| { 424 anyhow::anyhow!( 425 "no exported instance named `foo:foo/strings`" 426 ) 427 })?; 428 let mut lookup = move |name| { 429 _instance_pre 430 .component() 431 .get_export_index(Some(&instance), name) 432 .ok_or_else(|| { 433 anyhow::anyhow!( 434 "instance export `foo:foo/strings` does \ 435 not have export `{name}`" 436 ) 437 }) 438 }; 439 let _ = &mut lookup; 440 let a = lookup("a")?; 441 let b = lookup("b")?; 442 let c = lookup("c")?; 443 Ok(GuestIndices { a, b, c }) 444 } 445 pub fn load( 446 &self, 447 mut store: impl wasmtime::AsContextMut, 448 instance: &wasmtime::component::Instance, 449 ) -> wasmtime::Result<Guest> { 450 let _instance = instance; 451 let _instance_pre = _instance.instance_pre(&store); 452 let _instance_type = _instance_pre.instance_type(); 453 let mut store = store.as_context_mut(); 454 let _ = &mut store; 455 let a = *_instance 456 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 457 .func(); 458 let b = *_instance 459 .get_typed_func::< 460 (), 461 (wasmtime::component::__internal::String,), 462 >(&mut store, &self.b)? 463 .func(); 464 let c = *_instance 465 .get_typed_func::< 466 (&str, &str), 467 (wasmtime::component::__internal::String,), 468 >(&mut store, &self.c)? 469 .func(); 470 Ok(Guest { a, b, c }) 471 } 472 } 473 impl Guest { 474 pub async fn call_a<S: wasmtime::AsContextMut>( 475 &self, 476 mut store: S, 477 arg0: wasmtime::component::__internal::String, 478 ) -> wasmtime::Result<wasmtime::component::Promise<()>> 479 where 480 <S as wasmtime::AsContext>::Data: Send + 'static, 481 { 482 let callee = unsafe { 483 wasmtime::component::TypedFunc::< 484 (wasmtime::component::__internal::String,), 485 (), 486 >::new_unchecked(self.a) 487 }; 488 let promise = callee 489 .call_concurrent(store.as_context_mut(), (arg0,)) 490 .await?; 491 Ok(promise) 492 } 493 pub async fn call_b<S: wasmtime::AsContextMut>( 494 &self, 495 mut store: S, 496 ) -> wasmtime::Result< 497 wasmtime::component::Promise< 498 wasmtime::component::__internal::String, 499 >, 500 > 501 where 502 <S as wasmtime::AsContext>::Data: Send + 'static, 503 { 504 let callee = unsafe { 505 wasmtime::component::TypedFunc::< 506 (), 507 (wasmtime::component::__internal::String,), 508 >::new_unchecked(self.b) 509 }; 510 let promise = callee 511 .call_concurrent(store.as_context_mut(), ()) 512 .await?; 513 Ok(promise.map(|(v,)| v)) 514 } 515 pub async fn call_c<S: wasmtime::AsContextMut>( 516 &self, 517 mut store: S, 518 arg0: wasmtime::component::__internal::String, 519 arg1: wasmtime::component::__internal::String, 520 ) -> wasmtime::Result< 521 wasmtime::component::Promise< 522 wasmtime::component::__internal::String, 523 >, 524 > 525 where 526 <S as wasmtime::AsContext>::Data: Send + 'static, 527 { 528 let callee = unsafe { 529 wasmtime::component::TypedFunc::< 530 ( 531 wasmtime::component::__internal::String, 532 wasmtime::component::__internal::String, 533 ), 534 (wasmtime::component::__internal::String,), 535 >::new_unchecked(self.c) 536 }; 537 let promise = callee 538 .call_concurrent(store.as_context_mut(), (arg0, arg1)) 539 .await?; 540 Ok(promise.map(|(v,)| v)) 541 } 542 } 543 } 544 } 545 } 546 } 547