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