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)?; 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 /// These methods are all equivalent to one another and move 86 /// around the tradeoff of what work is performed when. 87 /// 88 /// [`Store`]: wasmtime::Store 89 /// [`Component`]: wasmtime::component::Component 90 /// [`Linker`]: wasmtime::component::Linker 91 pub struct TheWorld { 92 interface0: exports::foo::foo::strings::Guest, 93 } 94 const _: () = { 95 #[allow(unused_imports)] 96 use wasmtime::component::__internal::anyhow; 97 impl TheWorldIndices { 98 /// Creates a new copy of `TheWorldIndices` bindings which can then 99 /// be used to instantiate into a particular store. 100 /// 101 /// This method may fail if the component does not have the 102 /// required exports. 103 pub fn new<_T>( 104 _instance_pre: &wasmtime::component::InstancePre<_T>, 105 ) -> wasmtime::Result<Self> { 106 let _component = _instance_pre.component(); 107 let _instance_type = _instance_pre.instance_type(); 108 let interface0 = exports::foo::foo::strings::GuestIndices::new( 109 _instance_pre, 110 )?; 111 Ok(TheWorldIndices { interface0 }) 112 } 113 /// Uses the indices stored in `self` to load an instance 114 /// of [`TheWorld`] from the instance provided. 115 /// 116 /// Note that at this time this method will additionally 117 /// perform type-checks of all exports. 118 pub fn load( 119 &self, 120 mut store: impl wasmtime::AsContextMut, 121 instance: &wasmtime::component::Instance, 122 ) -> wasmtime::Result<TheWorld> { 123 let _ = &mut store; 124 let _instance = instance; 125 let interface0 = self.interface0.load(&mut store, &_instance)?; 126 Ok(TheWorld { interface0 }) 127 } 128 } 129 impl TheWorld { 130 /// Convenience wrapper around [`TheWorldPre::new`] and 131 /// [`TheWorldPre::instantiate`]. 132 pub fn instantiate<_T>( 133 store: impl wasmtime::AsContextMut<Data = _T>, 134 component: &wasmtime::component::Component, 135 linker: &wasmtime::component::Linker<_T>, 136 ) -> wasmtime::Result<TheWorld> { 137 let pre = linker.instantiate_pre(component)?; 138 TheWorldPre::new(pre)?.instantiate(store) 139 } 140 /// Convenience wrapper around [`TheWorldIndices::new`] and 141 /// [`TheWorldIndices::load`]. 142 pub fn new( 143 mut store: impl wasmtime::AsContextMut, 144 instance: &wasmtime::component::Instance, 145 ) -> wasmtime::Result<TheWorld> { 146 let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; 147 indices.load(&mut store, instance) 148 } 149 pub fn add_to_linker<T, U>( 150 linker: &mut wasmtime::component::Linker<T>, 151 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 152 ) -> wasmtime::Result<()> 153 where 154 U: foo::foo::strings::Host, 155 { 156 foo::foo::strings::add_to_linker(linker, get)?; 157 Ok(()) 158 } 159 pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { 160 &self.interface0 161 } 162 } 163 }; 164 pub mod foo { 165 pub mod foo { 166 #[allow(clippy::all)] 167 pub mod strings { 168 #[allow(unused_imports)] 169 use wasmtime::component::__internal::{anyhow, Box}; 170 pub trait Host { 171 fn a(&mut self, x: wasmtime::component::__internal::String) -> (); 172 fn b(&mut self) -> wasmtime::component::__internal::String; 173 fn c( 174 &mut self, 175 a: wasmtime::component::__internal::String, 176 b: wasmtime::component::__internal::String, 177 ) -> wasmtime::component::__internal::String; 178 } 179 pub trait GetHost< 180 T, 181 D, 182 >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static { 183 type Host: Host; 184 } 185 impl<F, T, D, O> GetHost<T, D> for F 186 where 187 F: Fn(T) -> O + Send + Sync + Copy + 'static, 188 O: Host, 189 { 190 type Host = O; 191 } 192 pub fn add_to_linker_get_host< 193 T, 194 G: for<'a> GetHost<&'a mut T, T, Host: Host>, 195 >( 196 linker: &mut wasmtime::component::Linker<T>, 197 host_getter: G, 198 ) -> wasmtime::Result<()> { 199 let mut inst = linker.instance("foo:foo/strings")?; 200 inst.func_wrap( 201 "a", 202 move | 203 mut caller: wasmtime::StoreContextMut<'_, T>, 204 (arg0,): (wasmtime::component::__internal::String,)| 205 { 206 let host = &mut host_getter(caller.data_mut()); 207 let r = Host::a(host, arg0); 208 Ok(r) 209 }, 210 )?; 211 inst.func_wrap( 212 "b", 213 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 214 let host = &mut host_getter(caller.data_mut()); 215 let r = Host::b(host); 216 Ok((r,)) 217 }, 218 )?; 219 inst.func_wrap( 220 "c", 221 move | 222 mut caller: wasmtime::StoreContextMut<'_, T>, 223 ( 224 arg0, 225 arg1, 226 ): ( 227 wasmtime::component::__internal::String, 228 wasmtime::component::__internal::String, 229 )| 230 { 231 let host = &mut host_getter(caller.data_mut()); 232 let r = Host::c(host, arg0, arg1); 233 Ok((r,)) 234 }, 235 )?; 236 Ok(()) 237 } 238 pub fn add_to_linker<T, U>( 239 linker: &mut wasmtime::component::Linker<T>, 240 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 241 ) -> wasmtime::Result<()> 242 where 243 U: Host, 244 { 245 add_to_linker_get_host(linker, get) 246 } 247 impl<_T: Host + ?Sized> Host for &mut _T { 248 fn a(&mut self, x: wasmtime::component::__internal::String) -> () { 249 Host::a(*self, x) 250 } 251 fn b(&mut self) -> wasmtime::component::__internal::String { 252 Host::b(*self) 253 } 254 fn c( 255 &mut self, 256 a: wasmtime::component::__internal::String, 257 b: wasmtime::component::__internal::String, 258 ) -> wasmtime::component::__internal::String { 259 Host::c(*self, a, b) 260 } 261 } 262 } 263 } 264 } 265 pub mod exports { 266 pub mod foo { 267 pub mod foo { 268 #[allow(clippy::all)] 269 pub mod strings { 270 #[allow(unused_imports)] 271 use wasmtime::component::__internal::{anyhow, Box}; 272 pub struct Guest { 273 a: wasmtime::component::Func, 274 b: wasmtime::component::Func, 275 c: wasmtime::component::Func, 276 } 277 #[derive(Clone)] 278 pub struct GuestIndices { 279 a: wasmtime::component::ComponentExportIndex, 280 b: wasmtime::component::ComponentExportIndex, 281 c: wasmtime::component::ComponentExportIndex, 282 } 283 impl GuestIndices { 284 /// Constructor for [`GuestIndices`] which takes a 285 /// [`Component`](wasmtime::component::Component) as input and can be executed 286 /// before instantiation. 287 /// 288 /// This constructor can be used to front-load string lookups to find exports 289 /// within a component. 290 pub fn new<_T>( 291 _instance_pre: &wasmtime::component::InstancePre<_T>, 292 ) -> wasmtime::Result<GuestIndices> { 293 let instance = _instance_pre 294 .component() 295 .get_export_index(None, "foo:foo/strings") 296 .ok_or_else(|| { 297 anyhow::anyhow!( 298 "no exported instance named `foo:foo/strings`" 299 ) 300 })?; 301 let mut lookup = move |name| { 302 _instance_pre 303 .component() 304 .get_export_index(Some(&instance), name) 305 .ok_or_else(|| { 306 anyhow::anyhow!( 307 "instance export `foo:foo/strings` does \ 308 not have export `{name}`" 309 ) 310 }) 311 }; 312 let _ = &mut lookup; 313 let a = lookup("a")?; 314 let b = lookup("b")?; 315 let c = lookup("c")?; 316 Ok(GuestIndices { a, b, c }) 317 } 318 pub fn load( 319 &self, 320 mut store: impl wasmtime::AsContextMut, 321 instance: &wasmtime::component::Instance, 322 ) -> wasmtime::Result<Guest> { 323 let _instance = instance; 324 let _instance_pre = _instance.instance_pre(&store); 325 let _instance_type = _instance_pre.instance_type(); 326 let mut store = store.as_context_mut(); 327 let _ = &mut store; 328 let a = *_instance 329 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 330 .func(); 331 let b = *_instance 332 .get_typed_func::< 333 (), 334 (wasmtime::component::__internal::String,), 335 >(&mut store, &self.b)? 336 .func(); 337 let c = *_instance 338 .get_typed_func::< 339 (&str, &str), 340 (wasmtime::component::__internal::String,), 341 >(&mut store, &self.c)? 342 .func(); 343 Ok(Guest { a, b, c }) 344 } 345 } 346 impl Guest { 347 pub fn call_a<S: wasmtime::AsContextMut>( 348 &self, 349 mut store: S, 350 arg0: &str, 351 ) -> wasmtime::Result<()> 352 where 353 <S as wasmtime::AsContext>::Data: Send, 354 { 355 let callee = unsafe { 356 wasmtime::component::TypedFunc::< 357 (&str,), 358 (), 359 >::new_unchecked(self.a) 360 }; 361 let () = callee.call(store.as_context_mut(), (arg0,))?; 362 callee.post_return(store.as_context_mut())?; 363 Ok(()) 364 } 365 pub fn call_b<S: wasmtime::AsContextMut>( 366 &self, 367 mut store: S, 368 ) -> wasmtime::Result<wasmtime::component::__internal::String> 369 where 370 <S as wasmtime::AsContext>::Data: Send, 371 { 372 let callee = unsafe { 373 wasmtime::component::TypedFunc::< 374 (), 375 (wasmtime::component::__internal::String,), 376 >::new_unchecked(self.b) 377 }; 378 let (ret0,) = callee.call(store.as_context_mut(), ())?; 379 callee.post_return(store.as_context_mut())?; 380 Ok(ret0) 381 } 382 pub fn call_c<S: wasmtime::AsContextMut>( 383 &self, 384 mut store: S, 385 arg0: &str, 386 arg1: &str, 387 ) -> wasmtime::Result<wasmtime::component::__internal::String> 388 where 389 <S as wasmtime::AsContext>::Data: Send, 390 { 391 let callee = unsafe { 392 wasmtime::component::TypedFunc::< 393 (&str, &str), 394 (wasmtime::component::__internal::String,), 395 >::new_unchecked(self.c) 396 }; 397 let (ret0,) = callee.call(store.as_context_mut(), (arg0, arg1))?; 398 callee.post_return(store.as_context_mut())?; 399 Ok(ret0) 400 } 401 } 402 } 403 } 404 } 405 } 406