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: 'static>( 198 accessor: &wasmtime::component::Accessor<T, Self>, 199 ) -> impl ::core::future::Future<Output = ()> + Send; 200 fn f2<T: 'static>( 201 accessor: &wasmtime::component::Accessor<T, Self>, 202 a: u32, 203 ) -> impl ::core::future::Future<Output = ()> + Send; 204 fn f3<T: 'static>( 205 accessor: &wasmtime::component::Accessor<T, Self>, 206 a: u32, 207 b: u32, 208 ) -> impl ::core::future::Future<Output = ()> + Send; 209 fn f4<T: 'static>( 210 accessor: &wasmtime::component::Accessor<T, Self>, 211 ) -> impl ::core::future::Future<Output = u32> + Send; 212 fn f5<T: 'static>( 213 accessor: &wasmtime::component::Accessor<T, Self>, 214 ) -> impl ::core::future::Future<Output = (u32, u32)> + Send; 215 fn f6<T: 'static>( 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 accessor = &caller.with_data(host_getter); 239 let r = <D as HostWithStore>::f1(accessor).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 accessor = &caller.with_data(host_getter); 249 let r = <D as HostWithStore>::f2(accessor, 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 accessor = &caller.with_data(host_getter); 262 let r = <D as HostWithStore>::f3(accessor, 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 accessor = &caller.with_data(host_getter); 272 let r = <D as HostWithStore>::f4(accessor).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 accessor = &caller.with_data(host_getter); 282 let r = <D as HostWithStore>::f5(accessor).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 accessor = &caller.with_data(host_getter); 295 let r = <D as HostWithStore>::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 async fn call_f1<_T, _D>( 411 &self, 412 accessor: &wasmtime::component::Accessor<_T, _D>, 413 ) -> wasmtime::Result<()> 414 where 415 _T: Send, 416 _D: wasmtime::component::HasData, 417 { 418 let callee = unsafe { 419 wasmtime::component::TypedFunc::< 420 (), 421 (), 422 >::new_unchecked(self.f1) 423 }; 424 let () = callee.call_concurrent(accessor, ()).await?; 425 Ok(()) 426 } 427 pub async fn call_f2<_T, _D>( 428 &self, 429 accessor: &wasmtime::component::Accessor<_T, _D>, 430 arg0: u32, 431 ) -> wasmtime::Result<()> 432 where 433 _T: Send, 434 _D: wasmtime::component::HasData, 435 { 436 let callee = unsafe { 437 wasmtime::component::TypedFunc::< 438 (u32,), 439 (), 440 >::new_unchecked(self.f2) 441 }; 442 let () = callee.call_concurrent(accessor, (arg0,)).await?; 443 Ok(()) 444 } 445 pub async fn call_f3<_T, _D>( 446 &self, 447 accessor: &wasmtime::component::Accessor<_T, _D>, 448 arg0: u32, 449 arg1: u32, 450 ) -> wasmtime::Result<()> 451 where 452 _T: Send, 453 _D: wasmtime::component::HasData, 454 { 455 let callee = unsafe { 456 wasmtime::component::TypedFunc::< 457 (u32, u32), 458 (), 459 >::new_unchecked(self.f3) 460 }; 461 let () = callee.call_concurrent(accessor, (arg0, arg1)).await?; 462 Ok(()) 463 } 464 pub async fn call_f4<_T, _D>( 465 &self, 466 accessor: &wasmtime::component::Accessor<_T, _D>, 467 ) -> wasmtime::Result<u32> 468 where 469 _T: Send, 470 _D: wasmtime::component::HasData, 471 { 472 let callee = unsafe { 473 wasmtime::component::TypedFunc::< 474 (), 475 (u32,), 476 >::new_unchecked(self.f4) 477 }; 478 let (ret0,) = callee.call_concurrent(accessor, ()).await?; 479 Ok(ret0) 480 } 481 pub async fn call_f5<_T, _D>( 482 &self, 483 accessor: &wasmtime::component::Accessor<_T, _D>, 484 ) -> wasmtime::Result<(u32, u32)> 485 where 486 _T: Send, 487 _D: wasmtime::component::HasData, 488 { 489 let callee = unsafe { 490 wasmtime::component::TypedFunc::< 491 (), 492 ((u32, u32),), 493 >::new_unchecked(self.f5) 494 }; 495 let (ret0,) = callee.call_concurrent(accessor, ()).await?; 496 Ok(ret0) 497 } 498 pub async fn call_f6<_T, _D>( 499 &self, 500 accessor: &wasmtime::component::Accessor<_T, _D>, 501 arg0: u32, 502 arg1: u32, 503 arg2: u32, 504 ) -> wasmtime::Result<(u32, u32, u32)> 505 where 506 _T: Send, 507 _D: wasmtime::component::HasData, 508 { 509 let callee = unsafe { 510 wasmtime::component::TypedFunc::< 511 (u32, u32, u32), 512 ((u32, u32, u32),), 513 >::new_unchecked(self.f6) 514 }; 515 let (ret0,) = callee 516 .call_concurrent(accessor, (arg0, arg1, arg2)) 517 .await?; 518 Ok(ret0) 519 } 520 } 521 } 522 } 523 } 524 } 525