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> { clone(&self) -> Self14 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. new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>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 } engine(&self) -> &wasmtime::Engine33 pub fn engine(&self) -> &wasmtime::Engine { 34 self.instance_pre.engine() 35 } instance_pre(&self) -> &wasmtime::component::InstancePre<_T>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. instantiate( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<TheWorld>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`. instantiate_async( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<TheWorld>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 impl TheWorldIndices { 107 /// Creates a new copy of `TheWorldIndices` 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. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>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::GuestIndices::new( 118 _instance_pre, 119 )?; 120 Ok(TheWorldIndices { interface0 }) 121 } 122 /// Uses the indices stored in `self` to load an instance 123 /// of [`TheWorld`] from the instance provided. 124 /// 125 /// Note that at this time this method will additionally 126 /// perform type-checks of all exports. load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<TheWorld>127 pub fn load( 128 &self, 129 mut store: impl wasmtime::AsContextMut, 130 instance: &wasmtime::component::Instance, 131 ) -> wasmtime::Result<TheWorld> { 132 let _ = &mut store; 133 let _instance = instance; 134 let interface0 = self.interface0.load(&mut store, &_instance)?; 135 Ok(TheWorld { interface0 }) 136 } 137 } 138 impl TheWorld { 139 /// Convenience wrapper around [`TheWorldPre::new`] and 140 /// [`TheWorldPre::instantiate`]. instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<TheWorld>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<TheWorld> { 146 let pre = linker.instantiate_pre(component)?; 147 TheWorldPre::new(pre)?.instantiate(store) 148 } 149 /// Convenience wrapper around [`TheWorldIndices::new`] and 150 /// [`TheWorldIndices::load`]. new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<TheWorld>151 pub fn new( 152 mut store: impl wasmtime::AsContextMut, 153 instance: &wasmtime::component::Instance, 154 ) -> wasmtime::Result<TheWorld> { 155 let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; 156 indices.load(&mut store, instance) 157 } 158 /// Convenience wrapper around [`TheWorldPre::new`] and 159 /// [`TheWorldPre::instantiate_async`]. instantiate_async<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<TheWorld> where _T: Send,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<TheWorld> 165 where 166 _T: Send, 167 { 168 let pre = linker.instantiate_pre(component)?; 169 TheWorldPre::new(pre)?.instantiate_async(store).await 170 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: foo::foo::simple::HostWithStore, for<'a> D::Data<'a>: foo::foo::simple::Host, T: 'static,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::HostWithStore, 177 for<'a> D::Data<'a>: foo::foo::simple::Host, 178 T: 'static, 179 { 180 foo::foo::simple::add_to_linker::<T, D>(linker, host_getter)?; 181 Ok(()) 182 } foo_foo_simple(&self) -> &exports::foo::foo::simple::Guest183 pub fn foo_foo_simple(&self) -> &exports::foo::foo::simple::Guest { 184 &self.interface0 185 } 186 } 187 }; 188 pub mod foo { 189 pub mod foo { 190 #[allow(clippy::all)] 191 pub mod simple { 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 { f1(&mut self) -> ()200 fn f1(&mut self) -> (); f2(&mut self, a: u32) -> ()201 fn f2(&mut self, a: u32) -> (); f3(&mut self, a: u32, b: u32) -> ()202 fn f3(&mut self, a: u32, b: u32) -> (); f4(&mut self) -> u32203 fn f4(&mut self) -> u32; f5(&mut self) -> (u32, u32)204 fn f5(&mut self) -> (u32, u32); f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32)205 fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32); 206 } 207 impl<_T: Host + ?Sized> Host for &mut _T { f1(&mut self) -> ()208 fn f1(&mut self) -> () { 209 Host::f1(*self) 210 } f2(&mut self, a: u32) -> ()211 fn f2(&mut self, a: u32) -> () { 212 Host::f2(*self, a) 213 } f3(&mut self, a: u32, b: u32) -> ()214 fn f3(&mut self, a: u32, b: u32) -> () { 215 Host::f3(*self, a, b) 216 } f4(&mut self) -> u32217 fn f4(&mut self) -> u32 { 218 Host::f4(*self) 219 } f5(&mut self) -> (u32, u32)220 fn f5(&mut self) -> (u32, u32) { 221 Host::f5(*self) 222 } f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32)223 fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32) { 224 Host::f6(*self, a, b, c) 225 } 226 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static,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, 235 { 236 let mut inst = linker.instance("foo:foo/simple")?; 237 inst.func_wrap( 238 "f1", 239 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 240 let host = &mut host_getter(caller.data_mut()); 241 let r = Host::f1(host); 242 Ok(r) 243 }, 244 )?; 245 inst.func_wrap( 246 "f2", 247 move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (u32,)| { 248 let host = &mut host_getter(caller.data_mut()); 249 let r = Host::f2(host, arg0); 250 Ok(r) 251 }, 252 )?; 253 inst.func_wrap( 254 "f3", 255 move | 256 mut caller: wasmtime::StoreContextMut<'_, T>, 257 (arg0, arg1): (u32, u32)| 258 { 259 let host = &mut host_getter(caller.data_mut()); 260 let r = Host::f3(host, arg0, arg1); 261 Ok(r) 262 }, 263 )?; 264 inst.func_wrap( 265 "f4", 266 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 267 let host = &mut host_getter(caller.data_mut()); 268 let r = Host::f4(host); 269 Ok((r,)) 270 }, 271 )?; 272 inst.func_wrap( 273 "f5", 274 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 275 let host = &mut host_getter(caller.data_mut()); 276 let r = Host::f5(host); 277 Ok((r,)) 278 }, 279 )?; 280 inst.func_wrap( 281 "f6", 282 move | 283 mut caller: wasmtime::StoreContextMut<'_, T>, 284 (arg0, arg1, arg2): (u32, u32, u32)| 285 { 286 let host = &mut host_getter(caller.data_mut()); 287 let r = Host::f6(host, arg0, arg1, arg2); 288 Ok((r,)) 289 }, 290 )?; 291 Ok(()) 292 } 293 } 294 } 295 } 296 pub mod exports { 297 pub mod foo { 298 pub mod foo { 299 #[allow(clippy::all)] 300 pub mod simple { 301 #[allow(unused_imports)] 302 use wasmtime::component::__internal::Box; 303 #[derive(Clone)] 304 pub struct Guest { 305 f1: wasmtime::component::Func, 306 f2: wasmtime::component::Func, 307 f3: wasmtime::component::Func, 308 f4: wasmtime::component::Func, 309 f5: wasmtime::component::Func, 310 f6: wasmtime::component::Func, 311 } 312 #[derive(Clone)] 313 pub struct GuestIndices { 314 f1: wasmtime::component::ComponentExportIndex, 315 f2: wasmtime::component::ComponentExportIndex, 316 f3: wasmtime::component::ComponentExportIndex, 317 f4: wasmtime::component::ComponentExportIndex, 318 f5: wasmtime::component::ComponentExportIndex, 319 f6: wasmtime::component::ComponentExportIndex, 320 } 321 impl GuestIndices { 322 /// Constructor for [`GuestIndices`] which takes a 323 /// [`Component`](wasmtime::component::Component) as input and can be executed 324 /// before instantiation. 325 /// 326 /// This constructor can be used to front-load string lookups to find exports 327 /// within a component. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<GuestIndices>328 pub fn new<_T>( 329 _instance_pre: &wasmtime::component::InstancePre<_T>, 330 ) -> wasmtime::Result<GuestIndices> { 331 let instance = _instance_pre 332 .component() 333 .get_export_index(None, "foo:foo/simple") 334 .ok_or_else(|| { 335 wasmtime::format_err!( 336 "no exported instance named `foo:foo/simple`" 337 ) 338 })?; 339 let mut lookup = move |name| { 340 _instance_pre 341 .component() 342 .get_export_index(Some(&instance), name) 343 .ok_or_else(|| { 344 wasmtime::format_err!( 345 "instance export `foo:foo/simple` does \ 346 not have export `{name}`" 347 ) 348 }) 349 }; 350 let _ = &mut lookup; 351 let f1 = lookup("f1")?; 352 let f2 = lookup("f2")?; 353 let f3 = lookup("f3")?; 354 let f4 = lookup("f4")?; 355 let f5 = lookup("f5")?; 356 let f6 = lookup("f6")?; 357 Ok(GuestIndices { 358 f1, 359 f2, 360 f3, 361 f4, 362 f5, 363 f6, 364 }) 365 } load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Guest>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 f1 = *_instance 377 .get_typed_func::<(), ()>(&mut store, &self.f1)? 378 .func(); 379 let f2 = *_instance 380 .get_typed_func::<(u32,), ()>(&mut store, &self.f2)? 381 .func(); 382 let f3 = *_instance 383 .get_typed_func::<(u32, u32), ()>(&mut store, &self.f3)? 384 .func(); 385 let f4 = *_instance 386 .get_typed_func::<(), (u32,)>(&mut store, &self.f4)? 387 .func(); 388 let f5 = *_instance 389 .get_typed_func::<(), ((u32, u32),)>(&mut store, &self.f5)? 390 .func(); 391 let f6 = *_instance 392 .get_typed_func::< 393 (u32, u32, u32), 394 ((u32, u32, u32),), 395 >(&mut store, &self.f6)? 396 .func(); 397 Ok(Guest { f1, f2, f3, f4, f5, f6 }) 398 } 399 } 400 impl Guest { call_f1<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<()>401 pub fn call_f1<S: wasmtime::AsContextMut>( 402 &self, 403 mut store: S, 404 ) -> wasmtime::Result<()> { 405 let callee = unsafe { 406 wasmtime::component::TypedFunc::< 407 (), 408 (), 409 >::new_unchecked(self.f1) 410 }; 411 let () = callee.call(store.as_context_mut(), ())?; 412 Ok(()) 413 } call_f2<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: u32, ) -> wasmtime::Result<()>414 pub fn call_f2<S: wasmtime::AsContextMut>( 415 &self, 416 mut store: S, 417 arg0: u32, 418 ) -> wasmtime::Result<()> { 419 let callee = unsafe { 420 wasmtime::component::TypedFunc::< 421 (u32,), 422 (), 423 >::new_unchecked(self.f2) 424 }; 425 let () = callee.call(store.as_context_mut(), (arg0,))?; 426 Ok(()) 427 } call_f3<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: u32, arg1: u32, ) -> wasmtime::Result<()>428 pub fn call_f3<S: wasmtime::AsContextMut>( 429 &self, 430 mut store: S, 431 arg0: u32, 432 arg1: u32, 433 ) -> wasmtime::Result<()> { 434 let callee = unsafe { 435 wasmtime::component::TypedFunc::< 436 (u32, u32), 437 (), 438 >::new_unchecked(self.f3) 439 }; 440 let () = callee.call(store.as_context_mut(), (arg0, arg1))?; 441 Ok(()) 442 } call_f4<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<u32>443 pub fn call_f4<S: wasmtime::AsContextMut>( 444 &self, 445 mut store: S, 446 ) -> wasmtime::Result<u32> { 447 let callee = unsafe { 448 wasmtime::component::TypedFunc::< 449 (), 450 (u32,), 451 >::new_unchecked(self.f4) 452 }; 453 let (ret0,) = callee.call(store.as_context_mut(), ())?; 454 Ok(ret0) 455 } call_f5<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<(u32, u32)>456 pub fn call_f5<S: wasmtime::AsContextMut>( 457 &self, 458 mut store: S, 459 ) -> wasmtime::Result<(u32, u32)> { 460 let callee = unsafe { 461 wasmtime::component::TypedFunc::< 462 (), 463 ((u32, u32),), 464 >::new_unchecked(self.f5) 465 }; 466 let (ret0,) = callee.call(store.as_context_mut(), ())?; 467 Ok(ret0) 468 } call_f6<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: u32, arg1: u32, arg2: u32, ) -> wasmtime::Result<(u32, u32, u32)>469 pub fn call_f6<S: wasmtime::AsContextMut>( 470 &self, 471 mut store: S, 472 arg0: u32, 473 arg1: u32, 474 arg2: u32, 475 ) -> wasmtime::Result<(u32, u32, u32)> { 476 let callee = unsafe { 477 wasmtime::component::TypedFunc::< 478 (u32, u32, u32), 479 ((u32, u32, u32),), 480 >::new_unchecked(self.f6) 481 }; 482 let (ret0,) = callee 483 .call(store.as_context_mut(), (arg0, arg1, arg2))?; 484 Ok(ret0) 485 } 486 } 487 } 488 } 489 } 490 } 491