1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// component which implements the world `my-world`. 3 /// 4 /// This structure is created through [`MyWorldPre::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 [`MyWorld`] as well. 9 pub struct MyWorldPre<T: 'static> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: MyWorldIndices, 12 } 13 impl<T: 'static> Clone for MyWorldPre<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> MyWorldPre<_T> { 22 /// Creates a new copy of `MyWorldPre` 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 = MyWorldIndices::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 [`MyWorld`] 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<MyWorld> { 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> MyWorldPre<_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<MyWorld> { 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 /// `my-world`. 68 /// 69 /// This is an implementation detail of [`MyWorldPre`] and can 70 /// be constructed if needed as well. 71 /// 72 /// For more information see [`MyWorld`] as well. 73 #[derive(Clone)] 74 pub struct MyWorldIndices { 75 interface0: exports::foo::foo::simple_lists::GuestIndices, 76 } 77 /// Auto-generated bindings for an instance a component which 78 /// implements the world `my-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 /// [`MyWorld::instantiate`] which only needs a 85 /// [`Store`], [`Component`], and [`Linker`]. 86 /// 87 /// * Alternatively you can create a [`MyWorldPre`] 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 [`MyWorldPre::instantiate`] to 91 /// create a [`MyWorld`]. 92 /// 93 /// * If you've instantiated the instance yourself already 94 /// then you can use [`MyWorld::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 MyWorld { 103 interface0: exports::foo::foo::simple_lists::Guest, 104 } 105 const _: () = { 106 impl MyWorldIndices { 107 /// Creates a new copy of `MyWorldIndices` 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. 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_lists::GuestIndices::new( 118 _instance_pre, 119 )?; 120 Ok(MyWorldIndices { interface0 }) 121 } 122 /// Uses the indices stored in `self` to load an instance 123 /// of [`MyWorld`] from the instance provided. 124 /// 125 /// Note that at this time this method will additionally 126 /// perform type-checks of all exports. 127 pub fn load( 128 &self, 129 mut store: impl wasmtime::AsContextMut, 130 instance: &wasmtime::component::Instance, 131 ) -> wasmtime::Result<MyWorld> { 132 let _ = &mut store; 133 let _instance = instance; 134 let interface0 = self.interface0.load(&mut store, &_instance)?; 135 Ok(MyWorld { interface0 }) 136 } 137 } 138 impl MyWorld { 139 /// Convenience wrapper around [`MyWorldPre::new`] and 140 /// [`MyWorldPre::instantiate`]. 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<MyWorld> { 146 let pre = linker.instantiate_pre(component)?; 147 MyWorldPre::new(pre)?.instantiate(store) 148 } 149 /// Convenience wrapper around [`MyWorldIndices::new`] and 150 /// [`MyWorldIndices::load`]. 151 pub fn new( 152 mut store: impl wasmtime::AsContextMut, 153 instance: &wasmtime::component::Instance, 154 ) -> wasmtime::Result<MyWorld> { 155 let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; 156 indices.load(&mut store, instance) 157 } 158 /// Convenience wrapper around [`MyWorldPre::new`] and 159 /// [`MyWorldPre::instantiate_async`]. 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<MyWorld> 165 where 166 _T: Send, 167 { 168 let pre = linker.instantiate_pre(component)?; 169 MyWorldPre::new(pre)?.instantiate_async(store).await 170 } 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_lists::HostWithStore + Send, 177 for<'a> D::Data<'a>: foo::foo::simple_lists::Host + Send, 178 T: 'static + Send, 179 { 180 foo::foo::simple_lists::add_to_linker::<T, D>(linker, host_getter)?; 181 Ok(()) 182 } 183 pub fn foo_foo_simple_lists(&self) -> &exports::foo::foo::simple_lists::Guest { 184 &self.interface0 185 } 186 } 187 }; 188 pub mod foo { 189 pub mod foo { 190 #[allow(clippy::all)] 191 pub mod simple_lists { 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 { 200 fn simple_list1( 201 &mut self, 202 l: wasmtime::component::__internal::Vec<u32>, 203 ) -> impl ::core::future::Future<Output = ()> + Send; 204 fn simple_list2( 205 &mut self, 206 ) -> impl ::core::future::Future< 207 Output = wasmtime::component::__internal::Vec<u32>, 208 > + Send; 209 fn simple_list3( 210 &mut self, 211 a: wasmtime::component::__internal::Vec<u32>, 212 b: wasmtime::component::__internal::Vec<u32>, 213 ) -> impl ::core::future::Future< 214 Output = ( 215 wasmtime::component::__internal::Vec<u32>, 216 wasmtime::component::__internal::Vec<u32>, 217 ), 218 > + Send; 219 fn simple_list4( 220 &mut self, 221 l: wasmtime::component::__internal::Vec< 222 wasmtime::component::__internal::Vec<u32>, 223 >, 224 ) -> impl ::core::future::Future< 225 Output = wasmtime::component::__internal::Vec< 226 wasmtime::component::__internal::Vec<u32>, 227 >, 228 > + Send; 229 } 230 impl<_T: Host + ?Sized + Send> Host for &mut _T { 231 fn simple_list1( 232 &mut self, 233 l: wasmtime::component::__internal::Vec<u32>, 234 ) -> impl ::core::future::Future<Output = ()> + Send { 235 async move { Host::simple_list1(*self, l).await } 236 } 237 fn simple_list2( 238 &mut self, 239 ) -> impl ::core::future::Future< 240 Output = wasmtime::component::__internal::Vec<u32>, 241 > + Send { 242 async move { Host::simple_list2(*self).await } 243 } 244 fn simple_list3( 245 &mut self, 246 a: wasmtime::component::__internal::Vec<u32>, 247 b: wasmtime::component::__internal::Vec<u32>, 248 ) -> impl ::core::future::Future< 249 Output = ( 250 wasmtime::component::__internal::Vec<u32>, 251 wasmtime::component::__internal::Vec<u32>, 252 ), 253 > + Send { 254 async move { Host::simple_list3(*self, a, b).await } 255 } 256 fn simple_list4( 257 &mut self, 258 l: wasmtime::component::__internal::Vec< 259 wasmtime::component::__internal::Vec<u32>, 260 >, 261 ) -> impl ::core::future::Future< 262 Output = wasmtime::component::__internal::Vec< 263 wasmtime::component::__internal::Vec<u32>, 264 >, 265 > + Send { 266 async move { Host::simple_list4(*self, l).await } 267 } 268 } 269 pub fn add_to_linker<T, D>( 270 linker: &mut wasmtime::component::Linker<T>, 271 host_getter: fn(&mut T) -> D::Data<'_>, 272 ) -> wasmtime::Result<()> 273 where 274 D: HostWithStore, 275 for<'a> D::Data<'a>: Host, 276 T: 'static + Send, 277 { 278 let mut inst = linker.instance("foo:foo/simple-lists")?; 279 inst.func_wrap_async( 280 "simple-list1", 281 move | 282 mut caller: wasmtime::StoreContextMut<'_, T>, 283 (arg0,): (wasmtime::component::__internal::Vec<u32>,)| 284 { 285 wasmtime::component::__internal::Box::new(async move { 286 let host = &mut host_getter(caller.data_mut()); 287 let r = Host::simple_list1(host, arg0).await; 288 Ok(r) 289 }) 290 }, 291 )?; 292 inst.func_wrap_async( 293 "simple-list2", 294 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 295 wasmtime::component::__internal::Box::new(async move { 296 let host = &mut host_getter(caller.data_mut()); 297 let r = Host::simple_list2(host).await; 298 Ok((r,)) 299 }) 300 }, 301 )?; 302 inst.func_wrap_async( 303 "simple-list3", 304 move | 305 mut caller: wasmtime::StoreContextMut<'_, T>, 306 ( 307 arg0, 308 arg1, 309 ): ( 310 wasmtime::component::__internal::Vec<u32>, 311 wasmtime::component::__internal::Vec<u32>, 312 )| 313 { 314 wasmtime::component::__internal::Box::new(async move { 315 let host = &mut host_getter(caller.data_mut()); 316 let r = Host::simple_list3(host, arg0, arg1).await; 317 Ok((r,)) 318 }) 319 }, 320 )?; 321 inst.func_wrap_async( 322 "simple-list4", 323 move | 324 mut caller: wasmtime::StoreContextMut<'_, T>, 325 ( 326 arg0, 327 ): ( 328 wasmtime::component::__internal::Vec< 329 wasmtime::component::__internal::Vec<u32>, 330 >, 331 )| 332 { 333 wasmtime::component::__internal::Box::new(async move { 334 let host = &mut host_getter(caller.data_mut()); 335 let r = Host::simple_list4(host, arg0).await; 336 Ok((r,)) 337 }) 338 }, 339 )?; 340 Ok(()) 341 } 342 } 343 } 344 } 345 pub mod exports { 346 pub mod foo { 347 pub mod foo { 348 #[allow(clippy::all)] 349 pub mod simple_lists { 350 #[allow(unused_imports)] 351 use wasmtime::component::__internal::Box; 352 #[derive(Clone)] 353 pub struct Guest { 354 simple_list1: wasmtime::component::Func, 355 simple_list2: wasmtime::component::Func, 356 simple_list3: wasmtime::component::Func, 357 simple_list4: wasmtime::component::Func, 358 } 359 #[derive(Clone)] 360 pub struct GuestIndices { 361 simple_list1: wasmtime::component::ComponentExportIndex, 362 simple_list2: wasmtime::component::ComponentExportIndex, 363 simple_list3: wasmtime::component::ComponentExportIndex, 364 simple_list4: wasmtime::component::ComponentExportIndex, 365 } 366 impl GuestIndices { 367 /// Constructor for [`GuestIndices`] which takes a 368 /// [`Component`](wasmtime::component::Component) as input and can be executed 369 /// before instantiation. 370 /// 371 /// This constructor can be used to front-load string lookups to find exports 372 /// within a component. 373 pub fn new<_T>( 374 _instance_pre: &wasmtime::component::InstancePre<_T>, 375 ) -> wasmtime::Result<GuestIndices> { 376 let instance = _instance_pre 377 .component() 378 .get_export_index(None, "foo:foo/simple-lists") 379 .ok_or_else(|| { 380 wasmtime::format_err!( 381 "no exported instance named `foo:foo/simple-lists`" 382 ) 383 })?; 384 let mut lookup = move |name| { 385 _instance_pre 386 .component() 387 .get_export_index(Some(&instance), name) 388 .ok_or_else(|| { 389 wasmtime::format_err!( 390 "instance export `foo:foo/simple-lists` does \ 391 not have export `{name}`" 392 ) 393 }) 394 }; 395 let _ = &mut lookup; 396 let simple_list1 = lookup("simple-list1")?; 397 let simple_list2 = lookup("simple-list2")?; 398 let simple_list3 = lookup("simple-list3")?; 399 let simple_list4 = lookup("simple-list4")?; 400 Ok(GuestIndices { 401 simple_list1, 402 simple_list2, 403 simple_list3, 404 simple_list4, 405 }) 406 } 407 pub fn load( 408 &self, 409 mut store: impl wasmtime::AsContextMut, 410 instance: &wasmtime::component::Instance, 411 ) -> wasmtime::Result<Guest> { 412 let _instance = instance; 413 let _instance_pre = _instance.instance_pre(&store); 414 let _instance_type = _instance_pre.instance_type(); 415 let mut store = store.as_context_mut(); 416 let _ = &mut store; 417 let simple_list1 = *_instance 418 .get_typed_func::< 419 (&[u32],), 420 (), 421 >(&mut store, &self.simple_list1)? 422 .func(); 423 let simple_list2 = *_instance 424 .get_typed_func::< 425 (), 426 (wasmtime::component::__internal::Vec<u32>,), 427 >(&mut store, &self.simple_list2)? 428 .func(); 429 let simple_list3 = *_instance 430 .get_typed_func::< 431 (&[u32], &[u32]), 432 ( 433 ( 434 wasmtime::component::__internal::Vec<u32>, 435 wasmtime::component::__internal::Vec<u32>, 436 ), 437 ), 438 >(&mut store, &self.simple_list3)? 439 .func(); 440 let simple_list4 = *_instance 441 .get_typed_func::< 442 (&[wasmtime::component::__internal::Vec<u32>],), 443 ( 444 wasmtime::component::__internal::Vec< 445 wasmtime::component::__internal::Vec<u32>, 446 >, 447 ), 448 >(&mut store, &self.simple_list4)? 449 .func(); 450 Ok(Guest { 451 simple_list1, 452 simple_list2, 453 simple_list3, 454 simple_list4, 455 }) 456 } 457 } 458 impl Guest { 459 pub async fn call_simple_list1<S: wasmtime::AsContextMut>( 460 &self, 461 mut store: S, 462 arg0: &[u32], 463 ) -> wasmtime::Result<()> 464 where 465 <S as wasmtime::AsContext>::Data: Send, 466 { 467 let callee = unsafe { 468 wasmtime::component::TypedFunc::< 469 (&[u32],), 470 (), 471 >::new_unchecked(self.simple_list1) 472 }; 473 let () = callee 474 .call_async(store.as_context_mut(), (arg0,)) 475 .await?; 476 Ok(()) 477 } 478 pub async fn call_simple_list2<S: wasmtime::AsContextMut>( 479 &self, 480 mut store: S, 481 ) -> wasmtime::Result<wasmtime::component::__internal::Vec<u32>> 482 where 483 <S as wasmtime::AsContext>::Data: Send, 484 { 485 let callee = unsafe { 486 wasmtime::component::TypedFunc::< 487 (), 488 (wasmtime::component::__internal::Vec<u32>,), 489 >::new_unchecked(self.simple_list2) 490 }; 491 let (ret0,) = callee 492 .call_async(store.as_context_mut(), ()) 493 .await?; 494 Ok(ret0) 495 } 496 pub async fn call_simple_list3<S: wasmtime::AsContextMut>( 497 &self, 498 mut store: S, 499 arg0: &[u32], 500 arg1: &[u32], 501 ) -> wasmtime::Result< 502 ( 503 wasmtime::component::__internal::Vec<u32>, 504 wasmtime::component::__internal::Vec<u32>, 505 ), 506 > 507 where 508 <S as wasmtime::AsContext>::Data: Send, 509 { 510 let callee = unsafe { 511 wasmtime::component::TypedFunc::< 512 (&[u32], &[u32]), 513 ( 514 ( 515 wasmtime::component::__internal::Vec<u32>, 516 wasmtime::component::__internal::Vec<u32>, 517 ), 518 ), 519 >::new_unchecked(self.simple_list3) 520 }; 521 let (ret0,) = callee 522 .call_async(store.as_context_mut(), (arg0, arg1)) 523 .await?; 524 Ok(ret0) 525 } 526 pub async fn call_simple_list4<S: wasmtime::AsContextMut>( 527 &self, 528 mut store: S, 529 arg0: &[wasmtime::component::__internal::Vec<u32>], 530 ) -> wasmtime::Result< 531 wasmtime::component::__internal::Vec< 532 wasmtime::component::__internal::Vec<u32>, 533 >, 534 > 535 where 536 <S as wasmtime::AsContext>::Data: Send, 537 { 538 let callee = unsafe { 539 wasmtime::component::TypedFunc::< 540 (&[wasmtime::component::__internal::Vec<u32>],), 541 ( 542 wasmtime::component::__internal::Vec< 543 wasmtime::component::__internal::Vec<u32>, 544 >, 545 ), 546 >::new_unchecked(self.simple_list4) 547 }; 548 let (ret0,) = callee 549 .call_async(store.as_context_mut(), (arg0,)) 550 .await?; 551 Ok(ret0) 552 } 553 } 554 } 555 } 556 } 557 } 558