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