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, Box}; 196 #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] 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 D, 209 >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static { 210 type Host: Host + Send; 211 } 212 impl<F, T, D, O> GetHost<T, D> for F 213 where 214 F: Fn(T) -> O + Send + Sync + Copy + 'static, 215 O: Host + Send, 216 { 217 type Host = O; 218 } 219 pub fn add_to_linker_get_host< 220 T, 221 G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>, 222 >( 223 linker: &mut wasmtime::component::Linker<T>, 224 host_getter: G, 225 ) -> wasmtime::Result<()> 226 where 227 T: Send, 228 { 229 let mut inst = linker.instance("foo:foo/strings")?; 230 inst.func_wrap_async( 231 "a", 232 move | 233 mut caller: wasmtime::StoreContextMut<'_, T>, 234 (arg0,): (wasmtime::component::__internal::String,)| 235 { 236 wasmtime::component::__internal::Box::new(async move { 237 let host = &mut host_getter(caller.data_mut()); 238 let r = Host::a(host, arg0).await; 239 Ok(r) 240 }) 241 }, 242 )?; 243 inst.func_wrap_async( 244 "b", 245 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 246 wasmtime::component::__internal::Box::new(async move { 247 let host = &mut host_getter(caller.data_mut()); 248 let r = Host::b(host).await; 249 Ok((r,)) 250 }) 251 }, 252 )?; 253 inst.func_wrap_async( 254 "c", 255 move | 256 mut caller: wasmtime::StoreContextMut<'_, T>, 257 ( 258 arg0, 259 arg1, 260 ): ( 261 wasmtime::component::__internal::String, 262 wasmtime::component::__internal::String, 263 )| 264 { 265 wasmtime::component::__internal::Box::new(async move { 266 let host = &mut host_getter(caller.data_mut()); 267 let r = Host::c(host, arg0, arg1).await; 268 Ok((r,)) 269 }) 270 }, 271 )?; 272 Ok(()) 273 } 274 pub fn add_to_linker<T, U>( 275 linker: &mut wasmtime::component::Linker<T>, 276 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 277 ) -> wasmtime::Result<()> 278 where 279 U: Host + Send, 280 T: Send, 281 { 282 add_to_linker_get_host(linker, get) 283 } 284 impl<_T: Host + ?Sized + Send> Host for &mut _T { 285 async fn a(&mut self, x: wasmtime::component::__internal::String) -> () { 286 Host::a(*self, x).await 287 } 288 async fn b(&mut self) -> wasmtime::component::__internal::String { 289 Host::b(*self).await 290 } 291 async fn c( 292 &mut self, 293 a: wasmtime::component::__internal::String, 294 b: wasmtime::component::__internal::String, 295 ) -> wasmtime::component::__internal::String { 296 Host::c(*self, a, b).await 297 } 298 } 299 } 300 } 301 } 302 pub mod exports { 303 pub mod foo { 304 pub mod foo { 305 #[allow(clippy::all)] 306 pub mod strings { 307 #[allow(unused_imports)] 308 use wasmtime::component::__internal::{anyhow, Box}; 309 pub struct Guest { 310 a: wasmtime::component::Func, 311 b: wasmtime::component::Func, 312 c: wasmtime::component::Func, 313 } 314 #[derive(Clone)] 315 pub struct GuestIndices { 316 a: wasmtime::component::ComponentExportIndex, 317 b: wasmtime::component::ComponentExportIndex, 318 c: wasmtime::component::ComponentExportIndex, 319 } 320 impl GuestIndices { 321 /// Constructor for [`GuestIndices`] which takes a 322 /// [`Component`](wasmtime::component::Component) as input and can be executed 323 /// before instantiation. 324 /// 325 /// This constructor can be used to front-load string lookups to find exports 326 /// within a component. 327 pub fn new( 328 component: &wasmtime::component::Component, 329 ) -> wasmtime::Result<GuestIndices> { 330 let (_, instance) = component 331 .export_index(None, "foo:foo/strings") 332 .ok_or_else(|| { 333 anyhow::anyhow!( 334 "no exported instance named `foo:foo/strings`" 335 ) 336 })?; 337 Self::_new(|name| { 338 component.export_index(Some(&instance), name).map(|p| p.1) 339 }) 340 } 341 /// This constructor is similar to [`GuestIndices::new`] except that it 342 /// performs string lookups after instantiation time. 343 pub fn new_instance( 344 mut store: impl wasmtime::AsContextMut, 345 instance: &wasmtime::component::Instance, 346 ) -> wasmtime::Result<GuestIndices> { 347 let instance_export = instance 348 .get_export(&mut store, None, "foo:foo/strings") 349 .ok_or_else(|| { 350 anyhow::anyhow!( 351 "no exported instance named `foo:foo/strings`" 352 ) 353 })?; 354 Self::_new(|name| { 355 instance.get_export(&mut store, Some(&instance_export), name) 356 }) 357 } 358 fn _new( 359 mut lookup: impl FnMut( 360 &str, 361 ) -> Option<wasmtime::component::ComponentExportIndex>, 362 ) -> wasmtime::Result<GuestIndices> { 363 let mut lookup = move |name| { 364 lookup(name) 365 .ok_or_else(|| { 366 anyhow::anyhow!( 367 "instance export `foo:foo/strings` does \ 368 not have export `{name}`" 369 ) 370 }) 371 }; 372 let _ = &mut lookup; 373 let a = lookup("a")?; 374 let b = lookup("b")?; 375 let c = lookup("c")?; 376 Ok(GuestIndices { a, b, c }) 377 } 378 pub fn load( 379 &self, 380 mut store: impl wasmtime::AsContextMut, 381 instance: &wasmtime::component::Instance, 382 ) -> wasmtime::Result<Guest> { 383 let mut store = store.as_context_mut(); 384 let _ = &mut store; 385 let _instance = instance; 386 let a = *_instance 387 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 388 .func(); 389 let b = *_instance 390 .get_typed_func::< 391 (), 392 (wasmtime::component::__internal::String,), 393 >(&mut store, &self.b)? 394 .func(); 395 let c = *_instance 396 .get_typed_func::< 397 (&str, &str), 398 (wasmtime::component::__internal::String,), 399 >(&mut store, &self.c)? 400 .func(); 401 Ok(Guest { a, b, c }) 402 } 403 } 404 impl Guest { 405 pub async fn call_a<S: wasmtime::AsContextMut>( 406 &self, 407 mut store: S, 408 arg0: &str, 409 ) -> wasmtime::Result<()> 410 where 411 <S as wasmtime::AsContext>::Data: Send, 412 { 413 let callee = unsafe { 414 wasmtime::component::TypedFunc::< 415 (&str,), 416 (), 417 >::new_unchecked(self.a) 418 }; 419 let () = callee 420 .call_async(store.as_context_mut(), (arg0,)) 421 .await?; 422 callee.post_return_async(store.as_context_mut()).await?; 423 Ok(()) 424 } 425 pub async fn call_b<S: wasmtime::AsContextMut>( 426 &self, 427 mut store: S, 428 ) -> wasmtime::Result<wasmtime::component::__internal::String> 429 where 430 <S as wasmtime::AsContext>::Data: Send, 431 { 432 let callee = unsafe { 433 wasmtime::component::TypedFunc::< 434 (), 435 (wasmtime::component::__internal::String,), 436 >::new_unchecked(self.b) 437 }; 438 let (ret0,) = callee 439 .call_async(store.as_context_mut(), ()) 440 .await?; 441 callee.post_return_async(store.as_context_mut()).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 let callee = unsafe { 454 wasmtime::component::TypedFunc::< 455 (&str, &str), 456 (wasmtime::component::__internal::String,), 457 >::new_unchecked(self.c) 458 }; 459 let (ret0,) = callee 460 .call_async(store.as_context_mut(), (arg0, arg1)) 461 .await?; 462 callee.post_return_async(store.as_context_mut()).await?; 463 Ok(ret0) 464 } 465 } 466 } 467 } 468 } 469 } 470