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> { 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> 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. 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 = TheWorldIndices::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 [`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. instantiate( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<TheWorld>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`. instantiate_async( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<TheWorld>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::anon::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::anon::Guest, 104 } 105 const _: () = { 106 impl TheWorldIndices { 107 /// Creates a new copy of `TheWorldIndices` bindings which can then 108 /// be used to instantiate into a particular store. 109 /// 110 /// This method may fail if the component does not have the 111 /// required exports. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>112 pub fn new<_T>( 113 _instance_pre: &wasmtime::component::InstancePre<_T>, 114 ) -> wasmtime::Result<Self> { 115 let _component = _instance_pre.component(); 116 let _instance_type = _instance_pre.instance_type(); 117 let interface0 = exports::foo::foo::anon::GuestIndices::new(_instance_pre)?; 118 Ok(TheWorldIndices { interface0 }) 119 } 120 /// Uses the indices stored in `self` to load an instance 121 /// of [`TheWorld`] from the instance provided. 122 /// 123 /// Note that at this time this method will additionally 124 /// perform type-checks of all exports. load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<TheWorld>125 pub fn load( 126 &self, 127 mut store: impl wasmtime::AsContextMut, 128 instance: &wasmtime::component::Instance, 129 ) -> wasmtime::Result<TheWorld> { 130 let _ = &mut store; 131 let _instance = instance; 132 let interface0 = self.interface0.load(&mut store, &_instance)?; 133 Ok(TheWorld { interface0 }) 134 } 135 } 136 impl TheWorld { 137 /// Convenience wrapper around [`TheWorldPre::new`] and 138 /// [`TheWorldPre::instantiate`]. instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<TheWorld>139 pub fn instantiate<_T>( 140 store: impl wasmtime::AsContextMut<Data = _T>, 141 component: &wasmtime::component::Component, 142 linker: &wasmtime::component::Linker<_T>, 143 ) -> wasmtime::Result<TheWorld> { 144 let pre = linker.instantiate_pre(component)?; 145 TheWorldPre::new(pre)?.instantiate(store) 146 } 147 /// Convenience wrapper around [`TheWorldIndices::new`] and 148 /// [`TheWorldIndices::load`]. new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<TheWorld>149 pub fn new( 150 mut store: impl wasmtime::AsContextMut, 151 instance: &wasmtime::component::Instance, 152 ) -> wasmtime::Result<TheWorld> { 153 let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; 154 indices.load(&mut store, instance) 155 } 156 /// Convenience wrapper around [`TheWorldPre::new`] and 157 /// [`TheWorldPre::instantiate_async`]. instantiate_async<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<TheWorld> where _T: Send,158 pub async fn instantiate_async<_T>( 159 store: impl wasmtime::AsContextMut<Data = _T>, 160 component: &wasmtime::component::Component, 161 linker: &wasmtime::component::Linker<_T>, 162 ) -> wasmtime::Result<TheWorld> 163 where 164 _T: Send, 165 { 166 let pre = linker.instantiate_pre(component)?; 167 TheWorldPre::new(pre)?.instantiate_async(store).await 168 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: foo::foo::anon::HostWithStore + Send, for<'a> D::Data<'a>: foo::foo::anon::Host + Send, T: 'static + Send,169 pub fn add_to_linker<T, D>( 170 linker: &mut wasmtime::component::Linker<T>, 171 host_getter: fn(&mut T) -> D::Data<'_>, 172 ) -> wasmtime::Result<()> 173 where 174 D: foo::foo::anon::HostWithStore + Send, 175 for<'a> D::Data<'a>: foo::foo::anon::Host + Send, 176 T: 'static + Send, 177 { 178 foo::foo::anon::add_to_linker::<T, D>(linker, host_getter)?; 179 Ok(()) 180 } foo_foo_anon(&self) -> &exports::foo::foo::anon::Guest181 pub fn foo_foo_anon(&self) -> &exports::foo::foo::anon::Guest { 182 &self.interface0 183 } 184 } 185 }; 186 pub mod foo { 187 pub mod foo { 188 #[allow(clippy::all)] 189 pub mod anon { 190 #[allow(unused_imports)] 191 use wasmtime::component::__internal::Box; 192 #[derive(wasmtime::component::ComponentType)] 193 #[derive(wasmtime::component::Lift)] 194 #[derive(wasmtime::component::Lower)] 195 #[component(enum)] 196 #[derive(Clone, Copy, Eq, PartialEq)] 197 #[repr(u8)] 198 pub enum Error { 199 #[component(name = "success")] 200 Success, 201 #[component(name = "failure")] 202 Failure, 203 } 204 impl Error { name(&self) -> &'static str205 pub fn name(&self) -> &'static str { 206 match self { 207 Error::Success => "success", 208 Error::Failure => "failure", 209 } 210 } message(&self) -> &'static str211 pub fn message(&self) -> &'static str { 212 match self { 213 Error::Success => "", 214 Error::Failure => "", 215 } 216 } 217 } 218 impl core::fmt::Debug for Error { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result219 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 220 f.debug_struct("Error") 221 .field("code", &(*self as i32)) 222 .field("name", &self.name()) 223 .field("message", &self.message()) 224 .finish() 225 } 226 } 227 impl core::fmt::Display for Error { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result228 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 229 write!(f, "{} (error {})", self.name(), * self as i32) 230 } 231 } 232 impl core::error::Error for Error {} 233 const _: () = { 234 assert!(1 == < Error as wasmtime::component::ComponentType >::SIZE32); 235 assert!(1 == < Error as wasmtime::component::ComponentType >::ALIGN32); 236 }; 237 pub trait HostWithStore: wasmtime::component::HasData + Send {} 238 impl<_T: ?Sized> HostWithStore for _T 239 where 240 _T: wasmtime::component::HasData + Send, 241 {} 242 pub trait Host: Send { option_test( &mut self, ) -> impl ::core::future::Future< Output = Result< Option<wasmtime::component::__internal::String>, Error, >, > + Send243 fn option_test( 244 &mut self, 245 ) -> impl ::core::future::Future< 246 Output = Result< 247 Option<wasmtime::component::__internal::String>, 248 Error, 249 >, 250 > + Send; 251 } 252 impl<_T: Host + ?Sized + Send> Host for &mut _T { option_test( &mut self, ) -> impl ::core::future::Future< Output = Result< Option<wasmtime::component::__internal::String>, Error, >, > + Send253 fn option_test( 254 &mut self, 255 ) -> impl ::core::future::Future< 256 Output = Result< 257 Option<wasmtime::component::__internal::String>, 258 Error, 259 >, 260 > + Send { 261 async move { Host::option_test(*self).await } 262 } 263 } 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,264 pub fn add_to_linker<T, D>( 265 linker: &mut wasmtime::component::Linker<T>, 266 host_getter: fn(&mut T) -> D::Data<'_>, 267 ) -> wasmtime::Result<()> 268 where 269 D: HostWithStore, 270 for<'a> D::Data<'a>: Host, 271 T: 'static + Send, 272 { 273 let mut inst = linker.instance("foo:foo/anon")?; 274 inst.func_wrap_async( 275 "option-test", 276 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 277 use tracing::Instrument; 278 let span = tracing::span!( 279 tracing::Level::TRACE, "wit-bindgen import", module = "anon", 280 function = "option-test", 281 ); 282 wasmtime::component::__internal::Box::new( 283 async move { 284 tracing::event!(tracing::Level::TRACE, "call"); 285 let host = &mut host_getter(caller.data_mut()); 286 let r = Host::option_test(host).await; 287 tracing::event!( 288 tracing::Level::TRACE, result = tracing::field::debug(& r), 289 "return" 290 ); 291 Ok((r,)) 292 } 293 .instrument(span), 294 ) 295 }, 296 )?; 297 Ok(()) 298 } 299 } 300 } 301 } 302 pub mod exports { 303 pub mod foo { 304 pub mod foo { 305 #[allow(clippy::all)] 306 pub mod anon { 307 #[allow(unused_imports)] 308 use wasmtime::component::__internal::Box; 309 #[derive(wasmtime::component::ComponentType)] 310 #[derive(wasmtime::component::Lift)] 311 #[derive(wasmtime::component::Lower)] 312 #[component(enum)] 313 #[derive(Clone, Copy, Eq, PartialEq)] 314 #[repr(u8)] 315 pub enum Error { 316 #[component(name = "success")] 317 Success, 318 #[component(name = "failure")] 319 Failure, 320 } 321 impl Error { name(&self) -> &'static str322 pub fn name(&self) -> &'static str { 323 match self { 324 Error::Success => "success", 325 Error::Failure => "failure", 326 } 327 } message(&self) -> &'static str328 pub fn message(&self) -> &'static str { 329 match self { 330 Error::Success => "", 331 Error::Failure => "", 332 } 333 } 334 } 335 impl core::fmt::Debug for Error { fmt( &self, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result336 fn fmt( 337 &self, 338 f: &mut core::fmt::Formatter<'_>, 339 ) -> core::fmt::Result { 340 f.debug_struct("Error") 341 .field("code", &(*self as i32)) 342 .field("name", &self.name()) 343 .field("message", &self.message()) 344 .finish() 345 } 346 } 347 impl core::fmt::Display for Error { fmt( &self, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result348 fn fmt( 349 &self, 350 f: &mut core::fmt::Formatter<'_>, 351 ) -> core::fmt::Result { 352 write!(f, "{} (error {})", self.name(), * self as i32) 353 } 354 } 355 impl core::error::Error for Error {} 356 const _: () = { 357 assert!( 358 1 == < Error as wasmtime::component::ComponentType >::SIZE32 359 ); 360 assert!( 361 1 == < Error as wasmtime::component::ComponentType >::ALIGN32 362 ); 363 }; 364 #[derive(Clone)] 365 pub struct Guest { 366 option_test: wasmtime::component::Func, 367 } 368 #[derive(Clone)] 369 pub struct GuestIndices { 370 option_test: wasmtime::component::ComponentExportIndex, 371 } 372 impl GuestIndices { 373 /// Constructor for [`GuestIndices`] which takes a 374 /// [`Component`](wasmtime::component::Component) as input and can be executed 375 /// before instantiation. 376 /// 377 /// This constructor can be used to front-load string lookups to find exports 378 /// within a component. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<GuestIndices>379 pub fn new<_T>( 380 _instance_pre: &wasmtime::component::InstancePre<_T>, 381 ) -> wasmtime::Result<GuestIndices> { 382 let instance = _instance_pre 383 .component() 384 .get_export_index(None, "foo:foo/anon") 385 .ok_or_else(|| { 386 wasmtime::format_err!( 387 "no exported instance named `foo:foo/anon`" 388 ) 389 })?; 390 let mut lookup = move |name| { 391 _instance_pre 392 .component() 393 .get_export_index(Some(&instance), name) 394 .ok_or_else(|| { 395 wasmtime::format_err!( 396 "instance export `foo:foo/anon` does \ 397 not have export `{name}`" 398 ) 399 }) 400 }; 401 let _ = &mut lookup; 402 let option_test = lookup("option-test")?; 403 Ok(GuestIndices { option_test }) 404 } load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Guest>405 pub fn load( 406 &self, 407 mut store: impl wasmtime::AsContextMut, 408 instance: &wasmtime::component::Instance, 409 ) -> wasmtime::Result<Guest> { 410 let _instance = instance; 411 let _instance_pre = _instance.instance_pre(&store); 412 let _instance_type = _instance_pre.instance_type(); 413 let mut store = store.as_context_mut(); 414 let _ = &mut store; 415 let option_test = *_instance 416 .get_typed_func::< 417 (), 418 ( 419 Result< 420 Option<wasmtime::component::__internal::String>, 421 Error, 422 >, 423 ), 424 >(&mut store, &self.option_test)? 425 .func(); 426 Ok(Guest { option_test }) 427 } 428 } 429 impl Guest { call_option_test<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result< Result<Option<wasmtime::component::__internal::String>, Error>, > where <S as wasmtime::AsContext>::Data: Send,430 pub async fn call_option_test<S: wasmtime::AsContextMut>( 431 &self, 432 mut store: S, 433 ) -> wasmtime::Result< 434 Result<Option<wasmtime::component::__internal::String>, Error>, 435 > 436 where 437 <S as wasmtime::AsContext>::Data: Send, 438 { 439 use tracing::Instrument; 440 let span = tracing::span!( 441 tracing::Level::TRACE, "wit-bindgen export", module = 442 "foo:foo/anon", function = "option-test", 443 ); 444 let callee = unsafe { 445 wasmtime::component::TypedFunc::< 446 (), 447 ( 448 Result< 449 Option<wasmtime::component::__internal::String>, 450 Error, 451 >, 452 ), 453 >::new_unchecked(self.option_test) 454 }; 455 let (ret0,) = callee 456 .call_async(store.as_context_mut(), ()) 457 .instrument(span.clone()) 458 .await?; 459 Ok(ret0) 460 } 461 } 462 } 463 } 464 } 465 } 466