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 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`. 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. 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. 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`]. 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`]. 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`]. 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 } 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 } 183 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 { 200 fn a( 201 &mut self, 202 x: wasmtime::component::__internal::String, 203 ) -> impl ::core::future::Future<Output = ()> + Send; 204 fn b( 205 &mut self, 206 ) -> impl ::core::future::Future< 207 Output = wasmtime::component::__internal::String, 208 > + Send; 209 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 { 218 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 } 224 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 } 231 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 } 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 wasmtime::component::__internal::Box::new(async move { 258 let host = &mut host_getter(caller.data_mut()); 259 let r = Host::a(host, arg0).await; 260 Ok(r) 261 }) 262 }, 263 )?; 264 inst.func_wrap_async( 265 "b", 266 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 267 wasmtime::component::__internal::Box::new(async move { 268 let host = &mut host_getter(caller.data_mut()); 269 let r = Host::b(host).await; 270 Ok((r,)) 271 }) 272 }, 273 )?; 274 inst.func_wrap_async( 275 "c", 276 move | 277 mut caller: wasmtime::StoreContextMut<'_, T>, 278 ( 279 arg0, 280 arg1, 281 ): ( 282 wasmtime::component::__internal::String, 283 wasmtime::component::__internal::String, 284 )| 285 { 286 wasmtime::component::__internal::Box::new(async move { 287 let host = &mut host_getter(caller.data_mut()); 288 let r = Host::c(host, arg0, arg1).await; 289 Ok((r,)) 290 }) 291 }, 292 )?; 293 Ok(()) 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::Box; 305 #[derive(Clone)] 306 pub struct Guest { 307 a: wasmtime::component::Func, 308 b: wasmtime::component::Func, 309 c: wasmtime::component::Func, 310 } 311 #[derive(Clone)] 312 pub struct GuestIndices { 313 a: wasmtime::component::ComponentExportIndex, 314 b: wasmtime::component::ComponentExportIndex, 315 c: wasmtime::component::ComponentExportIndex, 316 } 317 impl GuestIndices { 318 /// Constructor for [`GuestIndices`] which takes a 319 /// [`Component`](wasmtime::component::Component) as input and can be executed 320 /// before instantiation. 321 /// 322 /// This constructor can be used to front-load string lookups to find exports 323 /// within a component. 324 pub fn new<_T>( 325 _instance_pre: &wasmtime::component::InstancePre<_T>, 326 ) -> wasmtime::Result<GuestIndices> { 327 let instance = _instance_pre 328 .component() 329 .get_export_index(None, "foo:foo/strings") 330 .ok_or_else(|| { 331 wasmtime::format_err!( 332 "no exported instance named `foo:foo/strings`" 333 ) 334 })?; 335 let mut lookup = move |name| { 336 _instance_pre 337 .component() 338 .get_export_index(Some(&instance), name) 339 .ok_or_else(|| { 340 wasmtime::format_err!( 341 "instance export `foo:foo/strings` does \ 342 not have export `{name}`" 343 ) 344 }) 345 }; 346 let _ = &mut lookup; 347 let a = lookup("a")?; 348 let b = lookup("b")?; 349 let c = lookup("c")?; 350 Ok(GuestIndices { a, b, c }) 351 } 352 pub fn load( 353 &self, 354 mut store: impl wasmtime::AsContextMut, 355 instance: &wasmtime::component::Instance, 356 ) -> wasmtime::Result<Guest> { 357 let _instance = instance; 358 let _instance_pre = _instance.instance_pre(&store); 359 let _instance_type = _instance_pre.instance_type(); 360 let mut store = store.as_context_mut(); 361 let _ = &mut store; 362 let a = *_instance 363 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 364 .func(); 365 let b = *_instance 366 .get_typed_func::< 367 (), 368 (wasmtime::component::__internal::String,), 369 >(&mut store, &self.b)? 370 .func(); 371 let c = *_instance 372 .get_typed_func::< 373 (&str, &str), 374 (wasmtime::component::__internal::String,), 375 >(&mut store, &self.c)? 376 .func(); 377 Ok(Guest { a, b, c }) 378 } 379 } 380 impl Guest { 381 pub async fn call_a<S: wasmtime::AsContextMut>( 382 &self, 383 mut store: S, 384 arg0: &str, 385 ) -> wasmtime::Result<()> 386 where 387 <S as wasmtime::AsContext>::Data: Send, 388 { 389 let callee = unsafe { 390 wasmtime::component::TypedFunc::< 391 (&str,), 392 (), 393 >::new_unchecked(self.a) 394 }; 395 let () = callee 396 .call_async(store.as_context_mut(), (arg0,)) 397 .await?; 398 callee.post_return_async(store.as_context_mut()).await?; 399 Ok(()) 400 } 401 pub async fn call_b<S: wasmtime::AsContextMut>( 402 &self, 403 mut store: S, 404 ) -> wasmtime::Result<wasmtime::component::__internal::String> 405 where 406 <S as wasmtime::AsContext>::Data: Send, 407 { 408 let callee = unsafe { 409 wasmtime::component::TypedFunc::< 410 (), 411 (wasmtime::component::__internal::String,), 412 >::new_unchecked(self.b) 413 }; 414 let (ret0,) = callee 415 .call_async(store.as_context_mut(), ()) 416 .await?; 417 callee.post_return_async(store.as_context_mut()).await?; 418 Ok(ret0) 419 } 420 pub async fn call_c<S: wasmtime::AsContextMut>( 421 &self, 422 mut store: S, 423 arg0: &str, 424 arg1: &str, 425 ) -> wasmtime::Result<wasmtime::component::__internal::String> 426 where 427 <S as wasmtime::AsContext>::Data: Send, 428 { 429 let callee = unsafe { 430 wasmtime::component::TypedFunc::< 431 (&str, &str), 432 (wasmtime::component::__internal::String,), 433 >::new_unchecked(self.c) 434 }; 435 let (ret0,) = callee 436 .call_async(store.as_context_mut(), (arg0, arg1)) 437 .await?; 438 callee.post_return_async(store.as_context_mut()).await?; 439 Ok(ret0) 440 } 441 } 442 } 443 } 444 } 445 } 446