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