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::strings::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::strings::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::strings::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::strings::HostWithStore + Send, for<'a> D::Data<'a>: foo::foo::strings::Host + Send, T: 'static + Send,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::strings::HostWithStore + Send, 177 for<'a> D::Data<'a>: foo::foo::strings::Host + Send, 178 T: 'static + Send, 179 { 180 foo::foo::strings::add_to_linker::<T, D>(linker, host_getter)?; 181 Ok(()) 182 } foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest183 pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { 184 &self.interface0 185 } 186 } 187 }; 188 pub mod foo { 189 pub mod foo { 190 #[allow(clippy::all)] 191 pub mod strings { 192 #[allow(unused_imports)] 193 use wasmtime::component::__internal::Box; 194 pub trait HostWithStore: wasmtime::component::HasData + Send {} 195 impl<_T: ?Sized> HostWithStore for _T 196 where 197 _T: wasmtime::component::HasData + Send, 198 {} 199 pub trait Host: Send { a( &mut self, x: wasmtime::component::__internal::String, ) -> impl ::core::future::Future<Output = ()> + Send200 fn a( 201 &mut self, 202 x: wasmtime::component::__internal::String, 203 ) -> impl ::core::future::Future<Output = ()> + Send; b( &mut self, ) -> impl ::core::future::Future< Output = wasmtime::component::__internal::String, > + Send204 fn b( 205 &mut self, 206 ) -> impl ::core::future::Future< 207 Output = wasmtime::component::__internal::String, 208 > + Send; c( &mut self, a: wasmtime::component::__internal::String, b: wasmtime::component::__internal::String, ) -> impl ::core::future::Future< Output = wasmtime::component::__internal::String, > + Send209 fn c( 210 &mut self, 211 a: wasmtime::component::__internal::String, 212 b: wasmtime::component::__internal::String, 213 ) -> impl ::core::future::Future< 214 Output = wasmtime::component::__internal::String, 215 > + Send; 216 } 217 impl<_T: Host + ?Sized + Send> Host for &mut _T { a( &mut self, x: wasmtime::component::__internal::String, ) -> impl ::core::future::Future<Output = ()> + Send218 fn a( 219 &mut self, 220 x: wasmtime::component::__internal::String, 221 ) -> impl ::core::future::Future<Output = ()> + Send { 222 async move { Host::a(*self, x).await } 223 } b( &mut self, ) -> impl ::core::future::Future< Output = wasmtime::component::__internal::String, > + Send224 fn b( 225 &mut self, 226 ) -> impl ::core::future::Future< 227 Output = wasmtime::component::__internal::String, 228 > + Send { 229 async move { Host::b(*self).await } 230 } c( &mut self, a: wasmtime::component::__internal::String, b: wasmtime::component::__internal::String, ) -> impl ::core::future::Future< Output = wasmtime::component::__internal::String, > + Send231 fn c( 232 &mut self, 233 a: wasmtime::component::__internal::String, 234 b: wasmtime::component::__internal::String, 235 ) -> impl ::core::future::Future< 236 Output = wasmtime::component::__internal::String, 237 > + Send { 238 async move { Host::c(*self, a, b).await } 239 } 240 } 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 + Send,241 pub fn add_to_linker<T, D>( 242 linker: &mut wasmtime::component::Linker<T>, 243 host_getter: fn(&mut T) -> D::Data<'_>, 244 ) -> wasmtime::Result<()> 245 where 246 D: HostWithStore, 247 for<'a> D::Data<'a>: Host, 248 T: 'static + Send, 249 { 250 let mut inst = linker.instance("foo:foo/strings")?; 251 inst.func_wrap_async( 252 "a", 253 move | 254 mut caller: wasmtime::StoreContextMut<'_, T>, 255 (arg0,): (wasmtime::component::__internal::String,)| 256 { 257 use tracing::Instrument; 258 let span = tracing::span!( 259 tracing::Level::TRACE, "wit-bindgen import", module = 260 "strings", function = "a", 261 ); 262 wasmtime::component::__internal::Box::new( 263 async move { 264 tracing::event!( 265 tracing::Level::TRACE, x = tracing::field::debug(& arg0), 266 "call" 267 ); 268 let host = &mut host_getter(caller.data_mut()); 269 let r = Host::a(host, arg0).await; 270 tracing::event!( 271 tracing::Level::TRACE, result = tracing::field::debug(& r), 272 "return" 273 ); 274 Ok(r) 275 } 276 .instrument(span), 277 ) 278 }, 279 )?; 280 inst.func_wrap_async( 281 "b", 282 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 283 use tracing::Instrument; 284 let span = tracing::span!( 285 tracing::Level::TRACE, "wit-bindgen import", module = 286 "strings", function = "b", 287 ); 288 wasmtime::component::__internal::Box::new( 289 async move { 290 tracing::event!(tracing::Level::TRACE, "call"); 291 let host = &mut host_getter(caller.data_mut()); 292 let r = Host::b(host).await; 293 tracing::event!( 294 tracing::Level::TRACE, result = tracing::field::debug(& r), 295 "return" 296 ); 297 Ok((r,)) 298 } 299 .instrument(span), 300 ) 301 }, 302 )?; 303 inst.func_wrap_async( 304 "c", 305 move | 306 mut caller: wasmtime::StoreContextMut<'_, T>, 307 ( 308 arg0, 309 arg1, 310 ): ( 311 wasmtime::component::__internal::String, 312 wasmtime::component::__internal::String, 313 )| 314 { 315 use tracing::Instrument; 316 let span = tracing::span!( 317 tracing::Level::TRACE, "wit-bindgen import", module = 318 "strings", function = "c", 319 ); 320 wasmtime::component::__internal::Box::new( 321 async move { 322 tracing::event!( 323 tracing::Level::TRACE, a = tracing::field::debug(& arg0), b 324 = tracing::field::debug(& arg1), "call" 325 ); 326 let host = &mut host_getter(caller.data_mut()); 327 let r = Host::c(host, arg0, arg1).await; 328 tracing::event!( 329 tracing::Level::TRACE, result = tracing::field::debug(& r), 330 "return" 331 ); 332 Ok((r,)) 333 } 334 .instrument(span), 335 ) 336 }, 337 )?; 338 Ok(()) 339 } 340 } 341 } 342 } 343 pub mod exports { 344 pub mod foo { 345 pub mod foo { 346 #[allow(clippy::all)] 347 pub mod strings { 348 #[allow(unused_imports)] 349 use wasmtime::component::__internal::Box; 350 #[derive(Clone)] 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. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<GuestIndices>369 pub fn new<_T>( 370 _instance_pre: &wasmtime::component::InstancePre<_T>, 371 ) -> wasmtime::Result<GuestIndices> { 372 let instance = _instance_pre 373 .component() 374 .get_export_index(None, "foo:foo/strings") 375 .ok_or_else(|| { 376 wasmtime::format_err!( 377 "no exported instance named `foo:foo/strings`" 378 ) 379 })?; 380 let mut lookup = move |name| { 381 _instance_pre 382 .component() 383 .get_export_index(Some(&instance), name) 384 .ok_or_else(|| { 385 wasmtime::format_err!( 386 "instance export `foo:foo/strings` does \ 387 not have export `{name}`" 388 ) 389 }) 390 }; 391 let _ = &mut lookup; 392 let a = lookup("a")?; 393 let b = lookup("b")?; 394 let c = lookup("c")?; 395 Ok(GuestIndices { a, b, c }) 396 } load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Guest>397 pub fn load( 398 &self, 399 mut store: impl wasmtime::AsContextMut, 400 instance: &wasmtime::component::Instance, 401 ) -> wasmtime::Result<Guest> { 402 let _instance = instance; 403 let _instance_pre = _instance.instance_pre(&store); 404 let _instance_type = _instance_pre.instance_type(); 405 let mut store = store.as_context_mut(); 406 let _ = &mut store; 407 let a = *_instance 408 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 409 .func(); 410 let b = *_instance 411 .get_typed_func::< 412 (), 413 (wasmtime::component::__internal::String,), 414 >(&mut store, &self.b)? 415 .func(); 416 let c = *_instance 417 .get_typed_func::< 418 (&str, &str), 419 (wasmtime::component::__internal::String,), 420 >(&mut store, &self.c)? 421 .func(); 422 Ok(Guest { a, b, c }) 423 } 424 } 425 impl Guest { call_a<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: &str, ) -> wasmtime::Result<()> where <S as wasmtime::AsContext>::Data: Send,426 pub async fn call_a<S: wasmtime::AsContextMut>( 427 &self, 428 mut store: S, 429 arg0: &str, 430 ) -> wasmtime::Result<()> 431 where 432 <S as wasmtime::AsContext>::Data: Send, 433 { 434 use tracing::Instrument; 435 let span = tracing::span!( 436 tracing::Level::TRACE, "wit-bindgen export", module = 437 "foo:foo/strings", function = "a", 438 ); 439 let callee = unsafe { 440 wasmtime::component::TypedFunc::< 441 (&str,), 442 (), 443 >::new_unchecked(self.a) 444 }; 445 let () = callee 446 .call_async(store.as_context_mut(), (arg0,)) 447 .instrument(span.clone()) 448 .await?; 449 Ok(()) 450 } call_b<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<wasmtime::component::__internal::String> where <S as wasmtime::AsContext>::Data: Send,451 pub async fn call_b<S: wasmtime::AsContextMut>( 452 &self, 453 mut store: S, 454 ) -> wasmtime::Result<wasmtime::component::__internal::String> 455 where 456 <S as wasmtime::AsContext>::Data: Send, 457 { 458 use tracing::Instrument; 459 let span = tracing::span!( 460 tracing::Level::TRACE, "wit-bindgen export", module = 461 "foo:foo/strings", function = "b", 462 ); 463 let callee = unsafe { 464 wasmtime::component::TypedFunc::< 465 (), 466 (wasmtime::component::__internal::String,), 467 >::new_unchecked(self.b) 468 }; 469 let (ret0,) = callee 470 .call_async(store.as_context_mut(), ()) 471 .instrument(span.clone()) 472 .await?; 473 Ok(ret0) 474 } call_c<S: wasmtime::AsContextMut>( &self, mut store: S, arg0: &str, arg1: &str, ) -> wasmtime::Result<wasmtime::component::__internal::String> where <S as wasmtime::AsContext>::Data: Send,475 pub async fn call_c<S: wasmtime::AsContextMut>( 476 &self, 477 mut store: S, 478 arg0: &str, 479 arg1: &str, 480 ) -> wasmtime::Result<wasmtime::component::__internal::String> 481 where 482 <S as wasmtime::AsContext>::Data: Send, 483 { 484 use tracing::Instrument; 485 let span = tracing::span!( 486 tracing::Level::TRACE, "wit-bindgen export", module = 487 "foo:foo/strings", function = "c", 488 ); 489 let callee = unsafe { 490 wasmtime::component::TypedFunc::< 491 (&str, &str), 492 (wasmtime::component::__internal::String,), 493 >::new_unchecked(self.c) 494 }; 495 let (ret0,) = callee 496 .call_async(store.as_context_mut(), (arg0, arg1)) 497 .instrument(span.clone()) 498 .await?; 499 Ok(ret0) 500 } 501 } 502 } 503 } 504 } 505 } 506