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::records::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::records::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::records::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::records::HostWithStore, for<'a> D::Data<'a>: foo::foo::records::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::records::HostWithStore, 177 for<'a> D::Data<'a>: foo::foo::records::Host, 178 T: 'static, 179 { 180 foo::foo::records::add_to_linker::<T, D>(linker, host_getter)?; 181 Ok(()) 182 } foo_foo_records(&self) -> &exports::foo::foo::records::Guest183 pub fn foo_foo_records(&self) -> &exports::foo::foo::records::Guest { 184 &self.interface0 185 } 186 } 187 }; 188 pub mod foo { 189 pub mod foo { 190 #[allow(clippy::all)] 191 pub mod records { 192 #[allow(unused_imports)] 193 use wasmtime::component::__internal::Box; 194 #[derive(wasmtime::component::ComponentType)] 195 #[derive(wasmtime::component::Lift)] 196 #[derive(wasmtime::component::Lower)] 197 #[component(record)] 198 #[derive(Clone, Copy)] 199 pub struct Empty {} 200 impl core::fmt::Debug for Empty { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result201 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 202 f.debug_struct("Empty").finish() 203 } 204 } 205 const _: () = { 206 assert!(0 == < Empty as wasmtime::component::ComponentType >::SIZE32); 207 assert!(1 == < Empty as wasmtime::component::ComponentType >::ALIGN32); 208 }; 209 /// A record containing two scalar fields 210 /// that both have the same type 211 #[derive(wasmtime::component::ComponentType)] 212 #[derive(wasmtime::component::Lift)] 213 #[derive(wasmtime::component::Lower)] 214 #[component(record)] 215 #[derive(Clone, Copy)] 216 pub struct Scalars { 217 /// The first field, named a 218 #[component(name = "a")] 219 pub a: u32, 220 /// The second field, named b 221 #[component(name = "b")] 222 pub b: u32, 223 } 224 impl core::fmt::Debug for Scalars { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result225 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 226 f.debug_struct("Scalars") 227 .field("a", &self.a) 228 .field("b", &self.b) 229 .finish() 230 } 231 } 232 const _: () = { 233 assert!(8 == < Scalars as wasmtime::component::ComponentType >::SIZE32); 234 assert!(4 == < Scalars as wasmtime::component::ComponentType >::ALIGN32); 235 }; 236 /// A record that is really just flags 237 /// All of the fields are bool 238 #[derive(wasmtime::component::ComponentType)] 239 #[derive(wasmtime::component::Lift)] 240 #[derive(wasmtime::component::Lower)] 241 #[component(record)] 242 #[derive(Clone, Copy)] 243 pub struct ReallyFlags { 244 #[component(name = "a")] 245 pub a: bool, 246 #[component(name = "b")] 247 pub b: bool, 248 #[component(name = "c")] 249 pub c: bool, 250 #[component(name = "d")] 251 pub d: bool, 252 #[component(name = "e")] 253 pub e: bool, 254 #[component(name = "f")] 255 pub f: bool, 256 #[component(name = "g")] 257 pub g: bool, 258 #[component(name = "h")] 259 pub h: bool, 260 #[component(name = "i")] 261 pub i: bool, 262 } 263 impl core::fmt::Debug for ReallyFlags { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result264 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 265 f.debug_struct("ReallyFlags") 266 .field("a", &self.a) 267 .field("b", &self.b) 268 .field("c", &self.c) 269 .field("d", &self.d) 270 .field("e", &self.e) 271 .field("f", &self.f) 272 .field("g", &self.g) 273 .field("h", &self.h) 274 .field("i", &self.i) 275 .finish() 276 } 277 } 278 const _: () = { 279 assert!( 280 9 == < ReallyFlags as wasmtime::component::ComponentType >::SIZE32 281 ); 282 assert!( 283 1 == < ReallyFlags as wasmtime::component::ComponentType >::ALIGN32 284 ); 285 }; 286 #[derive(wasmtime::component::ComponentType)] 287 #[derive(wasmtime::component::Lift)] 288 #[derive(wasmtime::component::Lower)] 289 #[component(record)] 290 #[derive(Clone)] 291 pub struct Aggregates { 292 #[component(name = "a")] 293 pub a: Scalars, 294 #[component(name = "b")] 295 pub b: u32, 296 #[component(name = "c")] 297 pub c: Empty, 298 #[component(name = "d")] 299 pub d: wasmtime::component::__internal::String, 300 #[component(name = "e")] 301 pub e: ReallyFlags, 302 } 303 impl core::fmt::Debug for Aggregates { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result304 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 305 f.debug_struct("Aggregates") 306 .field("a", &self.a) 307 .field("b", &self.b) 308 .field("c", &self.c) 309 .field("d", &self.d) 310 .field("e", &self.e) 311 .finish() 312 } 313 } 314 const _: () = { 315 assert!( 316 32 == < Aggregates as wasmtime::component::ComponentType >::SIZE32 317 ); 318 assert!( 319 4 == < Aggregates as wasmtime::component::ComponentType >::ALIGN32 320 ); 321 }; 322 pub type TupleTypedef = (i32,); 323 const _: () = { 324 assert!( 325 4 == < TupleTypedef as wasmtime::component::ComponentType >::SIZE32 326 ); 327 assert!( 328 4 == < TupleTypedef as wasmtime::component::ComponentType >::ALIGN32 329 ); 330 }; 331 pub type IntTypedef = i32; 332 const _: () = { 333 assert!( 334 4 == < IntTypedef as wasmtime::component::ComponentType >::SIZE32 335 ); 336 assert!( 337 4 == < IntTypedef as wasmtime::component::ComponentType >::ALIGN32 338 ); 339 }; 340 pub type TupleTypedef2 = (IntTypedef,); 341 const _: () = { 342 assert!( 343 4 == < TupleTypedef2 as wasmtime::component::ComponentType >::SIZE32 344 ); 345 assert!( 346 4 == < TupleTypedef2 as wasmtime::component::ComponentType >::ALIGN32 347 ); 348 }; 349 #[derive(wasmtime::component::ComponentType)] 350 #[derive(wasmtime::component::Lift)] 351 #[derive(wasmtime::component::Lower)] 352 #[component(record)] 353 pub struct FuturesAndStreams { 354 #[component(name = "a")] 355 pub a: wasmtime::component::FutureReader<u8>, 356 #[component(name = "b")] 357 pub b: wasmtime::component::StreamReader<u8>, 358 #[component(name = "c")] 359 pub c: wasmtime::component::StreamReader< 360 wasmtime::component::FutureReader< 361 wasmtime::component::StreamReader<()>, 362 >, 363 >, 364 #[component(name = "d")] 365 pub d: wasmtime::component::FutureReader< 366 wasmtime::component::StreamReader< 367 wasmtime::component::FutureReader<()>, 368 >, 369 >, 370 } 371 impl core::fmt::Debug for FuturesAndStreams { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result372 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 373 f.debug_struct("FuturesAndStreams") 374 .field("a", &self.a) 375 .field("b", &self.b) 376 .field("c", &self.c) 377 .field("d", &self.d) 378 .finish() 379 } 380 } 381 const _: () = { 382 assert!( 383 16 == < FuturesAndStreams as wasmtime::component::ComponentType 384 >::SIZE32 385 ); 386 assert!( 387 4 == < FuturesAndStreams as wasmtime::component::ComponentType 388 >::ALIGN32 389 ); 390 }; 391 pub trait HostWithStore: wasmtime::component::HasData {} 392 impl<_T: ?Sized> HostWithStore for _T 393 where 394 _T: wasmtime::component::HasData, 395 {} 396 pub trait Host { tuple_arg(&mut self, x: (char, u32)) -> ()397 fn tuple_arg(&mut self, x: (char, u32)) -> (); tuple_result(&mut self) -> (char, u32)398 fn tuple_result(&mut self) -> (char, u32); empty_arg(&mut self, x: Empty) -> ()399 fn empty_arg(&mut self, x: Empty) -> (); empty_result(&mut self) -> Empty400 fn empty_result(&mut self) -> Empty; scalar_arg(&mut self, x: Scalars) -> ()401 fn scalar_arg(&mut self, x: Scalars) -> (); scalar_result(&mut self) -> Scalars402 fn scalar_result(&mut self) -> Scalars; flags_arg(&mut self, x: ReallyFlags) -> ()403 fn flags_arg(&mut self, x: ReallyFlags) -> (); flags_result(&mut self) -> ReallyFlags404 fn flags_result(&mut self) -> ReallyFlags; aggregate_arg(&mut self, x: Aggregates) -> ()405 fn aggregate_arg(&mut self, x: Aggregates) -> (); aggregate_result(&mut self) -> Aggregates406 fn aggregate_result(&mut self) -> Aggregates; typedef_inout(&mut self, e: TupleTypedef2) -> i32407 fn typedef_inout(&mut self, e: TupleTypedef2) -> i32; 408 } 409 impl<_T: Host + ?Sized> Host for &mut _T { tuple_arg(&mut self, x: (char, u32)) -> ()410 fn tuple_arg(&mut self, x: (char, u32)) -> () { 411 Host::tuple_arg(*self, x) 412 } tuple_result(&mut self) -> (char, u32)413 fn tuple_result(&mut self) -> (char, u32) { 414 Host::tuple_result(*self) 415 } empty_arg(&mut self, x: Empty) -> ()416 fn empty_arg(&mut self, x: Empty) -> () { 417 Host::empty_arg(*self, x) 418 } empty_result(&mut self) -> Empty419 fn empty_result(&mut self) -> Empty { 420 Host::empty_result(*self) 421 } scalar_arg(&mut self, x: Scalars) -> ()422 fn scalar_arg(&mut self, x: Scalars) -> () { 423 Host::scalar_arg(*self, x) 424 } scalar_result(&mut self) -> Scalars425 fn scalar_result(&mut self) -> Scalars { 426 Host::scalar_result(*self) 427 } flags_arg(&mut self, x: ReallyFlags) -> ()428 fn flags_arg(&mut self, x: ReallyFlags) -> () { 429 Host::flags_arg(*self, x) 430 } flags_result(&mut self) -> ReallyFlags431 fn flags_result(&mut self) -> ReallyFlags { 432 Host::flags_result(*self) 433 } aggregate_arg(&mut self, x: Aggregates) -> ()434 fn aggregate_arg(&mut self, x: Aggregates) -> () { 435 Host::aggregate_arg(*self, x) 436 } aggregate_result(&mut self) -> Aggregates437 fn aggregate_result(&mut self) -> Aggregates { 438 Host::aggregate_result(*self) 439 } typedef_inout(&mut self, e: TupleTypedef2) -> i32440 fn typedef_inout(&mut self, e: TupleTypedef2) -> i32 { 441 Host::typedef_inout(*self, e) 442 } 443 } 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,444 pub fn add_to_linker<T, D>( 445 linker: &mut wasmtime::component::Linker<T>, 446 host_getter: fn(&mut T) -> D::Data<'_>, 447 ) -> wasmtime::Result<()> 448 where 449 D: HostWithStore, 450 for<'a> D::Data<'a>: Host, 451 T: 'static, 452 { 453 let mut inst = linker.instance("foo:foo/records")?; 454 inst.func_wrap( 455 "tuple-arg", 456 move | 457 mut caller: wasmtime::StoreContextMut<'_, T>, 458 (arg0,): ((char, u32),)| 459 { 460 let host = &mut host_getter(caller.data_mut()); 461 let r = Host::tuple_arg(host, arg0); 462 Ok(r) 463 }, 464 )?; 465 inst.func_wrap( 466 "tuple-result", 467 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 468 let host = &mut host_getter(caller.data_mut()); 469 let r = Host::tuple_result(host); 470 Ok((r,)) 471 }, 472 )?; 473 inst.func_wrap( 474 "empty-arg", 475 move | 476 mut caller: wasmtime::StoreContextMut<'_, T>, 477 (arg0,): (Empty,)| 478 { 479 let host = &mut host_getter(caller.data_mut()); 480 let r = Host::empty_arg(host, arg0); 481 Ok(r) 482 }, 483 )?; 484 inst.func_wrap( 485 "empty-result", 486 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 487 let host = &mut host_getter(caller.data_mut()); 488 let r = Host::empty_result(host); 489 Ok((r,)) 490 }, 491 )?; 492 inst.func_wrap( 493 "scalar-arg", 494 move | 495 mut caller: wasmtime::StoreContextMut<'_, T>, 496 (arg0,): (Scalars,)| 497 { 498 let host = &mut host_getter(caller.data_mut()); 499 let r = Host::scalar_arg(host, arg0); 500 Ok(r) 501 }, 502 )?; 503 inst.func_wrap( 504 "scalar-result", 505 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 506 let host = &mut host_getter(caller.data_mut()); 507 let r = Host::scalar_result(host); 508 Ok((r,)) 509 }, 510 )?; 511 inst.func_wrap( 512 "flags-arg", 513 move | 514 mut caller: wasmtime::StoreContextMut<'_, T>, 515 (arg0,): (ReallyFlags,)| 516 { 517 let host = &mut host_getter(caller.data_mut()); 518 let r = Host::flags_arg(host, arg0); 519 Ok(r) 520 }, 521 )?; 522 inst.func_wrap( 523 "flags-result", 524 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 525 let host = &mut host_getter(caller.data_mut()); 526 let r = Host::flags_result(host); 527 Ok((r,)) 528 }, 529 )?; 530 inst.func_wrap( 531 "aggregate-arg", 532 move | 533 mut caller: wasmtime::StoreContextMut<'_, T>, 534 (arg0,): (Aggregates,)| 535 { 536 let host = &mut host_getter(caller.data_mut()); 537 let r = Host::aggregate_arg(host, arg0); 538 Ok(r) 539 }, 540 )?; 541 inst.func_wrap( 542 "aggregate-result", 543 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 544 let host = &mut host_getter(caller.data_mut()); 545 let r = Host::aggregate_result(host); 546 Ok((r,)) 547 }, 548 )?; 549 inst.func_wrap( 550 "typedef-inout", 551 move | 552 mut caller: wasmtime::StoreContextMut<'_, T>, 553 (arg0,): (TupleTypedef2,)| 554 { 555 let host = &mut host_getter(caller.data_mut()); 556 let r = Host::typedef_inout(host, arg0); 557 Ok((r,)) 558 }, 559 )?; 560 Ok(()) 561 } 562 } 563 } 564 } 565 pub mod exports { 566 pub mod foo { 567 pub mod foo { 568 #[allow(clippy::all)] 569 pub mod records { 570 #[allow(unused_imports)] 571 use wasmtime::component::__internal::Box; 572 #[derive(wasmtime::component::ComponentType)] 573 #[derive(wasmtime::component::Lift)] 574 #[derive(wasmtime::component::Lower)] 575 #[component(record)] 576 #[derive(Clone, Copy)] 577 pub struct Empty {} 578 impl core::fmt::Debug for Empty { fmt( &self, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result579 fn fmt( 580 &self, 581 f: &mut core::fmt::Formatter<'_>, 582 ) -> core::fmt::Result { 583 f.debug_struct("Empty").finish() 584 } 585 } 586 const _: () = { 587 assert!( 588 0 == < Empty as wasmtime::component::ComponentType >::SIZE32 589 ); 590 assert!( 591 1 == < Empty as wasmtime::component::ComponentType >::ALIGN32 592 ); 593 }; 594 /// A record containing two scalar fields 595 /// that both have the same type 596 #[derive(wasmtime::component::ComponentType)] 597 #[derive(wasmtime::component::Lift)] 598 #[derive(wasmtime::component::Lower)] 599 #[component(record)] 600 #[derive(Clone, Copy)] 601 pub struct Scalars { 602 /// The first field, named a 603 #[component(name = "a")] 604 pub a: u32, 605 /// The second field, named b 606 #[component(name = "b")] 607 pub b: u32, 608 } 609 impl core::fmt::Debug for Scalars { fmt( &self, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result610 fn fmt( 611 &self, 612 f: &mut core::fmt::Formatter<'_>, 613 ) -> core::fmt::Result { 614 f.debug_struct("Scalars") 615 .field("a", &self.a) 616 .field("b", &self.b) 617 .finish() 618 } 619 } 620 const _: () = { 621 assert!( 622 8 == < Scalars as wasmtime::component::ComponentType >::SIZE32 623 ); 624 assert!( 625 4 == < Scalars as wasmtime::component::ComponentType >::ALIGN32 626 ); 627 }; 628 /// A record that is really just flags 629 /// All of the fields are bool 630 #[derive(wasmtime::component::ComponentType)] 631 #[derive(wasmtime::component::Lift)] 632 #[derive(wasmtime::component::Lower)] 633 #[component(record)] 634 #[derive(Clone, Copy)] 635 pub struct ReallyFlags { 636 #[component(name = "a")] 637 pub a: bool, 638 #[component(name = "b")] 639 pub b: bool, 640 #[component(name = "c")] 641 pub c: bool, 642 #[component(name = "d")] 643 pub d: bool, 644 #[component(name = "e")] 645 pub e: bool, 646 #[component(name = "f")] 647 pub f: bool, 648 #[component(name = "g")] 649 pub g: bool, 650 #[component(name = "h")] 651 pub h: bool, 652 #[component(name = "i")] 653 pub i: bool, 654 } 655 impl core::fmt::Debug for ReallyFlags { fmt( &self, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result656 fn fmt( 657 &self, 658 f: &mut core::fmt::Formatter<'_>, 659 ) -> core::fmt::Result { 660 f.debug_struct("ReallyFlags") 661 .field("a", &self.a) 662 .field("b", &self.b) 663 .field("c", &self.c) 664 .field("d", &self.d) 665 .field("e", &self.e) 666 .field("f", &self.f) 667 .field("g", &self.g) 668 .field("h", &self.h) 669 .field("i", &self.i) 670 .finish() 671 } 672 } 673 const _: () = { 674 assert!( 675 9 == < ReallyFlags as wasmtime::component::ComponentType 676 >::SIZE32 677 ); 678 assert!( 679 1 == < ReallyFlags as wasmtime::component::ComponentType 680 >::ALIGN32 681 ); 682 }; 683 #[derive(wasmtime::component::ComponentType)] 684 #[derive(wasmtime::component::Lift)] 685 #[derive(wasmtime::component::Lower)] 686 #[component(record)] 687 #[derive(Clone)] 688 pub struct Aggregates { 689 #[component(name = "a")] 690 pub a: Scalars, 691 #[component(name = "b")] 692 pub b: u32, 693 #[component(name = "c")] 694 pub c: Empty, 695 #[component(name = "d")] 696 pub d: wasmtime::component::__internal::String, 697 #[component(name = "e")] 698 pub e: ReallyFlags, 699 } 700 impl core::fmt::Debug for Aggregates { fmt( &self, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result701 fn fmt( 702 &self, 703 f: &mut core::fmt::Formatter<'_>, 704 ) -> core::fmt::Result { 705 f.debug_struct("Aggregates") 706 .field("a", &self.a) 707 .field("b", &self.b) 708 .field("c", &self.c) 709 .field("d", &self.d) 710 .field("e", &self.e) 711 .finish() 712 } 713 } 714 const _: () = { 715 assert!( 716 32 == < Aggregates as wasmtime::component::ComponentType 717 >::SIZE32 718 ); 719 assert!( 720 4 == < Aggregates as wasmtime::component::ComponentType 721 >::ALIGN32 722 ); 723 }; 724 pub type TupleTypedef = (i32,); 725 const _: () = { 726 assert!( 727 4 == < TupleTypedef as wasmtime::component::ComponentType 728 >::SIZE32 729 ); 730 assert!( 731 4 == < TupleTypedef as wasmtime::component::ComponentType 732 >::ALIGN32 733 ); 734 }; 735 pub type IntTypedef = i32; 736 const _: () = { 737 assert!( 738 4 == < IntTypedef as wasmtime::component::ComponentType >::SIZE32 739 ); 740 assert!( 741 4 == < IntTypedef as wasmtime::component::ComponentType 742 >::ALIGN32 743 ); 744 }; 745 pub type TupleTypedef2 = (IntTypedef,); 746 const _: () = { 747 assert!( 748 4 == < TupleTypedef2 as wasmtime::component::ComponentType 749 >::SIZE32 750 ); 751 assert!( 752 4 == < TupleTypedef2 as wasmtime::component::ComponentType 753 >::ALIGN32 754 ); 755 }; 756 #[derive(wasmtime::component::ComponentType)] 757 #[derive(wasmtime::component::Lift)] 758 #[derive(wasmtime::component::Lower)] 759 #[component(record)] 760 pub struct FuturesAndStreams { 761 #[component(name = "a")] 762 pub a: wasmtime::component::FutureReader<u8>, 763 #[component(name = "b")] 764 pub b: wasmtime::component::StreamReader<u8>, 765 #[component(name = "c")] 766 pub c: wasmtime::component::StreamReader< 767 wasmtime::component::FutureReader< 768 wasmtime::component::StreamReader<()>, 769 >, 770 >, 771 #[component(name = "d")] 772 pub d: wasmtime::component::FutureReader< 773 wasmtime::component::StreamReader< 774 wasmtime::component::FutureReader<()>, 775 >, 776 >, 777 } 778 impl core::fmt::Debug for FuturesAndStreams { fmt( &self, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result779 fn fmt( 780 &self, 781 f: &mut core::fmt::Formatter<'_>, 782 ) -> core::fmt::Result { 783 f.debug_struct("FuturesAndStreams") 784 .field("a", &self.a) 785 .field("b", &self.b) 786 .field("c", &self.c) 787 .field("d", &self.d) 788 .finish() 789 } 790 } 791 const _: () = { 792 assert!( 793 16 == < FuturesAndStreams as wasmtime::component::ComponentType 794 >::SIZE32 795 ); 796 assert!( 797 4 == < FuturesAndStreams as wasmtime::component::ComponentType 798 >::ALIGN32 799 ); 800 }; 801 #[derive(Clone)] 802 pub struct Guest { 803 tuple_arg: wasmtime::component::Func, 804 tuple_result: wasmtime::component::Func, 805 empty_arg: wasmtime::component::Func, 806 empty_result: wasmtime::component::Func, 807 scalar_arg: wasmtime::component::Func, 808 scalar_result: wasmtime::component::Func, 809 flags_arg: wasmtime::component::Func, 810 flags_result: wasmtime::component::Func, 811 aggregate_arg: wasmtime::component::Func, 812 aggregate_result: wasmtime::component::Func, 813 typedef_inout: wasmtime::component::Func, 814 } 815 #[derive(Clone)] 816 pub struct GuestIndices { 817 tuple_arg: wasmtime::component::ComponentExportIndex, 818 tuple_result: wasmtime::component::ComponentExportIndex, 819 empty_arg: wasmtime::component::ComponentExportIndex, 820 empty_result: wasmtime::component::ComponentExportIndex, 821 scalar_arg: wasmtime::component::ComponentExportIndex, 822 scalar_result: wasmtime::component::ComponentExportIndex, 823 flags_arg: wasmtime::component::ComponentExportIndex, 824 flags_result: wasmtime::component::ComponentExportIndex, 825 aggregate_arg: wasmtime::component::ComponentExportIndex, 826 aggregate_result: wasmtime::component::ComponentExportIndex, 827 typedef_inout: wasmtime::component::ComponentExportIndex, 828 } 829 impl GuestIndices { 830 /// Constructor for [`GuestIndices`] which takes a 831 /// [`Component`](wasmtime::component::Component) as input and can be executed 832 /// before instantiation. 833 /// 834 /// This constructor can be used to front-load string lookups to find exports 835 /// within a component. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<GuestIndices>836 pub fn new<_T>( 837 _instance_pre: &wasmtime::component::InstancePre<_T>, 838 ) -> wasmtime::Result<GuestIndices> { 839 let instance = _instance_pre 840 .component() 841 .get_export_index(None, "foo:foo/records") 842 .ok_or_else(|| { 843 wasmtime::format_err!( 844 "no exported instance named `foo:foo/records`" 845 ) 846 })?; 847 let mut lookup = move |name| { 848 _instance_pre 849 .component() 850 .get_export_index(Some(&instance), name) 851 .ok_or_else(|| { 852 wasmtime::format_err!( 853 "instance export `foo:foo/records` does \ 854 not have export `{name}`" 855 ) 856 }) 857 }; 858 let _ = &mut lookup; 859 let tuple_arg = lookup("tuple-arg")?; 860 let tuple_result = lookup("tuple-result")?; 861 let empty_arg = lookup("empty-arg")?; 862 let empty_result = lookup("empty-result")?; 863 let scalar_arg = lookup("scalar-arg")?; 864 let scalar_result = lookup("scalar-result")?; 865 let flags_arg = lookup("flags-arg")?; 866 let flags_result = lookup("flags-result")?; 867 let aggregate_arg = lookup("aggregate-arg")?; 868 let aggregate_result = lookup("aggregate-result")?; 869 let typedef_inout = lookup("typedef-inout")?; 870 Ok(GuestIndices { 871 tuple_arg, 872 tuple_result, 873 empty_arg, 874 empty_result, 875 scalar_arg, 876 scalar_result, 877 flags_arg, 878 flags_result, 879 aggregate_arg, 880 aggregate_result, 881 typedef_inout, 882 }) 883 } load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Guest>884 pub fn load( 885 &self, 886 mut store: impl wasmtime::AsContextMut, 887 instance: &wasmtime::component::Instance, 888 ) -> wasmtime::Result<Guest> { 889 let _instance = instance; 890 let _instance_pre = _instance.instance_pre(&store); 891 let _instance_type = _instance_pre.instance_type(); 892 let mut store = store.as_context_mut(); 893 let _ = &mut store; 894 let tuple_arg = *_instance 895 .get_typed_func::< 896 ((char, u32),), 897 (), 898 >(&mut store, &self.tuple_arg)? 899 .func(); 900 let tuple_result = *_instance 901 .get_typed_func::< 902 (), 903 ((char, u32),), 904 >(&mut store, &self.tuple_result)? 905 .func(); 906 let empty_arg = *_instance 907 .get_typed_func::<(Empty,), ()>(&mut store, &self.empty_arg)? 908 .func(); 909 let empty_result = *_instance 910 .get_typed_func::< 911 (), 912 (Empty,), 913 >(&mut store, &self.empty_result)? 914 .func(); 915 let scalar_arg = *_instance 916 .get_typed_func::< 917 (Scalars,), 918 (), 919 >(&mut store, &self.scalar_arg)? 920 .func(); 921 let scalar_result = *_instance 922 .get_typed_func::< 923 (), 924 (Scalars,), 925 >(&mut store, &self.scalar_result)? 926 .func(); 927 let flags_arg = *_instance 928 .get_typed_func::< 929 (ReallyFlags,), 930 (), 931 >(&mut store, &self.flags_arg)? 932 .func(); 933 let flags_result = *_instance 934 .get_typed_func::< 935 (), 936 (ReallyFlags,), 937 >(&mut store, &self.flags_result)? 938 .func(); 939 let aggregate_arg = *_instance 940 .get_typed_func::< 941 (&Aggregates,), 942 (), 943 >(&mut store, &self.aggregate_arg)? 944 .func(); 945 let aggregate_result = *_instance 946 .get_typed_func::< 947 (), 948 (Aggregates,), 949 >(&mut store, &self.aggregate_result)? 950 .func(); 951 let typedef_inout = *_instance 952 .get_typed_func::< 953 (TupleTypedef2,), 954 (i32,), 955 >(&mut store, &self.typedef_inout)? 956 .func(); 957 Ok(Guest { 958 tuple_arg, 959 tuple_result, 960 empty_arg, 961 empty_result, 962 scalar_arg, 963 scalar_result, 964 flags_arg, 965 flags_result, 966 aggregate_arg, 967 aggregate_result, 968 typedef_inout, 969 }) 970 } 971 } 972 impl Guest { call_tuple_arg<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: (char, u32), ) -> wasmtime::Result<()>973 pub fn call_tuple_arg<S: wasmtime::AsContextMut>( 974 &self, 975 mut store: S, 976 arg0: (char, u32), 977 ) -> wasmtime::Result<()> { 978 let callee = unsafe { 979 wasmtime::component::TypedFunc::< 980 ((char, u32),), 981 (), 982 >::new_unchecked(self.tuple_arg) 983 }; 984 let () = callee.call(store.as_context_mut(), (arg0,))?; 985 Ok(()) 986 } call_tuple_result<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<(char, u32)>987 pub fn call_tuple_result<S: wasmtime::AsContextMut>( 988 &self, 989 mut store: S, 990 ) -> wasmtime::Result<(char, u32)> { 991 let callee = unsafe { 992 wasmtime::component::TypedFunc::< 993 (), 994 ((char, u32),), 995 >::new_unchecked(self.tuple_result) 996 }; 997 let (ret0,) = callee.call(store.as_context_mut(), ())?; 998 Ok(ret0) 999 } call_empty_arg<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: Empty, ) -> wasmtime::Result<()>1000 pub fn call_empty_arg<S: wasmtime::AsContextMut>( 1001 &self, 1002 mut store: S, 1003 arg0: Empty, 1004 ) -> wasmtime::Result<()> { 1005 let callee = unsafe { 1006 wasmtime::component::TypedFunc::< 1007 (Empty,), 1008 (), 1009 >::new_unchecked(self.empty_arg) 1010 }; 1011 let () = callee.call(store.as_context_mut(), (arg0,))?; 1012 Ok(()) 1013 } call_empty_result<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<Empty>1014 pub fn call_empty_result<S: wasmtime::AsContextMut>( 1015 &self, 1016 mut store: S, 1017 ) -> wasmtime::Result<Empty> { 1018 let callee = unsafe { 1019 wasmtime::component::TypedFunc::< 1020 (), 1021 (Empty,), 1022 >::new_unchecked(self.empty_result) 1023 }; 1024 let (ret0,) = callee.call(store.as_context_mut(), ())?; 1025 Ok(ret0) 1026 } call_scalar_arg<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: Scalars, ) -> wasmtime::Result<()>1027 pub fn call_scalar_arg<S: wasmtime::AsContextMut>( 1028 &self, 1029 mut store: S, 1030 arg0: Scalars, 1031 ) -> wasmtime::Result<()> { 1032 let callee = unsafe { 1033 wasmtime::component::TypedFunc::< 1034 (Scalars,), 1035 (), 1036 >::new_unchecked(self.scalar_arg) 1037 }; 1038 let () = callee.call(store.as_context_mut(), (arg0,))?; 1039 Ok(()) 1040 } call_scalar_result<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<Scalars>1041 pub fn call_scalar_result<S: wasmtime::AsContextMut>( 1042 &self, 1043 mut store: S, 1044 ) -> wasmtime::Result<Scalars> { 1045 let callee = unsafe { 1046 wasmtime::component::TypedFunc::< 1047 (), 1048 (Scalars,), 1049 >::new_unchecked(self.scalar_result) 1050 }; 1051 let (ret0,) = callee.call(store.as_context_mut(), ())?; 1052 Ok(ret0) 1053 } call_flags_arg<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: ReallyFlags, ) -> wasmtime::Result<()>1054 pub fn call_flags_arg<S: wasmtime::AsContextMut>( 1055 &self, 1056 mut store: S, 1057 arg0: ReallyFlags, 1058 ) -> wasmtime::Result<()> { 1059 let callee = unsafe { 1060 wasmtime::component::TypedFunc::< 1061 (ReallyFlags,), 1062 (), 1063 >::new_unchecked(self.flags_arg) 1064 }; 1065 let () = callee.call(store.as_context_mut(), (arg0,))?; 1066 Ok(()) 1067 } call_flags_result<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<ReallyFlags>1068 pub fn call_flags_result<S: wasmtime::AsContextMut>( 1069 &self, 1070 mut store: S, 1071 ) -> wasmtime::Result<ReallyFlags> { 1072 let callee = unsafe { 1073 wasmtime::component::TypedFunc::< 1074 (), 1075 (ReallyFlags,), 1076 >::new_unchecked(self.flags_result) 1077 }; 1078 let (ret0,) = callee.call(store.as_context_mut(), ())?; 1079 Ok(ret0) 1080 } call_aggregate_arg<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: &Aggregates, ) -> wasmtime::Result<()>1081 pub fn call_aggregate_arg<S: wasmtime::AsContextMut>( 1082 &self, 1083 mut store: S, 1084 arg0: &Aggregates, 1085 ) -> wasmtime::Result<()> { 1086 let callee = unsafe { 1087 wasmtime::component::TypedFunc::< 1088 (&Aggregates,), 1089 (), 1090 >::new_unchecked(self.aggregate_arg) 1091 }; 1092 let () = callee.call(store.as_context_mut(), (arg0,))?; 1093 Ok(()) 1094 } call_aggregate_result<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<Aggregates>1095 pub fn call_aggregate_result<S: wasmtime::AsContextMut>( 1096 &self, 1097 mut store: S, 1098 ) -> wasmtime::Result<Aggregates> { 1099 let callee = unsafe { 1100 wasmtime::component::TypedFunc::< 1101 (), 1102 (Aggregates,), 1103 >::new_unchecked(self.aggregate_result) 1104 }; 1105 let (ret0,) = callee.call(store.as_context_mut(), ())?; 1106 Ok(ret0) 1107 } call_typedef_inout<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: TupleTypedef2, ) -> wasmtime::Result<i32>1108 pub fn call_typedef_inout<S: wasmtime::AsContextMut>( 1109 &self, 1110 mut store: S, 1111 arg0: TupleTypedef2, 1112 ) -> wasmtime::Result<i32> { 1113 let callee = unsafe { 1114 wasmtime::component::TypedFunc::< 1115 (TupleTypedef2,), 1116 (i32,), 1117 >::new_unchecked(self.typedef_inout) 1118 }; 1119 let (ret0,) = callee.call(store.as_context_mut(), (arg0,))?; 1120 Ok(ret0) 1121 } 1122 } 1123 } 1124 } 1125 } 1126 } 1127