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> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: TheWorldIndices, 12 } 13 impl<T> 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> 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.component())?; 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 async fn instantiate_async( 47 &self, 48 mut store: impl wasmtime::AsContextMut<Data = _T>, 49 ) -> wasmtime::Result<TheWorld> 50 where 51 _T: Send, 52 { 53 let mut store = store.as_context_mut(); 54 let instance = self.instance_pre.instantiate_async(&mut store).await?; 55 self.indices.load(&mut store, &instance) 56 } 57 } 58 /// Auto-generated bindings for index of the exports of 59 /// `the-world`. 60 /// 61 /// This is an implementation detail of [`TheWorldPre`] and can 62 /// be constructed if needed as well. 63 /// 64 /// For more information see [`TheWorld`] as well. 65 #[derive(Clone)] 66 pub struct TheWorldIndices { 67 interface0: exports::foo::foo::strings::GuestIndices, 68 } 69 /// Auto-generated bindings for an instance a component which 70 /// implements the world `the-world`. 71 /// 72 /// This structure can be created through a number of means 73 /// depending on your requirements and what you have on hand: 74 /// 75 /// * The most convenient way is to use 76 /// [`TheWorld::instantiate_async`] which only needs a 77 /// [`Store`], [`Component`], and [`Linker`]. 78 /// 79 /// * Alternatively you can create a [`TheWorldPre`] ahead of 80 /// time with a [`Component`] to front-load string lookups 81 /// of exports once instead of per-instantiation. This 82 /// method then uses [`TheWorldPre::instantiate_async`] to 83 /// create a [`TheWorld`]. 84 /// 85 /// * If you've instantiated the instance yourself already 86 /// then you can use [`TheWorld::new`]. 87 /// 88 /// * You can also access the guts of instantiation through 89 /// [`TheWorldIndices::new_instance`] followed 90 /// by [`TheWorldIndices::load`] to crate an instance of this 91 /// type. 92 /// 93 /// These methods are all equivalent to one another and move 94 /// around the tradeoff of what work is performed when. 95 /// 96 /// [`Store`]: wasmtime::Store 97 /// [`Component`]: wasmtime::component::Component 98 /// [`Linker`]: wasmtime::component::Linker 99 pub struct TheWorld { 100 interface0: exports::foo::foo::strings::Guest, 101 } 102 const _: () = { 103 #[allow(unused_imports)] 104 use wasmtime::component::__internal::anyhow; 105 impl TheWorldIndices { 106 /// Creates a new copy of `TheWorldIndices` bindings which can then 107 /// be used to instantiate into a particular store. 108 /// 109 /// This method may fail if the component does not have the 110 /// required exports. 111 pub fn new( 112 component: &wasmtime::component::Component, 113 ) -> wasmtime::Result<Self> { 114 let _component = component; 115 let interface0 = exports::foo::foo::strings::GuestIndices::new(_component)?; 116 Ok(TheWorldIndices { interface0 }) 117 } 118 /// Creates a new instance of [`TheWorldIndices`] from an 119 /// instantiated component. 120 /// 121 /// This method of creating a [`TheWorld`] will perform string 122 /// lookups for all exports when this method is called. This 123 /// will only succeed if the provided instance matches the 124 /// requirements of [`TheWorld`]. 125 pub fn new_instance( 126 mut store: impl wasmtime::AsContextMut, 127 instance: &wasmtime::component::Instance, 128 ) -> wasmtime::Result<Self> { 129 let _instance = instance; 130 let interface0 = exports::foo::foo::strings::GuestIndices::new_instance( 131 &mut store, 132 _instance, 133 )?; 134 Ok(TheWorldIndices { interface0 }) 135 } 136 /// Uses the indices stored in `self` to load an instance 137 /// of [`TheWorld`] from the instance provided. 138 /// 139 /// Note that at this time this method will additionally 140 /// perform type-checks of all exports. 141 pub fn load( 142 &self, 143 mut store: impl wasmtime::AsContextMut, 144 instance: &wasmtime::component::Instance, 145 ) -> wasmtime::Result<TheWorld> { 146 let _instance = instance; 147 let interface0 = self.interface0.load(&mut store, &_instance)?; 148 Ok(TheWorld { interface0 }) 149 } 150 } 151 impl TheWorld { 152 /// Convenience wrapper around [`TheWorldPre::new`] and 153 /// [`TheWorldPre::instantiate_async`]. 154 pub async fn instantiate_async<_T>( 155 mut store: impl wasmtime::AsContextMut<Data = _T>, 156 component: &wasmtime::component::Component, 157 linker: &wasmtime::component::Linker<_T>, 158 ) -> wasmtime::Result<TheWorld> 159 where 160 _T: Send, 161 { 162 let pre = linker.instantiate_pre(component)?; 163 TheWorldPre::new(pre)?.instantiate_async(store).await 164 } 165 /// Convenience wrapper around [`TheWorldIndices::new_instance`] and 166 /// [`TheWorldIndices::load`]. 167 pub fn new( 168 mut store: impl wasmtime::AsContextMut, 169 instance: &wasmtime::component::Instance, 170 ) -> wasmtime::Result<TheWorld> { 171 let indices = TheWorldIndices::new_instance(&mut store, instance)?; 172 indices.load(store, instance) 173 } 174 pub fn add_to_linker<T, U>( 175 linker: &mut wasmtime::component::Linker<T>, 176 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 177 ) -> wasmtime::Result<()> 178 where 179 T: Send, 180 U: foo::foo::strings::Host + Send, 181 { 182 foo::foo::strings::add_to_linker(linker, get)?; 183 Ok(()) 184 } 185 pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { 186 &self.interface0 187 } 188 } 189 }; 190 pub mod foo { 191 pub mod foo { 192 #[allow(clippy::all)] 193 pub mod strings { 194 #[allow(unused_imports)] 195 use wasmtime::component::__internal::anyhow; 196 #[wasmtime::component::__internal::async_trait] 197 pub trait Host: Send { 198 async fn a(&mut self, x: wasmtime::component::__internal::String) -> (); 199 async fn b(&mut self) -> wasmtime::component::__internal::String; 200 async fn c( 201 &mut self, 202 a: wasmtime::component::__internal::String, 203 b: wasmtime::component::__internal::String, 204 ) -> wasmtime::component::__internal::String; 205 } 206 pub trait GetHost< 207 T, 208 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 209 type Host: Host + Send; 210 } 211 impl<F, T, O> GetHost<T> for F 212 where 213 F: Fn(T) -> O + Send + Sync + Copy + 'static, 214 O: Host + Send, 215 { 216 type Host = O; 217 } 218 pub fn add_to_linker_get_host<T>( 219 linker: &mut wasmtime::component::Linker<T>, 220 host_getter: impl for<'a> GetHost<&'a mut T>, 221 ) -> wasmtime::Result<()> 222 where 223 T: Send, 224 { 225 let mut inst = linker.instance("foo:foo/strings")?; 226 inst.func_wrap_async( 227 "a", 228 move | 229 mut caller: wasmtime::StoreContextMut<'_, T>, 230 (arg0,): (wasmtime::component::__internal::String,)| 231 { 232 use tracing::Instrument; 233 let span = tracing::span!( 234 tracing::Level::TRACE, "wit-bindgen import", module = 235 "strings", function = "a", 236 ); 237 wasmtime::component::__internal::Box::new( 238 async move { 239 tracing::event!( 240 tracing::Level::TRACE, x = tracing::field::debug(& arg0), 241 "call" 242 ); 243 let host = &mut host_getter(caller.data_mut()); 244 let r = Host::a(host, arg0).await; 245 tracing::event!( 246 tracing::Level::TRACE, result = tracing::field::debug(& r), 247 "return" 248 ); 249 Ok(r) 250 } 251 .instrument(span), 252 ) 253 }, 254 )?; 255 inst.func_wrap_async( 256 "b", 257 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 258 use tracing::Instrument; 259 let span = tracing::span!( 260 tracing::Level::TRACE, "wit-bindgen import", module = 261 "strings", function = "b", 262 ); 263 wasmtime::component::__internal::Box::new( 264 async move { 265 tracing::event!(tracing::Level::TRACE, "call"); 266 let host = &mut host_getter(caller.data_mut()); 267 let r = Host::b(host).await; 268 tracing::event!( 269 tracing::Level::TRACE, result = tracing::field::debug(& r), 270 "return" 271 ); 272 Ok((r,)) 273 } 274 .instrument(span), 275 ) 276 }, 277 )?; 278 inst.func_wrap_async( 279 "c", 280 move | 281 mut caller: wasmtime::StoreContextMut<'_, T>, 282 ( 283 arg0, 284 arg1, 285 ): ( 286 wasmtime::component::__internal::String, 287 wasmtime::component::__internal::String, 288 )| 289 { 290 use tracing::Instrument; 291 let span = tracing::span!( 292 tracing::Level::TRACE, "wit-bindgen import", module = 293 "strings", function = "c", 294 ); 295 wasmtime::component::__internal::Box::new( 296 async move { 297 tracing::event!( 298 tracing::Level::TRACE, a = tracing::field::debug(& arg0), b 299 = tracing::field::debug(& arg1), "call" 300 ); 301 let host = &mut host_getter(caller.data_mut()); 302 let r = Host::c(host, arg0, arg1).await; 303 tracing::event!( 304 tracing::Level::TRACE, result = tracing::field::debug(& r), 305 "return" 306 ); 307 Ok((r,)) 308 } 309 .instrument(span), 310 ) 311 }, 312 )?; 313 Ok(()) 314 } 315 pub fn add_to_linker<T, U>( 316 linker: &mut wasmtime::component::Linker<T>, 317 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 318 ) -> wasmtime::Result<()> 319 where 320 U: Host + Send, 321 T: Send, 322 { 323 add_to_linker_get_host(linker, get) 324 } 325 #[wasmtime::component::__internal::async_trait] 326 impl<_T: Host + ?Sized + Send> Host for &mut _T { 327 async fn a(&mut self, x: wasmtime::component::__internal::String) -> () { 328 Host::a(*self, x).await 329 } 330 async fn b(&mut self) -> wasmtime::component::__internal::String { 331 Host::b(*self).await 332 } 333 async fn c( 334 &mut self, 335 a: wasmtime::component::__internal::String, 336 b: wasmtime::component::__internal::String, 337 ) -> wasmtime::component::__internal::String { 338 Host::c(*self, a, b).await 339 } 340 } 341 } 342 } 343 } 344 pub mod exports { 345 pub mod foo { 346 pub mod foo { 347 #[allow(clippy::all)] 348 pub mod strings { 349 #[allow(unused_imports)] 350 use wasmtime::component::__internal::anyhow; 351 pub struct Guest { 352 a: wasmtime::component::Func, 353 b: wasmtime::component::Func, 354 c: wasmtime::component::Func, 355 } 356 #[derive(Clone)] 357 pub struct GuestIndices { 358 a: wasmtime::component::ComponentExportIndex, 359 b: wasmtime::component::ComponentExportIndex, 360 c: wasmtime::component::ComponentExportIndex, 361 } 362 impl GuestIndices { 363 /// Constructor for [`GuestIndices`] which takes a 364 /// [`Component`](wasmtime::component::Component) as input and can be executed 365 /// before instantiation. 366 /// 367 /// This constructor can be used to front-load string lookups to find exports 368 /// within a component. 369 pub fn new( 370 component: &wasmtime::component::Component, 371 ) -> wasmtime::Result<GuestIndices> { 372 let (_, instance) = component 373 .export_index(None, "foo:foo/strings") 374 .ok_or_else(|| { 375 anyhow::anyhow!( 376 "no exported instance named `foo:foo/strings`" 377 ) 378 })?; 379 Self::_new(|name| { 380 component.export_index(Some(&instance), name).map(|p| p.1) 381 }) 382 } 383 /// This constructor is similar to [`GuestIndices::new`] except that it 384 /// performs string lookups after instantiation time. 385 pub fn new_instance( 386 mut store: impl wasmtime::AsContextMut, 387 instance: &wasmtime::component::Instance, 388 ) -> wasmtime::Result<GuestIndices> { 389 let instance_export = instance 390 .get_export(&mut store, None, "foo:foo/strings") 391 .ok_or_else(|| { 392 anyhow::anyhow!( 393 "no exported instance named `foo:foo/strings`" 394 ) 395 })?; 396 Self::_new(|name| { 397 instance.get_export(&mut store, Some(&instance_export), name) 398 }) 399 } 400 fn _new( 401 mut lookup: impl FnMut( 402 &str, 403 ) -> Option<wasmtime::component::ComponentExportIndex>, 404 ) -> wasmtime::Result<GuestIndices> { 405 let mut lookup = move |name| { 406 lookup(name) 407 .ok_or_else(|| { 408 anyhow::anyhow!( 409 "instance export `foo:foo/strings` does \ 410 not have export `{name}`" 411 ) 412 }) 413 }; 414 let _ = &mut lookup; 415 let a = lookup("a")?; 416 let b = lookup("b")?; 417 let c = lookup("c")?; 418 Ok(GuestIndices { a, b, c }) 419 } 420 pub fn load( 421 &self, 422 mut store: impl wasmtime::AsContextMut, 423 instance: &wasmtime::component::Instance, 424 ) -> wasmtime::Result<Guest> { 425 let mut store = store.as_context_mut(); 426 let _ = &mut store; 427 let _instance = instance; 428 let a = *_instance 429 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 430 .func(); 431 let b = *_instance 432 .get_typed_func::< 433 (), 434 (wasmtime::component::__internal::String,), 435 >(&mut store, &self.b)? 436 .func(); 437 let c = *_instance 438 .get_typed_func::< 439 (&str, &str), 440 (wasmtime::component::__internal::String,), 441 >(&mut store, &self.c)? 442 .func(); 443 Ok(Guest { a, b, c }) 444 } 445 } 446 impl Guest { 447 pub async fn call_a<S: wasmtime::AsContextMut>( 448 &self, 449 mut store: S, 450 arg0: &str, 451 ) -> wasmtime::Result<()> 452 where 453 <S as wasmtime::AsContext>::Data: Send, 454 { 455 use tracing::Instrument; 456 let span = tracing::span!( 457 tracing::Level::TRACE, "wit-bindgen export", module = 458 "foo:foo/strings", function = "a", 459 ); 460 let callee = unsafe { 461 wasmtime::component::TypedFunc::< 462 (&str,), 463 (), 464 >::new_unchecked(self.a) 465 }; 466 let () = callee 467 .call_async(store.as_context_mut(), (arg0,)) 468 .instrument(span.clone()) 469 .await?; 470 callee 471 .post_return_async(store.as_context_mut()) 472 .instrument(span) 473 .await?; 474 Ok(()) 475 } 476 pub async fn call_b<S: wasmtime::AsContextMut>( 477 &self, 478 mut store: S, 479 ) -> wasmtime::Result<wasmtime::component::__internal::String> 480 where 481 <S as wasmtime::AsContext>::Data: Send, 482 { 483 use tracing::Instrument; 484 let span = tracing::span!( 485 tracing::Level::TRACE, "wit-bindgen export", module = 486 "foo:foo/strings", function = "b", 487 ); 488 let callee = unsafe { 489 wasmtime::component::TypedFunc::< 490 (), 491 (wasmtime::component::__internal::String,), 492 >::new_unchecked(self.b) 493 }; 494 let (ret0,) = callee 495 .call_async(store.as_context_mut(), ()) 496 .instrument(span.clone()) 497 .await?; 498 callee 499 .post_return_async(store.as_context_mut()) 500 .instrument(span) 501 .await?; 502 Ok(ret0) 503 } 504 pub async fn call_c<S: wasmtime::AsContextMut>( 505 &self, 506 mut store: S, 507 arg0: &str, 508 arg1: &str, 509 ) -> wasmtime::Result<wasmtime::component::__internal::String> 510 where 511 <S as wasmtime::AsContext>::Data: Send, 512 { 513 use tracing::Instrument; 514 let span = tracing::span!( 515 tracing::Level::TRACE, "wit-bindgen export", module = 516 "foo:foo/strings", function = "c", 517 ); 518 let callee = unsafe { 519 wasmtime::component::TypedFunc::< 520 (&str, &str), 521 (wasmtime::component::__internal::String,), 522 >::new_unchecked(self.c) 523 }; 524 let (ret0,) = callee 525 .call_async(store.as_context_mut(), (arg0, arg1)) 526 .instrument(span.clone()) 527 .await?; 528 callee 529 .post_return_async(store.as_context_mut()) 530 .instrument(span) 531 .await?; 532 Ok(ret0) 533 } 534 } 535 } 536 } 537 } 538 } 539