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> { 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> 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)?; 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 /// These methods are all equivalent to one another and move 89 /// around the tradeoff of what work is performed when. 90 /// 91 /// [`Store`]: wasmtime::Store 92 /// [`Component`]: wasmtime::component::Component 93 /// [`Linker`]: wasmtime::component::Linker 94 pub struct TheWorld { 95 interface0: exports::foo::foo::strings::Guest, 96 } 97 const _: () = { 98 #[allow(unused_imports)] 99 use wasmtime::component::__internal::anyhow; 100 impl TheWorldIndices { 101 /// Creates a new copy of `TheWorldIndices` bindings which can then 102 /// be used to instantiate into a particular store. 103 /// 104 /// This method may fail if the component does not have the 105 /// required exports. 106 pub fn new<_T>( 107 _instance_pre: &wasmtime::component::InstancePre<_T>, 108 ) -> wasmtime::Result<Self> { 109 let _component = _instance_pre.component(); 110 let _instance_type = _instance_pre.instance_type(); 111 let interface0 = exports::foo::foo::strings::GuestIndices::new( 112 _instance_pre, 113 )?; 114 Ok(TheWorldIndices { interface0 }) 115 } 116 /// Uses the indices stored in `self` to load an instance 117 /// of [`TheWorld`] from the instance provided. 118 /// 119 /// Note that at this time this method will additionally 120 /// perform type-checks of all exports. 121 pub fn load( 122 &self, 123 mut store: impl wasmtime::AsContextMut, 124 instance: &wasmtime::component::Instance, 125 ) -> wasmtime::Result<TheWorld> { 126 let _ = &mut store; 127 let _instance = instance; 128 let interface0 = self.interface0.load(&mut store, &_instance)?; 129 Ok(TheWorld { interface0 }) 130 } 131 } 132 impl TheWorld { 133 /// Convenience wrapper around [`TheWorldPre::new`] and 134 /// [`TheWorldPre::instantiate_async`]. 135 pub async fn instantiate_async<_T>( 136 store: impl wasmtime::AsContextMut<Data = _T>, 137 component: &wasmtime::component::Component, 138 linker: &wasmtime::component::Linker<_T>, 139 ) -> wasmtime::Result<TheWorld> 140 where 141 _T: Send, 142 { 143 let pre = linker.instantiate_pre(component)?; 144 TheWorldPre::new(pre)?.instantiate_async(store).await 145 } 146 /// Convenience wrapper around [`TheWorldIndices::new`] and 147 /// [`TheWorldIndices::load`]. 148 pub fn new( 149 mut store: impl wasmtime::AsContextMut, 150 instance: &wasmtime::component::Instance, 151 ) -> wasmtime::Result<TheWorld> { 152 let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; 153 indices.load(&mut store, instance) 154 } 155 pub fn add_to_linker<T, D>( 156 linker: &mut wasmtime::component::Linker<T>, 157 host_getter: fn(&mut T) -> D::Data<'_>, 158 ) -> wasmtime::Result<()> 159 where 160 D: wasmtime::component::HasData, 161 for<'a> D::Data<'a>: foo::foo::strings::Host + Send, 162 T: 'static + Send, 163 { 164 foo::foo::strings::add_to_linker::<T, D>(linker, host_getter)?; 165 Ok(()) 166 } 167 pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { 168 &self.interface0 169 } 170 } 171 }; 172 pub mod foo { 173 pub mod foo { 174 #[allow(clippy::all)] 175 pub mod strings { 176 #[allow(unused_imports)] 177 use wasmtime::component::__internal::{anyhow, Box}; 178 #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] 179 pub trait Host: Send { 180 async fn a(&mut self, x: wasmtime::component::__internal::String) -> (); 181 async fn b(&mut self) -> wasmtime::component::__internal::String; 182 async fn c( 183 &mut self, 184 a: wasmtime::component::__internal::String, 185 b: wasmtime::component::__internal::String, 186 ) -> wasmtime::component::__internal::String; 187 } 188 impl<_T: Host + ?Sized + Send> Host for &mut _T { 189 async fn a(&mut self, x: wasmtime::component::__internal::String) -> () { 190 Host::a(*self, x).await 191 } 192 async fn b(&mut self) -> wasmtime::component::__internal::String { 193 Host::b(*self).await 194 } 195 async fn c( 196 &mut self, 197 a: wasmtime::component::__internal::String, 198 b: wasmtime::component::__internal::String, 199 ) -> wasmtime::component::__internal::String { 200 Host::c(*self, a, b).await 201 } 202 } 203 pub fn add_to_linker<T, D>( 204 linker: &mut wasmtime::component::Linker<T>, 205 host_getter: fn(&mut T) -> D::Data<'_>, 206 ) -> wasmtime::Result<()> 207 where 208 D: wasmtime::component::HasData, 209 for<'a> D::Data<'a>: Host, 210 T: 'static + Send, 211 { 212 let mut inst = linker.instance("foo:foo/strings")?; 213 inst.func_wrap_async( 214 "a", 215 move | 216 mut caller: wasmtime::StoreContextMut<'_, T>, 217 (arg0,): (wasmtime::component::__internal::String,)| 218 { 219 use tracing::Instrument; 220 let span = tracing::span!( 221 tracing::Level::TRACE, "wit-bindgen import", module = 222 "strings", function = "a", 223 ); 224 wasmtime::component::__internal::Box::new( 225 async move { 226 tracing::event!( 227 tracing::Level::TRACE, x = tracing::field::debug(& arg0), 228 "call" 229 ); 230 let host = &mut host_getter(caller.data_mut()); 231 let r = Host::a(host, arg0).await; 232 tracing::event!( 233 tracing::Level::TRACE, result = tracing::field::debug(& r), 234 "return" 235 ); 236 Ok(r) 237 } 238 .instrument(span), 239 ) 240 }, 241 )?; 242 inst.func_wrap_async( 243 "b", 244 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 245 use tracing::Instrument; 246 let span = tracing::span!( 247 tracing::Level::TRACE, "wit-bindgen import", module = 248 "strings", function = "b", 249 ); 250 wasmtime::component::__internal::Box::new( 251 async move { 252 tracing::event!(tracing::Level::TRACE, "call"); 253 let host = &mut host_getter(caller.data_mut()); 254 let r = Host::b(host).await; 255 tracing::event!( 256 tracing::Level::TRACE, result = tracing::field::debug(& r), 257 "return" 258 ); 259 Ok((r,)) 260 } 261 .instrument(span), 262 ) 263 }, 264 )?; 265 inst.func_wrap_async( 266 "c", 267 move | 268 mut caller: wasmtime::StoreContextMut<'_, T>, 269 ( 270 arg0, 271 arg1, 272 ): ( 273 wasmtime::component::__internal::String, 274 wasmtime::component::__internal::String, 275 )| 276 { 277 use tracing::Instrument; 278 let span = tracing::span!( 279 tracing::Level::TRACE, "wit-bindgen import", module = 280 "strings", function = "c", 281 ); 282 wasmtime::component::__internal::Box::new( 283 async move { 284 tracing::event!( 285 tracing::Level::TRACE, a = tracing::field::debug(& arg0), b 286 = tracing::field::debug(& arg1), "call" 287 ); 288 let host = &mut host_getter(caller.data_mut()); 289 let r = Host::c(host, arg0, arg1).await; 290 tracing::event!( 291 tracing::Level::TRACE, result = tracing::field::debug(& r), 292 "return" 293 ); 294 Ok((r,)) 295 } 296 .instrument(span), 297 ) 298 }, 299 )?; 300 Ok(()) 301 } 302 } 303 } 304 } 305 pub mod exports { 306 pub mod foo { 307 pub mod foo { 308 #[allow(clippy::all)] 309 pub mod strings { 310 #[allow(unused_imports)] 311 use wasmtime::component::__internal::{anyhow, Box}; 312 pub struct Guest { 313 a: wasmtime::component::Func, 314 b: wasmtime::component::Func, 315 c: wasmtime::component::Func, 316 } 317 #[derive(Clone)] 318 pub struct GuestIndices { 319 a: wasmtime::component::ComponentExportIndex, 320 b: wasmtime::component::ComponentExportIndex, 321 c: wasmtime::component::ComponentExportIndex, 322 } 323 impl GuestIndices { 324 /// Constructor for [`GuestIndices`] which takes a 325 /// [`Component`](wasmtime::component::Component) as input and can be executed 326 /// before instantiation. 327 /// 328 /// This constructor can be used to front-load string lookups to find exports 329 /// within a component. 330 pub fn new<_T>( 331 _instance_pre: &wasmtime::component::InstancePre<_T>, 332 ) -> wasmtime::Result<GuestIndices> { 333 let instance = _instance_pre 334 .component() 335 .get_export_index(None, "foo:foo/strings") 336 .ok_or_else(|| { 337 anyhow::anyhow!( 338 "no exported instance named `foo:foo/strings`" 339 ) 340 })?; 341 let mut lookup = move |name| { 342 _instance_pre 343 .component() 344 .get_export_index(Some(&instance), name) 345 .ok_or_else(|| { 346 anyhow::anyhow!( 347 "instance export `foo:foo/strings` does \ 348 not have export `{name}`" 349 ) 350 }) 351 }; 352 let _ = &mut lookup; 353 let a = lookup("a")?; 354 let b = lookup("b")?; 355 let c = lookup("c")?; 356 Ok(GuestIndices { a, b, c }) 357 } 358 pub fn load( 359 &self, 360 mut store: impl wasmtime::AsContextMut, 361 instance: &wasmtime::component::Instance, 362 ) -> wasmtime::Result<Guest> { 363 let _instance = instance; 364 let _instance_pre = _instance.instance_pre(&store); 365 let _instance_type = _instance_pre.instance_type(); 366 let mut store = store.as_context_mut(); 367 let _ = &mut store; 368 let a = *_instance 369 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 370 .func(); 371 let b = *_instance 372 .get_typed_func::< 373 (), 374 (wasmtime::component::__internal::String,), 375 >(&mut store, &self.b)? 376 .func(); 377 let c = *_instance 378 .get_typed_func::< 379 (&str, &str), 380 (wasmtime::component::__internal::String,), 381 >(&mut store, &self.c)? 382 .func(); 383 Ok(Guest { a, b, c }) 384 } 385 } 386 impl Guest { 387 pub async fn call_a<S: wasmtime::AsContextMut>( 388 &self, 389 mut store: S, 390 arg0: &str, 391 ) -> wasmtime::Result<()> 392 where 393 <S as wasmtime::AsContext>::Data: Send, 394 { 395 use tracing::Instrument; 396 let span = tracing::span!( 397 tracing::Level::TRACE, "wit-bindgen export", module = 398 "foo:foo/strings", function = "a", 399 ); 400 let callee = unsafe { 401 wasmtime::component::TypedFunc::< 402 (&str,), 403 (), 404 >::new_unchecked(self.a) 405 }; 406 let () = callee 407 .call_async(store.as_context_mut(), (arg0,)) 408 .instrument(span.clone()) 409 .await?; 410 callee 411 .post_return_async(store.as_context_mut()) 412 .instrument(span) 413 .await?; 414 Ok(()) 415 } 416 pub async fn call_b<S: wasmtime::AsContextMut>( 417 &self, 418 mut store: S, 419 ) -> wasmtime::Result<wasmtime::component::__internal::String> 420 where 421 <S as wasmtime::AsContext>::Data: Send, 422 { 423 use tracing::Instrument; 424 let span = tracing::span!( 425 tracing::Level::TRACE, "wit-bindgen export", module = 426 "foo:foo/strings", function = "b", 427 ); 428 let callee = unsafe { 429 wasmtime::component::TypedFunc::< 430 (), 431 (wasmtime::component::__internal::String,), 432 >::new_unchecked(self.b) 433 }; 434 let (ret0,) = callee 435 .call_async(store.as_context_mut(), ()) 436 .instrument(span.clone()) 437 .await?; 438 callee 439 .post_return_async(store.as_context_mut()) 440 .instrument(span) 441 .await?; 442 Ok(ret0) 443 } 444 pub async fn call_c<S: wasmtime::AsContextMut>( 445 &self, 446 mut store: S, 447 arg0: &str, 448 arg1: &str, 449 ) -> wasmtime::Result<wasmtime::component::__internal::String> 450 where 451 <S as wasmtime::AsContext>::Data: Send, 452 { 453 use tracing::Instrument; 454 let span = tracing::span!( 455 tracing::Level::TRACE, "wit-bindgen export", module = 456 "foo:foo/strings", function = "c", 457 ); 458 let callee = unsafe { 459 wasmtime::component::TypedFunc::< 460 (&str, &str), 461 (wasmtime::component::__internal::String,), 462 >::new_unchecked(self.c) 463 }; 464 let (ret0,) = callee 465 .call_async(store.as_context_mut(), (arg0, arg1)) 466 .instrument(span.clone()) 467 .await?; 468 callee 469 .post_return_async(store.as_context_mut()) 470 .instrument(span) 471 .await?; 472 Ok(ret0) 473 } 474 } 475 } 476 } 477 } 478 } 479