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