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 wasmtime::component::__internal::Box::new(async move { 220 let host = &mut host_getter(caller.data_mut()); 221 let r = Host::a(host, arg0).await; 222 Ok(r) 223 }) 224 }, 225 )?; 226 inst.func_wrap_async( 227 "b", 228 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 229 wasmtime::component::__internal::Box::new(async move { 230 let host = &mut host_getter(caller.data_mut()); 231 let r = Host::b(host).await; 232 Ok((r,)) 233 }) 234 }, 235 )?; 236 inst.func_wrap_async( 237 "c", 238 move | 239 mut caller: wasmtime::StoreContextMut<'_, T>, 240 ( 241 arg0, 242 arg1, 243 ): ( 244 wasmtime::component::__internal::String, 245 wasmtime::component::__internal::String, 246 )| 247 { 248 wasmtime::component::__internal::Box::new(async move { 249 let host = &mut host_getter(caller.data_mut()); 250 let r = Host::c(host, arg0, arg1).await; 251 Ok((r,)) 252 }) 253 }, 254 )?; 255 Ok(()) 256 } 257 } 258 } 259 } 260 pub mod exports { 261 pub mod foo { 262 pub mod foo { 263 #[allow(clippy::all)] 264 pub mod strings { 265 #[allow(unused_imports)] 266 use wasmtime::component::__internal::{anyhow, Box}; 267 pub struct Guest { 268 a: wasmtime::component::Func, 269 b: wasmtime::component::Func, 270 c: wasmtime::component::Func, 271 } 272 #[derive(Clone)] 273 pub struct GuestIndices { 274 a: wasmtime::component::ComponentExportIndex, 275 b: wasmtime::component::ComponentExportIndex, 276 c: wasmtime::component::ComponentExportIndex, 277 } 278 impl GuestIndices { 279 /// Constructor for [`GuestIndices`] which takes a 280 /// [`Component`](wasmtime::component::Component) as input and can be executed 281 /// before instantiation. 282 /// 283 /// This constructor can be used to front-load string lookups to find exports 284 /// within a component. 285 pub fn new<_T>( 286 _instance_pre: &wasmtime::component::InstancePre<_T>, 287 ) -> wasmtime::Result<GuestIndices> { 288 let instance = _instance_pre 289 .component() 290 .get_export_index(None, "foo:foo/strings") 291 .ok_or_else(|| { 292 anyhow::anyhow!( 293 "no exported instance named `foo:foo/strings`" 294 ) 295 })?; 296 let mut lookup = move |name| { 297 _instance_pre 298 .component() 299 .get_export_index(Some(&instance), name) 300 .ok_or_else(|| { 301 anyhow::anyhow!( 302 "instance export `foo:foo/strings` does \ 303 not have export `{name}`" 304 ) 305 }) 306 }; 307 let _ = &mut lookup; 308 let a = lookup("a")?; 309 let b = lookup("b")?; 310 let c = lookup("c")?; 311 Ok(GuestIndices { a, b, c }) 312 } 313 pub fn load( 314 &self, 315 mut store: impl wasmtime::AsContextMut, 316 instance: &wasmtime::component::Instance, 317 ) -> wasmtime::Result<Guest> { 318 let _instance = instance; 319 let _instance_pre = _instance.instance_pre(&store); 320 let _instance_type = _instance_pre.instance_type(); 321 let mut store = store.as_context_mut(); 322 let _ = &mut store; 323 let a = *_instance 324 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 325 .func(); 326 let b = *_instance 327 .get_typed_func::< 328 (), 329 (wasmtime::component::__internal::String,), 330 >(&mut store, &self.b)? 331 .func(); 332 let c = *_instance 333 .get_typed_func::< 334 (&str, &str), 335 (wasmtime::component::__internal::String,), 336 >(&mut store, &self.c)? 337 .func(); 338 Ok(Guest { a, b, c }) 339 } 340 } 341 impl Guest { 342 pub async fn call_a<S: wasmtime::AsContextMut>( 343 &self, 344 mut store: S, 345 arg0: &str, 346 ) -> wasmtime::Result<()> 347 where 348 <S as wasmtime::AsContext>::Data: Send, 349 { 350 let callee = unsafe { 351 wasmtime::component::TypedFunc::< 352 (&str,), 353 (), 354 >::new_unchecked(self.a) 355 }; 356 let () = callee 357 .call_async(store.as_context_mut(), (arg0,)) 358 .await?; 359 callee.post_return_async(store.as_context_mut()).await?; 360 Ok(()) 361 } 362 pub async fn call_b<S: wasmtime::AsContextMut>( 363 &self, 364 mut store: S, 365 ) -> wasmtime::Result<wasmtime::component::__internal::String> 366 where 367 <S as wasmtime::AsContext>::Data: Send, 368 { 369 let callee = unsafe { 370 wasmtime::component::TypedFunc::< 371 (), 372 (wasmtime::component::__internal::String,), 373 >::new_unchecked(self.b) 374 }; 375 let (ret0,) = callee 376 .call_async(store.as_context_mut(), ()) 377 .await?; 378 callee.post_return_async(store.as_context_mut()).await?; 379 Ok(ret0) 380 } 381 pub async fn call_c<S: wasmtime::AsContextMut>( 382 &self, 383 mut store: S, 384 arg0: &str, 385 arg1: &str, 386 ) -> wasmtime::Result<wasmtime::component::__internal::String> 387 where 388 <S as wasmtime::AsContext>::Data: Send, 389 { 390 let callee = unsafe { 391 wasmtime::component::TypedFunc::< 392 (&str, &str), 393 (wasmtime::component::__internal::String,), 394 >::new_unchecked(self.c) 395 }; 396 let (ret0,) = callee 397 .call_async(store.as_context_mut(), (arg0, arg1)) 398 .await?; 399 callee.post_return_async(store.as_context_mut()).await?; 400 Ok(ret0) 401 } 402 } 403 } 404 } 405 } 406 } 407