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