1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// component which implements the world `d`. 3 /// 4 /// This structure is created through [`DPre::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 [`D`] as well. 9 pub struct DPre<T: 'static> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: DIndices, 12 } 13 impl<T: 'static> Clone for DPre<T> { clone(&self) -> Self14 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> DPre<_T> { 22 /// Creates a new copy of `DPre` 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. new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>27 pub fn new( 28 instance_pre: wasmtime::component::InstancePre<_T>, 29 ) -> wasmtime::Result<Self> { 30 let indices = DIndices::new(&instance_pre)?; 31 Ok(Self { instance_pre, indices }) 32 } engine(&self) -> &wasmtime::Engine33 pub fn engine(&self) -> &wasmtime::Engine { 34 self.instance_pre.engine() 35 } instance_pre(&self) -> &wasmtime::component::InstancePre<_T>36 pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 37 &self.instance_pre 38 } 39 /// Instantiates a new instance of [`D`] 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. instantiate( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<D>46 pub fn instantiate( 47 &self, 48 mut store: impl wasmtime::AsContextMut<Data = _T>, 49 ) -> wasmtime::Result<D> { 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> DPre<_T> { 56 /// Same as [`Self::instantiate`], except with `async`. instantiate_async( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<D>57 pub async fn instantiate_async( 58 &self, 59 mut store: impl wasmtime::AsContextMut<Data = _T>, 60 ) -> wasmtime::Result<D> { 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 /// `d`. 68 /// 69 /// This is an implementation detail of [`DPre`] and can 70 /// be constructed if needed as well. 71 /// 72 /// For more information see [`D`] as well. 73 #[derive(Clone)] 74 pub struct DIndices {} 75 /// Auto-generated bindings for an instance a component which 76 /// implements the world `d`. 77 /// 78 /// This structure can be created through a number of means 79 /// depending on your requirements and what you have on hand: 80 /// 81 /// * The most convenient way is to use 82 /// [`D::instantiate`] which only needs a 83 /// [`Store`], [`Component`], and [`Linker`]. 84 /// 85 /// * Alternatively you can create a [`DPre`] ahead of 86 /// time with a [`Component`] to front-load string lookups 87 /// of exports once instead of per-instantiation. This 88 /// method then uses [`DPre::instantiate`] to 89 /// create a [`D`]. 90 /// 91 /// * If you've instantiated the instance yourself already 92 /// then you can use [`D::new`]. 93 /// 94 /// These methods are all equivalent to one another and move 95 /// around the tradeoff of what work is performed when. 96 /// 97 /// [`Store`]: wasmtime::Store 98 /// [`Component`]: wasmtime::component::Component 99 /// [`Linker`]: wasmtime::component::Linker 100 pub struct D {} 101 const _: () = { 102 impl DIndices { 103 /// Creates a new copy of `DIndices` 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. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>108 pub fn new<_T>( 109 _instance_pre: &wasmtime::component::InstancePre<_T>, 110 ) -> wasmtime::Result<Self> { 111 let _component = _instance_pre.component(); 112 let _instance_type = _instance_pre.instance_type(); 113 Ok(DIndices {}) 114 } 115 /// Uses the indices stored in `self` to load an instance 116 /// of [`D`] from the instance provided. 117 /// 118 /// Note that at this time this method will additionally 119 /// perform type-checks of all exports. load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<D>120 pub fn load( 121 &self, 122 mut store: impl wasmtime::AsContextMut, 123 instance: &wasmtime::component::Instance, 124 ) -> wasmtime::Result<D> { 125 let _ = &mut store; 126 let _instance = instance; 127 Ok(D {}) 128 } 129 } 130 impl D { 131 /// Convenience wrapper around [`DPre::new`] and 132 /// [`DPre::instantiate`]. instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<D>133 pub fn instantiate<_T>( 134 store: impl wasmtime::AsContextMut<Data = _T>, 135 component: &wasmtime::component::Component, 136 linker: &wasmtime::component::Linker<_T>, 137 ) -> wasmtime::Result<D> { 138 let pre = linker.instantiate_pre(component)?; 139 DPre::new(pre)?.instantiate(store) 140 } 141 /// Convenience wrapper around [`DIndices::new`] and 142 /// [`DIndices::load`]. new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<D>143 pub fn new( 144 mut store: impl wasmtime::AsContextMut, 145 instance: &wasmtime::component::Instance, 146 ) -> wasmtime::Result<D> { 147 let indices = DIndices::new(&instance.instance_pre(&store))?; 148 indices.load(&mut store, instance) 149 } 150 /// Convenience wrapper around [`DPre::new`] and 151 /// [`DPre::instantiate_async`]. instantiate_async<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<D> where _T: Send,152 pub async fn instantiate_async<_T>( 153 store: impl wasmtime::AsContextMut<Data = _T>, 154 component: &wasmtime::component::Component, 155 linker: &wasmtime::component::Linker<_T>, 156 ) -> wasmtime::Result<D> 157 where 158 _T: Send, 159 { 160 let pre = linker.instantiate_pre(component)?; 161 DPre::new(pre)?.instantiate_async(store).await 162 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: foo::foo::a::HostWithStore + foo::foo::b::HostWithStore + foo::foo::c::HostWithStore + d::HostWithStore + Send, for<'a> D::Data< 'a, >: foo::foo::a::Host + foo::foo::b::Host + foo::foo::c::Host + d::Host + Send, T: 'static + Send,163 pub fn add_to_linker<T, D>( 164 linker: &mut wasmtime::component::Linker<T>, 165 host_getter: fn(&mut T) -> D::Data<'_>, 166 ) -> wasmtime::Result<()> 167 where 168 D: foo::foo::a::HostWithStore + foo::foo::b::HostWithStore 169 + foo::foo::c::HostWithStore + d::HostWithStore + Send, 170 for<'a> D::Data< 171 'a, 172 >: foo::foo::a::Host + foo::foo::b::Host + foo::foo::c::Host + d::Host 173 + Send, 174 T: 'static + Send, 175 { 176 foo::foo::a::add_to_linker::<T, D>(linker, host_getter)?; 177 foo::foo::b::add_to_linker::<T, D>(linker, host_getter)?; 178 foo::foo::c::add_to_linker::<T, D>(linker, host_getter)?; 179 d::add_to_linker::<T, D>(linker, host_getter)?; 180 Ok(()) 181 } 182 } 183 }; 184 pub mod foo { 185 pub mod foo { 186 #[allow(clippy::all)] 187 pub mod a { 188 #[allow(unused_imports)] 189 use wasmtime::component::__internal::Box; 190 #[derive(wasmtime::component::ComponentType)] 191 #[derive(wasmtime::component::Lift)] 192 #[derive(wasmtime::component::Lower)] 193 #[component(record)] 194 #[derive(Clone, Copy)] 195 pub struct Foo {} 196 impl core::fmt::Debug for Foo { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result197 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 198 f.debug_struct("Foo").finish() 199 } 200 } 201 const _: () = { 202 assert!(0 == < Foo as wasmtime::component::ComponentType >::SIZE32); 203 assert!(1 == < Foo as wasmtime::component::ComponentType >::ALIGN32); 204 }; 205 pub trait HostWithStore: wasmtime::component::HasData + Send {} 206 impl<_T: ?Sized> HostWithStore for _T 207 where 208 _T: wasmtime::component::HasData + Send, 209 {} 210 pub trait Host: Send { a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send211 fn a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send; 212 } 213 impl<_T: Host + ?Sized + Send> Host for &mut _T { a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send214 fn a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send { 215 async move { Host::a(*self).await } 216 } 217 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static + Send,218 pub fn add_to_linker<T, D>( 219 linker: &mut wasmtime::component::Linker<T>, 220 host_getter: fn(&mut T) -> D::Data<'_>, 221 ) -> wasmtime::Result<()> 222 where 223 D: HostWithStore, 224 for<'a> D::Data<'a>: Host, 225 T: 'static + Send, 226 { 227 let mut inst = linker.instance("foo:foo/a")?; 228 inst.func_wrap_async( 229 "a", 230 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 231 use tracing::Instrument; 232 let span = tracing::span!( 233 tracing::Level::TRACE, "wit-bindgen import", module = "a", 234 function = "a", 235 ); 236 wasmtime::component::__internal::Box::new( 237 async move { 238 tracing::event!(tracing::Level::TRACE, "call"); 239 let host = &mut host_getter(caller.data_mut()); 240 let r = Host::a(host).await; 241 tracing::event!( 242 tracing::Level::TRACE, result = tracing::field::debug(& r), 243 "return" 244 ); 245 Ok((r,)) 246 } 247 .instrument(span), 248 ) 249 }, 250 )?; 251 Ok(()) 252 } 253 } 254 #[allow(clippy::all)] 255 pub mod b { 256 #[allow(unused_imports)] 257 use wasmtime::component::__internal::Box; 258 pub type Foo = super::super::super::foo::foo::a::Foo; 259 const _: () = { 260 assert!(0 == < Foo as wasmtime::component::ComponentType >::SIZE32); 261 assert!(1 == < Foo as wasmtime::component::ComponentType >::ALIGN32); 262 }; 263 pub trait HostWithStore: wasmtime::component::HasData + Send {} 264 impl<_T: ?Sized> HostWithStore for _T 265 where 266 _T: wasmtime::component::HasData + Send, 267 {} 268 pub trait Host: Send { a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send269 fn a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send; 270 } 271 impl<_T: Host + ?Sized + Send> Host for &mut _T { a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send272 fn a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send { 273 async move { Host::a(*self).await } 274 } 275 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static + Send,276 pub fn add_to_linker<T, D>( 277 linker: &mut wasmtime::component::Linker<T>, 278 host_getter: fn(&mut T) -> D::Data<'_>, 279 ) -> wasmtime::Result<()> 280 where 281 D: HostWithStore, 282 for<'a> D::Data<'a>: Host, 283 T: 'static + Send, 284 { 285 let mut inst = linker.instance("foo:foo/b")?; 286 inst.func_wrap_async( 287 "a", 288 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 289 use tracing::Instrument; 290 let span = tracing::span!( 291 tracing::Level::TRACE, "wit-bindgen import", module = "b", 292 function = "a", 293 ); 294 wasmtime::component::__internal::Box::new( 295 async move { 296 tracing::event!(tracing::Level::TRACE, "call"); 297 let host = &mut host_getter(caller.data_mut()); 298 let r = Host::a(host).await; 299 tracing::event!( 300 tracing::Level::TRACE, result = tracing::field::debug(& r), 301 "return" 302 ); 303 Ok((r,)) 304 } 305 .instrument(span), 306 ) 307 }, 308 )?; 309 Ok(()) 310 } 311 } 312 #[allow(clippy::all)] 313 pub mod c { 314 #[allow(unused_imports)] 315 use wasmtime::component::__internal::Box; 316 pub type Foo = super::super::super::foo::foo::b::Foo; 317 const _: () = { 318 assert!(0 == < Foo as wasmtime::component::ComponentType >::SIZE32); 319 assert!(1 == < Foo as wasmtime::component::ComponentType >::ALIGN32); 320 }; 321 pub trait HostWithStore: wasmtime::component::HasData + Send {} 322 impl<_T: ?Sized> HostWithStore for _T 323 where 324 _T: wasmtime::component::HasData + Send, 325 {} 326 pub trait Host: Send { a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send327 fn a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send; 328 } 329 impl<_T: Host + ?Sized + Send> Host for &mut _T { a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send330 fn a(&mut self) -> impl ::core::future::Future<Output = Foo> + Send { 331 async move { Host::a(*self).await } 332 } 333 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static + Send,334 pub fn add_to_linker<T, D>( 335 linker: &mut wasmtime::component::Linker<T>, 336 host_getter: fn(&mut T) -> D::Data<'_>, 337 ) -> wasmtime::Result<()> 338 where 339 D: HostWithStore, 340 for<'a> D::Data<'a>: Host, 341 T: 'static + Send, 342 { 343 let mut inst = linker.instance("foo:foo/c")?; 344 inst.func_wrap_async( 345 "a", 346 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 347 use tracing::Instrument; 348 let span = tracing::span!( 349 tracing::Level::TRACE, "wit-bindgen import", module = "c", 350 function = "a", 351 ); 352 wasmtime::component::__internal::Box::new( 353 async move { 354 tracing::event!(tracing::Level::TRACE, "call"); 355 let host = &mut host_getter(caller.data_mut()); 356 let r = Host::a(host).await; 357 tracing::event!( 358 tracing::Level::TRACE, result = tracing::field::debug(& r), 359 "return" 360 ); 361 Ok((r,)) 362 } 363 .instrument(span), 364 ) 365 }, 366 )?; 367 Ok(()) 368 } 369 } 370 } 371 } 372 #[allow(clippy::all)] 373 pub mod d { 374 #[allow(unused_imports)] 375 use wasmtime::component::__internal::Box; 376 pub type Foo = super::foo::foo::c::Foo; 377 const _: () = { 378 assert!(0 == < Foo as wasmtime::component::ComponentType >::SIZE32); 379 assert!(1 == < Foo as wasmtime::component::ComponentType >::ALIGN32); 380 }; 381 pub trait HostWithStore: wasmtime::component::HasData + Send {} 382 impl<_T: ?Sized> HostWithStore for _T 383 where 384 _T: wasmtime::component::HasData + Send, 385 {} 386 pub trait Host: Send { b(&mut self) -> impl ::core::future::Future<Output = Foo> + Send387 fn b(&mut self) -> impl ::core::future::Future<Output = Foo> + Send; 388 } 389 impl<_T: Host + ?Sized + Send> Host for &mut _T { b(&mut self) -> impl ::core::future::Future<Output = Foo> + Send390 fn b(&mut self) -> impl ::core::future::Future<Output = Foo> + Send { 391 async move { Host::b(*self).await } 392 } 393 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static + Send,394 pub fn add_to_linker<T, D>( 395 linker: &mut wasmtime::component::Linker<T>, 396 host_getter: fn(&mut T) -> D::Data<'_>, 397 ) -> wasmtime::Result<()> 398 where 399 D: HostWithStore, 400 for<'a> D::Data<'a>: Host, 401 T: 'static + Send, 402 { 403 let mut inst = linker.instance("d")?; 404 inst.func_wrap_async( 405 "b", 406 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 407 use tracing::Instrument; 408 let span = tracing::span!( 409 tracing::Level::TRACE, "wit-bindgen import", module = "<no module>", 410 function = "b", 411 ); 412 wasmtime::component::__internal::Box::new( 413 async move { 414 tracing::event!(tracing::Level::TRACE, "call"); 415 let host = &mut host_getter(caller.data_mut()); 416 let r = Host::b(host).await; 417 tracing::event!( 418 tracing::Level::TRACE, result = tracing::field::debug(& r), 419 "return" 420 ); 421 Ok((r,)) 422 } 423 .instrument(span), 424 ) 425 }, 426 )?; 427 Ok(()) 428 } 429 } 430