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