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