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