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 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 /// Auto-generated bindings for index of the exports of 56 /// `the-world`. 57 /// 58 /// This is an implementation detail of [`TheWorldPre`] and can 59 /// be constructed if needed as well. 60 /// 61 /// For more information see [`TheWorld`] as well. 62 #[derive(Clone)] 63 pub struct TheWorldIndices { 64 interface0: exports::foo::foo::strings::GuestIndices, 65 } 66 /// Auto-generated bindings for an instance a component which 67 /// implements the world `the-world`. 68 /// 69 /// This structure can be created through a number of means 70 /// depending on your requirements and what you have on hand: 71 /// 72 /// * The most convenient way is to use 73 /// [`TheWorld::instantiate`] which only needs a 74 /// [`Store`], [`Component`], and [`Linker`]. 75 /// 76 /// * Alternatively you can create a [`TheWorldPre`] ahead of 77 /// time with a [`Component`] to front-load string lookups 78 /// of exports once instead of per-instantiation. This 79 /// method then uses [`TheWorldPre::instantiate`] to 80 /// create a [`TheWorld`]. 81 /// 82 /// * If you've instantiated the instance yourself already 83 /// then you can use [`TheWorld::new`]. 84 /// 85 /// * You can also access the guts of instantiation through 86 /// [`TheWorldIndices::new_instance`] followed 87 /// by [`TheWorldIndices::load`] to crate an instance of this 88 /// type. 89 /// 90 /// These methods are all equivalent to one another and move 91 /// around the tradeoff of what work is performed when. 92 /// 93 /// [`Store`]: wasmtime::Store 94 /// [`Component`]: wasmtime::component::Component 95 /// [`Linker`]: wasmtime::component::Linker 96 pub struct TheWorld { 97 interface0: exports::foo::foo::strings::Guest, 98 } 99 const _: () = { 100 #[allow(unused_imports)] 101 use wasmtime::component::__internal::anyhow; 102 impl TheWorldIndices { 103 /// Creates a new copy of `TheWorldIndices` bindings which can then 104 /// be used to instantiate into a particular store. 105 /// 106 /// This method may fail if the component does not have the 107 /// required exports. 108 pub fn new( 109 component: &wasmtime::component::Component, 110 ) -> wasmtime::Result<Self> { 111 let _component = component; 112 let interface0 = exports::foo::foo::strings::GuestIndices::new(_component)?; 113 Ok(TheWorldIndices { interface0 }) 114 } 115 /// Creates a new instance of [`TheWorldIndices`] from an 116 /// instantiated component. 117 /// 118 /// This method of creating a [`TheWorld`] will perform string 119 /// lookups for all exports when this method is called. This 120 /// will only succeed if the provided instance matches the 121 /// requirements of [`TheWorld`]. 122 pub fn new_instance( 123 mut store: impl wasmtime::AsContextMut, 124 instance: &wasmtime::component::Instance, 125 ) -> wasmtime::Result<Self> { 126 let _instance = instance; 127 let interface0 = exports::foo::foo::strings::GuestIndices::new_instance( 128 &mut store, 129 _instance, 130 )?; 131 Ok(TheWorldIndices { interface0 }) 132 } 133 /// Uses the indices stored in `self` to load an instance 134 /// of [`TheWorld`] from the instance provided. 135 /// 136 /// Note that at this time this method will additionally 137 /// perform type-checks of all exports. 138 pub fn load( 139 &self, 140 mut store: impl wasmtime::AsContextMut, 141 instance: &wasmtime::component::Instance, 142 ) -> wasmtime::Result<TheWorld> { 143 let _instance = instance; 144 let interface0 = self.interface0.load(&mut store, &_instance)?; 145 Ok(TheWorld { interface0 }) 146 } 147 } 148 impl TheWorld { 149 /// Convenience wrapper around [`TheWorldPre::new`] and 150 /// [`TheWorldPre::instantiate`]. 151 pub fn instantiate<_T>( 152 mut store: impl wasmtime::AsContextMut<Data = _T>, 153 component: &wasmtime::component::Component, 154 linker: &wasmtime::component::Linker<_T>, 155 ) -> wasmtime::Result<TheWorld> { 156 let pre = linker.instantiate_pre(component)?; 157 TheWorldPre::new(pre)?.instantiate(store) 158 } 159 /// Convenience wrapper around [`TheWorldIndices::new_instance`] and 160 /// [`TheWorldIndices::load`]. 161 pub fn new( 162 mut store: impl wasmtime::AsContextMut, 163 instance: &wasmtime::component::Instance, 164 ) -> wasmtime::Result<TheWorld> { 165 let indices = TheWorldIndices::new_instance(&mut store, instance)?; 166 indices.load(store, instance) 167 } 168 pub fn add_to_linker<T, U>( 169 linker: &mut wasmtime::component::Linker<T>, 170 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 171 ) -> wasmtime::Result<()> 172 where 173 U: foo::foo::strings::Host, 174 { 175 foo::foo::strings::add_to_linker(linker, get)?; 176 Ok(()) 177 } 178 pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { 179 &self.interface0 180 } 181 } 182 }; 183 pub mod foo { 184 pub mod foo { 185 #[allow(clippy::all)] 186 pub mod strings { 187 #[allow(unused_imports)] 188 use wasmtime::component::__internal::{anyhow, Box}; 189 pub trait Host { 190 fn a(&mut self, x: wasmtime::component::__internal::String) -> (); 191 fn b(&mut self) -> wasmtime::component::__internal::String; 192 fn c( 193 &mut self, 194 a: wasmtime::component::__internal::String, 195 b: wasmtime::component::__internal::String, 196 ) -> wasmtime::component::__internal::String; 197 } 198 pub trait GetHost< 199 T, 200 D, 201 >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static { 202 type Host: Host; 203 } 204 impl<F, T, D, O> GetHost<T, D> for F 205 where 206 F: Fn(T) -> O + Send + Sync + Copy + 'static, 207 O: Host, 208 { 209 type Host = O; 210 } 211 pub fn add_to_linker_get_host< 212 T, 213 G: for<'a> GetHost<&'a mut T, T, Host: Host>, 214 >( 215 linker: &mut wasmtime::component::Linker<T>, 216 host_getter: G, 217 ) -> wasmtime::Result<()> { 218 let mut inst = linker.instance("foo:foo/strings")?; 219 inst.func_wrap( 220 "a", 221 move | 222 mut caller: wasmtime::StoreContextMut<'_, T>, 223 (arg0,): (wasmtime::component::__internal::String,)| 224 { 225 let host = &mut host_getter(caller.data_mut()); 226 let r = Host::a(host, arg0); 227 Ok(r) 228 }, 229 )?; 230 inst.func_wrap( 231 "b", 232 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 233 let host = &mut host_getter(caller.data_mut()); 234 let r = Host::b(host); 235 Ok((r,)) 236 }, 237 )?; 238 inst.func_wrap( 239 "c", 240 move | 241 mut caller: wasmtime::StoreContextMut<'_, T>, 242 ( 243 arg0, 244 arg1, 245 ): ( 246 wasmtime::component::__internal::String, 247 wasmtime::component::__internal::String, 248 )| 249 { 250 let host = &mut host_getter(caller.data_mut()); 251 let r = Host::c(host, arg0, arg1); 252 Ok((r,)) 253 }, 254 )?; 255 Ok(()) 256 } 257 pub fn add_to_linker<T, U>( 258 linker: &mut wasmtime::component::Linker<T>, 259 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 260 ) -> wasmtime::Result<()> 261 where 262 U: Host, 263 { 264 add_to_linker_get_host(linker, get) 265 } 266 impl<_T: Host + ?Sized> Host for &mut _T { 267 fn a(&mut self, x: wasmtime::component::__internal::String) -> () { 268 Host::a(*self, x) 269 } 270 fn b(&mut self) -> wasmtime::component::__internal::String { 271 Host::b(*self) 272 } 273 fn c( 274 &mut self, 275 a: wasmtime::component::__internal::String, 276 b: wasmtime::component::__internal::String, 277 ) -> wasmtime::component::__internal::String { 278 Host::c(*self, a, b) 279 } 280 } 281 } 282 } 283 } 284 pub mod exports { 285 pub mod foo { 286 pub mod foo { 287 #[allow(clippy::all)] 288 pub mod strings { 289 #[allow(unused_imports)] 290 use wasmtime::component::__internal::{anyhow, Box}; 291 pub struct Guest { 292 a: wasmtime::component::Func, 293 b: wasmtime::component::Func, 294 c: wasmtime::component::Func, 295 } 296 #[derive(Clone)] 297 pub struct GuestIndices { 298 a: wasmtime::component::ComponentExportIndex, 299 b: wasmtime::component::ComponentExportIndex, 300 c: wasmtime::component::ComponentExportIndex, 301 } 302 impl GuestIndices { 303 /// Constructor for [`GuestIndices`] which takes a 304 /// [`Component`](wasmtime::component::Component) as input and can be executed 305 /// before instantiation. 306 /// 307 /// This constructor can be used to front-load string lookups to find exports 308 /// within a component. 309 pub fn new( 310 component: &wasmtime::component::Component, 311 ) -> wasmtime::Result<GuestIndices> { 312 let (_, instance) = component 313 .export_index(None, "foo:foo/strings") 314 .ok_or_else(|| { 315 anyhow::anyhow!( 316 "no exported instance named `foo:foo/strings`" 317 ) 318 })?; 319 Self::_new(|name| { 320 component.export_index(Some(&instance), name).map(|p| p.1) 321 }) 322 } 323 /// This constructor is similar to [`GuestIndices::new`] except that it 324 /// performs string lookups after instantiation time. 325 pub fn new_instance( 326 mut store: impl wasmtime::AsContextMut, 327 instance: &wasmtime::component::Instance, 328 ) -> wasmtime::Result<GuestIndices> { 329 let instance_export = instance 330 .get_export(&mut store, None, "foo:foo/strings") 331 .ok_or_else(|| { 332 anyhow::anyhow!( 333 "no exported instance named `foo:foo/strings`" 334 ) 335 })?; 336 Self::_new(|name| { 337 instance.get_export(&mut store, Some(&instance_export), name) 338 }) 339 } 340 fn _new( 341 mut lookup: impl FnMut( 342 &str, 343 ) -> Option<wasmtime::component::ComponentExportIndex>, 344 ) -> wasmtime::Result<GuestIndices> { 345 let mut lookup = move |name| { 346 lookup(name) 347 .ok_or_else(|| { 348 anyhow::anyhow!( 349 "instance export `foo:foo/strings` does \ 350 not have export `{name}`" 351 ) 352 }) 353 }; 354 let _ = &mut lookup; 355 let a = lookup("a")?; 356 let b = lookup("b")?; 357 let c = lookup("c")?; 358 Ok(GuestIndices { a, b, c }) 359 } 360 pub fn load( 361 &self, 362 mut store: impl wasmtime::AsContextMut, 363 instance: &wasmtime::component::Instance, 364 ) -> wasmtime::Result<Guest> { 365 let mut store = store.as_context_mut(); 366 let _ = &mut store; 367 let _instance = instance; 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 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 let callee = unsafe { 396 wasmtime::component::TypedFunc::< 397 (&str,), 398 (), 399 >::new_unchecked(self.a) 400 }; 401 let () = callee.call(store.as_context_mut(), (arg0,))?; 402 callee.post_return(store.as_context_mut())?; 403 Ok(()) 404 } 405 pub fn call_b<S: wasmtime::AsContextMut>( 406 &self, 407 mut store: S, 408 ) -> wasmtime::Result<wasmtime::component::__internal::String> 409 where 410 <S as wasmtime::AsContext>::Data: Send, 411 { 412 let callee = unsafe { 413 wasmtime::component::TypedFunc::< 414 (), 415 (wasmtime::component::__internal::String,), 416 >::new_unchecked(self.b) 417 }; 418 let (ret0,) = callee.call(store.as_context_mut(), ())?; 419 callee.post_return(store.as_context_mut())?; 420 Ok(ret0) 421 } 422 pub fn call_c<S: wasmtime::AsContextMut>( 423 &self, 424 mut store: S, 425 arg0: &str, 426 arg1: &str, 427 ) -> wasmtime::Result<wasmtime::component::__internal::String> 428 where 429 <S as wasmtime::AsContext>::Data: Send, 430 { 431 let callee = unsafe { 432 wasmtime::component::TypedFunc::< 433 (&str, &str), 434 (wasmtime::component::__internal::String,), 435 >::new_unchecked(self.c) 436 }; 437 let (ret0,) = callee.call(store.as_context_mut(), (arg0, arg1))?; 438 callee.post_return(store.as_context_mut())?; 439 Ok(ret0) 440 } 441 } 442 } 443 } 444 } 445 } 446