1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// component which implements the world `foo`. 3 /// 4 /// This structure is created through [`FooPre::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 [`Foo`] as well. 9 pub struct FooPre<T: 'static> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: FooIndices, 12 } 13 impl<T: 'static> Clone for FooPre<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> FooPre<_T> { 22 /// Creates a new copy of `FooPre` 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 = FooIndices::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 [`Foo`] 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<Foo> { 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> FooPre<_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<Foo> { 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 /// `foo`. 68 /// 69 /// This is an implementation detail of [`FooPre`] and can 70 /// be constructed if needed as well. 71 /// 72 /// For more information see [`Foo`] as well. 73 #[derive(Clone)] 74 pub struct FooIndices { 75 interface0: exports::my::dep0_1_0::a::GuestIndices, 76 interface1: exports::my::dep0_2_0::a::GuestIndices, 77 } 78 /// Auto-generated bindings for an instance a component which 79 /// implements the world `foo`. 80 /// 81 /// This structure can be created through a number of means 82 /// depending on your requirements and what you have on hand: 83 /// 84 /// * The most convenient way is to use 85 /// [`Foo::instantiate`] which only needs a 86 /// [`Store`], [`Component`], and [`Linker`]. 87 /// 88 /// * Alternatively you can create a [`FooPre`] ahead of 89 /// time with a [`Component`] to front-load string lookups 90 /// of exports once instead of per-instantiation. This 91 /// method then uses [`FooPre::instantiate`] to 92 /// create a [`Foo`]. 93 /// 94 /// * If you've instantiated the instance yourself already 95 /// then you can use [`Foo::new`]. 96 /// 97 /// These methods are all equivalent to one another and move 98 /// around the tradeoff of what work is performed when. 99 /// 100 /// [`Store`]: wasmtime::Store 101 /// [`Component`]: wasmtime::component::Component 102 /// [`Linker`]: wasmtime::component::Linker 103 pub struct Foo { 104 interface0: exports::my::dep0_1_0::a::Guest, 105 interface1: exports::my::dep0_2_0::a::Guest, 106 } 107 const _: () = { 108 impl FooIndices { 109 /// Creates a new copy of `FooIndices` 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::my::dep0_1_0::a::GuestIndices::new(_instance_pre)?; 120 let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_instance_pre)?; 121 Ok(FooIndices { 122 interface0, 123 interface1, 124 }) 125 } 126 /// Uses the indices stored in `self` to load an instance 127 /// of [`Foo`] from the instance provided. 128 /// 129 /// Note that at this time this method will additionally 130 /// perform type-checks of all exports. 131 pub fn load( 132 &self, 133 mut store: impl wasmtime::AsContextMut, 134 instance: &wasmtime::component::Instance, 135 ) -> wasmtime::Result<Foo> { 136 let _ = &mut store; 137 let _instance = instance; 138 let interface0 = self.interface0.load(&mut store, &_instance)?; 139 let interface1 = self.interface1.load(&mut store, &_instance)?; 140 Ok(Foo { interface0, interface1 }) 141 } 142 } 143 impl Foo { 144 /// Convenience wrapper around [`FooPre::new`] and 145 /// [`FooPre::instantiate`]. 146 pub fn instantiate<_T>( 147 store: impl wasmtime::AsContextMut<Data = _T>, 148 component: &wasmtime::component::Component, 149 linker: &wasmtime::component::Linker<_T>, 150 ) -> wasmtime::Result<Foo> { 151 let pre = linker.instantiate_pre(component)?; 152 FooPre::new(pre)?.instantiate(store) 153 } 154 /// Convenience wrapper around [`FooIndices::new`] and 155 /// [`FooIndices::load`]. 156 pub fn new( 157 mut store: impl wasmtime::AsContextMut, 158 instance: &wasmtime::component::Instance, 159 ) -> wasmtime::Result<Foo> { 160 let indices = FooIndices::new(&instance.instance_pre(&store))?; 161 indices.load(&mut store, instance) 162 } 163 /// Convenience wrapper around [`FooPre::new`] and 164 /// [`FooPre::instantiate_async`]. 165 pub async fn instantiate_async<_T>( 166 store: impl wasmtime::AsContextMut<Data = _T>, 167 component: &wasmtime::component::Component, 168 linker: &wasmtime::component::Linker<_T>, 169 ) -> wasmtime::Result<Foo> 170 where 171 _T: Send, 172 { 173 let pre = linker.instantiate_pre(component)?; 174 FooPre::new(pre)?.instantiate_async(store).await 175 } 176 pub fn add_to_linker<T, D>( 177 linker: &mut wasmtime::component::Linker<T>, 178 host_getter: fn(&mut T) -> D::Data<'_>, 179 ) -> wasmtime::Result<()> 180 where 181 D: my::dep0_1_0::a::HostWithStore + my::dep0_2_0::a::HostWithStore + Send, 182 for<'a> D::Data<'a>: my::dep0_1_0::a::Host + my::dep0_2_0::a::Host + Send, 183 T: 'static + Send, 184 { 185 my::dep0_1_0::a::add_to_linker::<T, D>(linker, host_getter)?; 186 my::dep0_2_0::a::add_to_linker::<T, D>(linker, host_getter)?; 187 Ok(()) 188 } 189 pub fn my_dep0_1_0_a(&self) -> &exports::my::dep0_1_0::a::Guest { 190 &self.interface0 191 } 192 pub fn my_dep0_2_0_a(&self) -> &exports::my::dep0_2_0::a::Guest { 193 &self.interface1 194 } 195 } 196 }; 197 pub mod my { 198 pub mod dep0_1_0 { 199 #[allow(clippy::all)] 200 pub mod a { 201 #[allow(unused_imports)] 202 use wasmtime::component::__internal::Box; 203 pub trait HostWithStore: wasmtime::component::HasData + Send { 204 fn x<T: Send>( 205 accessor: &wasmtime::component::Accessor<T, Self>, 206 ) -> impl ::core::future::Future<Output = ()> + Send; 207 } 208 pub trait Host: Send {} 209 impl<_T: Host + ?Sized + Send> Host for &mut _T {} 210 pub fn add_to_linker<T, D>( 211 linker: &mut wasmtime::component::Linker<T>, 212 host_getter: fn(&mut T) -> D::Data<'_>, 213 ) -> wasmtime::Result<()> 214 where 215 D: HostWithStore, 216 for<'a> D::Data<'a>: Host, 217 T: 'static + Send, 218 { 219 let mut inst = linker.instance("my:dep/[email protected]")?; 220 inst.func_wrap_concurrent( 221 "x", 222 move |caller: &wasmtime::component::Accessor<T>, (): ()| { 223 wasmtime::component::__internal::Box::pin(async move { 224 let host = &caller.with_getter(host_getter); 225 let r = <D as HostWithStore>::x(host).await; 226 Ok(r) 227 }) 228 }, 229 )?; 230 Ok(()) 231 } 232 } 233 } 234 pub mod dep0_2_0 { 235 #[allow(clippy::all)] 236 pub mod a { 237 #[allow(unused_imports)] 238 use wasmtime::component::__internal::Box; 239 pub trait HostWithStore: wasmtime::component::HasData + Send { 240 fn x<T: Send>( 241 accessor: &wasmtime::component::Accessor<T, Self>, 242 ) -> impl ::core::future::Future<Output = ()> + Send; 243 } 244 pub trait Host: Send {} 245 impl<_T: Host + ?Sized + Send> Host for &mut _T {} 246 pub fn add_to_linker<T, D>( 247 linker: &mut wasmtime::component::Linker<T>, 248 host_getter: fn(&mut T) -> D::Data<'_>, 249 ) -> wasmtime::Result<()> 250 where 251 D: HostWithStore, 252 for<'a> D::Data<'a>: Host, 253 T: 'static + Send, 254 { 255 let mut inst = linker.instance("my:dep/[email protected]")?; 256 inst.func_wrap_concurrent( 257 "x", 258 move |caller: &wasmtime::component::Accessor<T>, (): ()| { 259 wasmtime::component::__internal::Box::pin(async move { 260 let host = &caller.with_getter(host_getter); 261 let r = <D as HostWithStore>::x(host).await; 262 Ok(r) 263 }) 264 }, 265 )?; 266 Ok(()) 267 } 268 } 269 } 270 } 271 pub mod exports { 272 pub mod my { 273 pub mod dep0_1_0 { 274 #[allow(clippy::all)] 275 pub mod a { 276 #[allow(unused_imports)] 277 use wasmtime::component::__internal::Box; 278 #[derive(Clone)] 279 pub struct Guest { 280 x: wasmtime::component::Func, 281 } 282 #[derive(Clone)] 283 pub struct GuestIndices { 284 x: wasmtime::component::ComponentExportIndex, 285 } 286 impl GuestIndices { 287 /// Constructor for [`GuestIndices`] which takes a 288 /// [`Component`](wasmtime::component::Component) as input and can be executed 289 /// before instantiation. 290 /// 291 /// This constructor can be used to front-load string lookups to find exports 292 /// within a component. 293 pub fn new<_T>( 294 _instance_pre: &wasmtime::component::InstancePre<_T>, 295 ) -> wasmtime::Result<GuestIndices> { 296 let instance = _instance_pre 297 .component() 298 .get_export_index(None, "my:dep/[email protected]") 299 .ok_or_else(|| { 300 wasmtime::format_err!( 301 "no exported instance named `my:dep/[email protected]`" 302 ) 303 })?; 304 let mut lookup = move |name| { 305 _instance_pre 306 .component() 307 .get_export_index(Some(&instance), name) 308 .ok_or_else(|| { 309 wasmtime::format_err!( 310 "instance export `my:dep/[email protected]` does \ 311 not have export `{name}`" 312 ) 313 }) 314 }; 315 let _ = &mut lookup; 316 let x = lookup("x")?; 317 Ok(GuestIndices { x }) 318 } 319 pub fn load( 320 &self, 321 mut store: impl wasmtime::AsContextMut, 322 instance: &wasmtime::component::Instance, 323 ) -> wasmtime::Result<Guest> { 324 let _instance = instance; 325 let _instance_pre = _instance.instance_pre(&store); 326 let _instance_type = _instance_pre.instance_type(); 327 let mut store = store.as_context_mut(); 328 let _ = &mut store; 329 let x = *_instance 330 .get_typed_func::<(), ()>(&mut store, &self.x)? 331 .func(); 332 Ok(Guest { x }) 333 } 334 } 335 impl Guest { 336 pub async fn call_x<_T, _D>( 337 &self, 338 accessor: &wasmtime::component::Accessor<_T, _D>, 339 ) -> wasmtime::Result<()> 340 where 341 _T: Send, 342 _D: wasmtime::component::HasData, 343 { 344 let callee = unsafe { 345 wasmtime::component::TypedFunc::< 346 (), 347 (), 348 >::new_unchecked(self.x) 349 }; 350 let ((), _) = callee.call_concurrent(accessor, ()).await?; 351 Ok(()) 352 } 353 } 354 } 355 } 356 pub mod dep0_2_0 { 357 #[allow(clippy::all)] 358 pub mod a { 359 #[allow(unused_imports)] 360 use wasmtime::component::__internal::Box; 361 #[derive(Clone)] 362 pub struct Guest { 363 x: wasmtime::component::Func, 364 } 365 #[derive(Clone)] 366 pub struct GuestIndices { 367 x: wasmtime::component::ComponentExportIndex, 368 } 369 impl GuestIndices { 370 /// Constructor for [`GuestIndices`] which takes a 371 /// [`Component`](wasmtime::component::Component) as input and can be executed 372 /// before instantiation. 373 /// 374 /// This constructor can be used to front-load string lookups to find exports 375 /// within a component. 376 pub fn new<_T>( 377 _instance_pre: &wasmtime::component::InstancePre<_T>, 378 ) -> wasmtime::Result<GuestIndices> { 379 let instance = _instance_pre 380 .component() 381 .get_export_index(None, "my:dep/[email protected]") 382 .ok_or_else(|| { 383 wasmtime::format_err!( 384 "no exported instance named `my:dep/[email protected]`" 385 ) 386 })?; 387 let mut lookup = move |name| { 388 _instance_pre 389 .component() 390 .get_export_index(Some(&instance), name) 391 .ok_or_else(|| { 392 wasmtime::format_err!( 393 "instance export `my:dep/[email protected]` does \ 394 not have export `{name}`" 395 ) 396 }) 397 }; 398 let _ = &mut lookup; 399 let x = lookup("x")?; 400 Ok(GuestIndices { x }) 401 } 402 pub fn load( 403 &self, 404 mut store: impl wasmtime::AsContextMut, 405 instance: &wasmtime::component::Instance, 406 ) -> wasmtime::Result<Guest> { 407 let _instance = instance; 408 let _instance_pre = _instance.instance_pre(&store); 409 let _instance_type = _instance_pre.instance_type(); 410 let mut store = store.as_context_mut(); 411 let _ = &mut store; 412 let x = *_instance 413 .get_typed_func::<(), ()>(&mut store, &self.x)? 414 .func(); 415 Ok(Guest { x }) 416 } 417 } 418 impl Guest { 419 pub async fn call_x<_T, _D>( 420 &self, 421 accessor: &wasmtime::component::Accessor<_T, _D>, 422 ) -> wasmtime::Result<()> 423 where 424 _T: Send, 425 _D: wasmtime::component::HasData, 426 { 427 let callee = unsafe { 428 wasmtime::component::TypedFunc::< 429 (), 430 (), 431 >::new_unchecked(self.x) 432 }; 433 let ((), _) = callee.call_concurrent(accessor, ()).await?; 434 Ok(()) 435 } 436 } 437 } 438 } 439 } 440 } 441