13171ef6dSAlex Crichton /// Auto-generated bindings for a pre-instantiated version of a 25393c2bfSBruce Mitchener /// component which implements the world `imports`. 33171ef6dSAlex Crichton /// 43171ef6dSAlex Crichton /// This structure is created through [`ImportsPre::new`] which 53171ef6dSAlex Crichton /// takes a [`InstancePre`](wasmtime::component::InstancePre) that 63171ef6dSAlex Crichton /// has been created through a [`Linker`](wasmtime::component::Linker). 799d861cbSAlex Crichton /// 899d861cbSAlex Crichton /// For more information see [`Imports`] as well. 9f81c0dc0SAlex Crichton pub struct ImportsPre<T: 'static> { 103171ef6dSAlex Crichton instance_pre: wasmtime::component::InstancePre<T>, 1199d861cbSAlex Crichton indices: ImportsIndices, 123171ef6dSAlex Crichton } 13f81c0dc0SAlex Crichton impl<T: 'static> Clone for ImportsPre<T> { clone(&self) -> Self14f471b4dcSAlex Crichton fn clone(&self) -> Self { 15f471b4dcSAlex Crichton Self { 16f471b4dcSAlex Crichton instance_pre: self.instance_pre.clone(), 1799d861cbSAlex Crichton indices: self.indices.clone(), 18f471b4dcSAlex Crichton } 19f471b4dcSAlex Crichton } 20f471b4dcSAlex Crichton } 21f81c0dc0SAlex Crichton impl<_T: 'static> ImportsPre<_T> { 223171ef6dSAlex Crichton /// Creates a new copy of `ImportsPre` bindings which can then 233171ef6dSAlex Crichton /// be used to instantiate into a particular store. 243171ef6dSAlex Crichton /// 255393c2bfSBruce Mitchener /// This method may fail if the component behind `instance_pre` 263171ef6dSAlex Crichton /// does not have the required exports. new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>273171ef6dSAlex Crichton pub fn new( 283171ef6dSAlex Crichton instance_pre: wasmtime::component::InstancePre<_T>, 293171ef6dSAlex Crichton ) -> wasmtime::Result<Self> { 30bb77f602SPat Hickey let indices = ImportsIndices::new(&instance_pre)?; 3199d861cbSAlex Crichton Ok(Self { instance_pre, indices }) 3299d861cbSAlex Crichton } engine(&self) -> &wasmtime::Engine3399d861cbSAlex Crichton pub fn engine(&self) -> &wasmtime::Engine { 3499d861cbSAlex Crichton self.instance_pre.engine() 3599d861cbSAlex Crichton } instance_pre(&self) -> &wasmtime::component::InstancePre<_T>3699d861cbSAlex Crichton pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 3799d861cbSAlex Crichton &self.instance_pre 383171ef6dSAlex Crichton } 393171ef6dSAlex Crichton /// Instantiates a new instance of [`Imports`] within the 403171ef6dSAlex Crichton /// `store` provided. 413171ef6dSAlex Crichton /// 423171ef6dSAlex Crichton /// This function will use `self` as the pre-instantiated 433171ef6dSAlex Crichton /// instance to perform instantiation. Afterwards the preloaded 443171ef6dSAlex Crichton /// indices in `self` are used to lookup all exports on the 453171ef6dSAlex Crichton /// resulting instance. instantiate( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Imports>461155d6dfSAlex Crichton pub fn instantiate( 471155d6dfSAlex Crichton &self, 481155d6dfSAlex Crichton mut store: impl wasmtime::AsContextMut<Data = _T>, 491155d6dfSAlex Crichton ) -> wasmtime::Result<Imports> { 501155d6dfSAlex Crichton let mut store = store.as_context_mut(); 511155d6dfSAlex Crichton let instance = self.instance_pre.instantiate(&mut store)?; 521155d6dfSAlex Crichton self.indices.load(&mut store, &instance) 531155d6dfSAlex Crichton } 541155d6dfSAlex Crichton } 551155d6dfSAlex Crichton impl<_T: Send + 'static> ImportsPre<_T> { 561155d6dfSAlex Crichton /// Same as [`Self::instantiate`], except with `async`. instantiate_async( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Imports>573171ef6dSAlex Crichton pub async fn instantiate_async( 583171ef6dSAlex Crichton &self, 593171ef6dSAlex Crichton mut store: impl wasmtime::AsContextMut<Data = _T>, 601155d6dfSAlex Crichton ) -> wasmtime::Result<Imports> { 613171ef6dSAlex Crichton let mut store = store.as_context_mut(); 6299d861cbSAlex Crichton let instance = self.instance_pre.instantiate_async(&mut store).await?; 6399d861cbSAlex Crichton self.indices.load(&mut store, &instance) 6499d861cbSAlex Crichton } 6599d861cbSAlex Crichton } 6699d861cbSAlex Crichton /// Auto-generated bindings for index of the exports of 6799d861cbSAlex Crichton /// `imports`. 6899d861cbSAlex Crichton /// 6999d861cbSAlex Crichton /// This is an implementation detail of [`ImportsPre`] and can 7099d861cbSAlex Crichton /// be constructed if needed as well. 7199d861cbSAlex Crichton /// 7299d861cbSAlex Crichton /// For more information see [`Imports`] as well. 7399d861cbSAlex Crichton #[derive(Clone)] 7499d861cbSAlex Crichton pub struct ImportsIndices {} 7599d861cbSAlex Crichton /// Auto-generated bindings for an instance a component which 7699d861cbSAlex Crichton /// implements the world `imports`. 7799d861cbSAlex Crichton /// 7899d861cbSAlex Crichton /// This structure can be created through a number of means 7999d861cbSAlex Crichton /// depending on your requirements and what you have on hand: 8099d861cbSAlex Crichton /// 8199d861cbSAlex Crichton /// * The most convenient way is to use 821155d6dfSAlex Crichton /// [`Imports::instantiate`] which only needs a 8399d861cbSAlex Crichton /// [`Store`], [`Component`], and [`Linker`]. 8499d861cbSAlex Crichton /// 8599d861cbSAlex Crichton /// * Alternatively you can create a [`ImportsPre`] ahead of 8699d861cbSAlex Crichton /// time with a [`Component`] to front-load string lookups 8799d861cbSAlex Crichton /// of exports once instead of per-instantiation. This 881155d6dfSAlex Crichton /// method then uses [`ImportsPre::instantiate`] to 8999d861cbSAlex Crichton /// create a [`Imports`]. 9099d861cbSAlex Crichton /// 9199d861cbSAlex Crichton /// * If you've instantiated the instance yourself already 9299d861cbSAlex Crichton /// then you can use [`Imports::new`]. 9399d861cbSAlex Crichton /// 9499d861cbSAlex Crichton /// These methods are all equivalent to one another and move 9599d861cbSAlex Crichton /// around the tradeoff of what work is performed when. 9699d861cbSAlex Crichton /// 9799d861cbSAlex Crichton /// [`Store`]: wasmtime::Store 9899d861cbSAlex Crichton /// [`Component`]: wasmtime::component::Component 9999d861cbSAlex Crichton /// [`Linker`]: wasmtime::component::Linker 10099d861cbSAlex Crichton pub struct Imports {} 10199d861cbSAlex Crichton const _: () = { 10299d861cbSAlex Crichton impl ImportsIndices { 10399d861cbSAlex Crichton /// Creates a new copy of `ImportsIndices` bindings which can then 10499d861cbSAlex Crichton /// be used to instantiate into a particular store. 10599d861cbSAlex Crichton /// 10699d861cbSAlex Crichton /// This method may fail if the component does not have the 10799d861cbSAlex Crichton /// required exports. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>108bb77f602SPat Hickey pub fn new<_T>( 109bb77f602SPat Hickey _instance_pre: &wasmtime::component::InstancePre<_T>, 11099d861cbSAlex Crichton ) -> wasmtime::Result<Self> { 111bb77f602SPat Hickey let _component = _instance_pre.component(); 112bb77f602SPat Hickey let _instance_type = _instance_pre.instance_type(); 11399d861cbSAlex Crichton Ok(ImportsIndices {}) 11499d861cbSAlex Crichton } 11599d861cbSAlex Crichton /// Uses the indices stored in `self` to load an instance 11699d861cbSAlex Crichton /// of [`Imports`] from the instance provided. 11799d861cbSAlex Crichton /// 11899d861cbSAlex Crichton /// Note that at this time this method will additionally 11999d861cbSAlex Crichton /// perform type-checks of all exports. load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Imports>12099d861cbSAlex Crichton pub fn load( 12199d861cbSAlex Crichton &self, 12299d861cbSAlex Crichton mut store: impl wasmtime::AsContextMut, 12399d861cbSAlex Crichton instance: &wasmtime::component::Instance, 12499d861cbSAlex Crichton ) -> wasmtime::Result<Imports> { 125bb77f602SPat Hickey let _ = &mut store; 12699d861cbSAlex Crichton let _instance = instance; 1273171ef6dSAlex Crichton Ok(Imports {}) 1283171ef6dSAlex Crichton } 1293171ef6dSAlex Crichton } 130eea68f59SLann impl Imports { 1313171ef6dSAlex Crichton /// Convenience wrapper around [`ImportsPre::new`] and 1321155d6dfSAlex Crichton /// [`ImportsPre::instantiate`]. instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Imports>1331155d6dfSAlex Crichton pub fn instantiate<_T>( 1341155d6dfSAlex Crichton store: impl wasmtime::AsContextMut<Data = _T>, 1351155d6dfSAlex Crichton component: &wasmtime::component::Component, 1361155d6dfSAlex Crichton linker: &wasmtime::component::Linker<_T>, 1371155d6dfSAlex Crichton ) -> wasmtime::Result<Imports> { 1381155d6dfSAlex Crichton let pre = linker.instantiate_pre(component)?; 1391155d6dfSAlex Crichton ImportsPre::new(pre)?.instantiate(store) 1401155d6dfSAlex Crichton } 1411155d6dfSAlex Crichton /// Convenience wrapper around [`ImportsIndices::new`] and 1421155d6dfSAlex Crichton /// [`ImportsIndices::load`]. new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Imports>1431155d6dfSAlex Crichton pub fn new( 1441155d6dfSAlex Crichton mut store: impl wasmtime::AsContextMut, 1451155d6dfSAlex Crichton instance: &wasmtime::component::Instance, 1461155d6dfSAlex Crichton ) -> wasmtime::Result<Imports> { 1471155d6dfSAlex Crichton let indices = ImportsIndices::new(&instance.instance_pre(&store))?; 1481155d6dfSAlex Crichton indices.load(&mut store, instance) 1491155d6dfSAlex Crichton } 1501155d6dfSAlex Crichton /// Convenience wrapper around [`ImportsPre::new`] and 1513171ef6dSAlex Crichton /// [`ImportsPre::instantiate_async`]. instantiate_async<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Imports> where _T: Send,1523171ef6dSAlex Crichton pub async fn instantiate_async<_T>( 153bb77f602SPat Hickey store: impl wasmtime::AsContextMut<Data = _T>, 1543171ef6dSAlex Crichton component: &wasmtime::component::Component, 1553171ef6dSAlex Crichton linker: &wasmtime::component::Linker<_T>, 1563171ef6dSAlex Crichton ) -> wasmtime::Result<Imports> 1573171ef6dSAlex Crichton where 1583171ef6dSAlex Crichton _T: Send, 1593171ef6dSAlex Crichton { 1603171ef6dSAlex Crichton let pre = linker.instantiate_pre(component)?; 1613171ef6dSAlex Crichton ImportsPre::new(pre)?.instantiate_async(store).await 1623171ef6dSAlex Crichton } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: a::b::interface_with_live_type::HostWithStore + a::b::interface_with_dead_type::HostWithStore + Send, for<'a> D::Data< 'a, >: a::b::interface_with_live_type::Host + a::b::interface_with_dead_type::Host + Send, T: 'static + Send,163f6775a33SAlex Crichton pub fn add_to_linker<T, D>( 164eea68f59SLann linker: &mut wasmtime::component::Linker<T>, 1657c790607SAlex Crichton host_getter: fn(&mut T) -> D::Data<'_>, 166eea68f59SLann ) -> wasmtime::Result<()> 167eea68f59SLann where 1681155d6dfSAlex Crichton D: a::b::interface_with_live_type::HostWithStore 1691155d6dfSAlex Crichton + a::b::interface_with_dead_type::HostWithStore + Send, 170f6775a33SAlex Crichton for<'a> D::Data< 171f6775a33SAlex Crichton 'a, 172f6775a33SAlex Crichton >: a::b::interface_with_live_type::Host 173eea68f59SLann + a::b::interface_with_dead_type::Host + Send, 1747c790607SAlex Crichton T: 'static + Send, 175eea68f59SLann { 1767c790607SAlex Crichton a::b::interface_with_live_type::add_to_linker::<T, D>(linker, host_getter)?; 1777c790607SAlex Crichton a::b::interface_with_dead_type::add_to_linker::<T, D>(linker, host_getter)?; 178eea68f59SLann Ok(()) 179eea68f59SLann } 180eea68f59SLann } 181eea68f59SLann }; 182eea68f59SLann pub mod a { 183eea68f59SLann pub mod b { 184eea68f59SLann #[allow(clippy::all)] 185eea68f59SLann pub mod interface_with_live_type { 186eea68f59SLann #[allow(unused_imports)] 187*96e19700SNick Fitzgerald use wasmtime::component::__internal::Box; 188eea68f59SLann #[derive(wasmtime::component::ComponentType)] 189eea68f59SLann #[derive(wasmtime::component::Lift)] 190eea68f59SLann #[derive(wasmtime::component::Lower)] 191eea68f59SLann #[component(record)] 192eea68f59SLann #[derive(Clone, Copy)] 193eea68f59SLann pub struct LiveType { 194eea68f59SLann #[component(name = "a")] 195eea68f59SLann pub a: u32, 196eea68f59SLann } 197eea68f59SLann impl core::fmt::Debug for LiveType { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result198eea68f59SLann fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 199eea68f59SLann f.debug_struct("LiveType").field("a", &self.a).finish() 200eea68f59SLann } 201eea68f59SLann } 202eea68f59SLann const _: () = { 203eea68f59SLann assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); 204eea68f59SLann assert!( 205eea68f59SLann 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 206eea68f59SLann ); 207eea68f59SLann }; 2081155d6dfSAlex Crichton pub trait HostWithStore: wasmtime::component::HasData + Send {} 2091155d6dfSAlex Crichton impl<_T: ?Sized> HostWithStore for _T 2101155d6dfSAlex Crichton where 2111155d6dfSAlex Crichton _T: wasmtime::component::HasData + Send, 2121155d6dfSAlex Crichton {} 2133cd96e17SLann pub trait Host: Send { f(&mut self) -> impl ::core::future::Future<Output = LiveType> + Send2141155d6dfSAlex Crichton fn f(&mut self) -> impl ::core::future::Future<Output = LiveType> + Send; 215eea68f59SLann } 2167c790607SAlex Crichton impl<_T: Host + ?Sized + Send> Host for &mut _T { f( &mut self, ) -> impl ::core::future::Future<Output = LiveType> + Send2171155d6dfSAlex Crichton fn f( 2181155d6dfSAlex Crichton &mut self, 2191155d6dfSAlex Crichton ) -> impl ::core::future::Future<Output = LiveType> + Send { 2201155d6dfSAlex Crichton async move { Host::f(*self).await } 2217c790607SAlex Crichton } 2227c790607SAlex Crichton } 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,223f6775a33SAlex Crichton pub fn add_to_linker<T, D>( 2243cd96e17SLann linker: &mut wasmtime::component::Linker<T>, 225f6775a33SAlex Crichton host_getter: fn(&mut T) -> D::Data<'_>, 2263cd96e17SLann ) -> wasmtime::Result<()> 2273cd96e17SLann where 2281155d6dfSAlex Crichton D: HostWithStore, 2297c790607SAlex Crichton for<'a> D::Data<'a>: Host, 2307c790607SAlex Crichton T: 'static + Send, 2313cd96e17SLann { 2323cd96e17SLann let mut inst = linker.instance("a:b/interface-with-live-type")?; 2333cd96e17SLann inst.func_wrap_async( 2343cd96e17SLann "f", 2352b29e459SSean Lynch move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 2362b29e459SSean Lynch wasmtime::component::__internal::Box::new(async move { 2373cd96e17SLann let host = &mut host_getter(caller.data_mut()); 2383cd96e17SLann let r = Host::f(host).await; 2393cd96e17SLann Ok((r,)) 2402b29e459SSean Lynch }) 2412b29e459SSean Lynch }, 2423cd96e17SLann )?; 2433cd96e17SLann Ok(()) 2443cd96e17SLann } 245eea68f59SLann } 246eea68f59SLann #[allow(clippy::all)] 247eea68f59SLann pub mod interface_with_dead_type { 248eea68f59SLann #[allow(unused_imports)] 249*96e19700SNick Fitzgerald use wasmtime::component::__internal::Box; 250af31e80dSPat Hickey pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType; 251af31e80dSPat Hickey const _: () = { 252af31e80dSPat Hickey assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); 253af31e80dSPat Hickey assert!( 254af31e80dSPat Hickey 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 255af31e80dSPat Hickey ); 256af31e80dSPat Hickey }; 257af31e80dSPat Hickey #[derive(wasmtime::component::ComponentType)] 258af31e80dSPat Hickey #[derive(wasmtime::component::Lift)] 259af31e80dSPat Hickey #[derive(wasmtime::component::Lower)] 260af31e80dSPat Hickey #[component(record)] 261af31e80dSPat Hickey #[derive(Clone, Copy)] 262af31e80dSPat Hickey pub struct DeadType { 263af31e80dSPat Hickey #[component(name = "a")] 264af31e80dSPat Hickey pub a: u32, 265af31e80dSPat Hickey } 266af31e80dSPat Hickey impl core::fmt::Debug for DeadType { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result267af31e80dSPat Hickey fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 268af31e80dSPat Hickey f.debug_struct("DeadType").field("a", &self.a).finish() 269af31e80dSPat Hickey } 270af31e80dSPat Hickey } 271af31e80dSPat Hickey const _: () = { 272af31e80dSPat Hickey assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32); 273af31e80dSPat Hickey assert!( 274af31e80dSPat Hickey 4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32 275af31e80dSPat Hickey ); 276af31e80dSPat Hickey }; 277af31e80dSPat Hickey #[derive(wasmtime::component::ComponentType)] 278af31e80dSPat Hickey #[derive(wasmtime::component::Lift)] 279af31e80dSPat Hickey #[derive(wasmtime::component::Lower)] 280af31e80dSPat Hickey #[component(variant)] 281af31e80dSPat Hickey #[derive(Clone, Copy)] 282af31e80dSPat Hickey pub enum V { 283af31e80dSPat Hickey #[component(name = "a")] 284af31e80dSPat Hickey A(LiveType), 285af31e80dSPat Hickey #[component(name = "b")] 286af31e80dSPat Hickey B(DeadType), 287af31e80dSPat Hickey } 288af31e80dSPat Hickey impl core::fmt::Debug for V { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result289af31e80dSPat Hickey fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 290af31e80dSPat Hickey match self { 291af31e80dSPat Hickey V::A(e) => f.debug_tuple("V::A").field(e).finish(), 292af31e80dSPat Hickey V::B(e) => f.debug_tuple("V::B").field(e).finish(), 293af31e80dSPat Hickey } 294af31e80dSPat Hickey } 295af31e80dSPat Hickey } 296af31e80dSPat Hickey const _: () = { 297af31e80dSPat Hickey assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32); 298af31e80dSPat Hickey assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32); 299af31e80dSPat Hickey }; 3001155d6dfSAlex Crichton pub trait HostWithStore: wasmtime::component::HasData {} 3011155d6dfSAlex Crichton impl<_T: ?Sized> HostWithStore for _T 3021155d6dfSAlex Crichton where 3031155d6dfSAlex Crichton _T: wasmtime::component::HasData, 3041155d6dfSAlex Crichton {} 3051155d6dfSAlex Crichton pub trait Host {} 3061155d6dfSAlex Crichton impl<_T: Host + ?Sized> Host for &mut _T {} 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,307f6775a33SAlex Crichton pub fn add_to_linker<T, D>( 3083cd96e17SLann linker: &mut wasmtime::component::Linker<T>, 309f6775a33SAlex Crichton host_getter: fn(&mut T) -> D::Data<'_>, 3103cd96e17SLann ) -> wasmtime::Result<()> 3113cd96e17SLann where 3121155d6dfSAlex Crichton D: HostWithStore, 3137c790607SAlex Crichton for<'a> D::Data<'a>: Host, 3141155d6dfSAlex Crichton T: 'static, 3153cd96e17SLann { 3163cd96e17SLann let mut inst = linker.instance("a:b/interface-with-dead-type")?; 3173cd96e17SLann Ok(()) 3183cd96e17SLann } 319eea68f59SLann } 320eea68f59SLann } 321eea68f59SLann } 322