1636435f1SJoel Dice /// Auto-generated bindings for a pre-instantiated version of a 2636435f1SJoel Dice /// component which implements the world `imports`. 3636435f1SJoel Dice /// 4636435f1SJoel Dice /// This structure is created through [`ImportsPre::new`] which 5636435f1SJoel Dice /// takes a [`InstancePre`](wasmtime::component::InstancePre) that 6636435f1SJoel Dice /// has been created through a [`Linker`](wasmtime::component::Linker). 7636435f1SJoel Dice /// 8636435f1SJoel Dice /// For more information see [`Imports`] as well. 9f81c0dc0SAlex Crichton pub struct ImportsPre<T: 'static> { 10636435f1SJoel Dice instance_pre: wasmtime::component::InstancePre<T>, 11636435f1SJoel Dice indices: ImportsIndices, 12636435f1SJoel Dice } 13f81c0dc0SAlex Crichton impl<T: 'static> Clone for ImportsPre<T> { clone(&self) -> Self14636435f1SJoel Dice fn clone(&self) -> Self { 15636435f1SJoel Dice Self { 16636435f1SJoel Dice instance_pre: self.instance_pre.clone(), 17636435f1SJoel Dice indices: self.indices.clone(), 18636435f1SJoel Dice } 19636435f1SJoel Dice } 20636435f1SJoel Dice } 21f81c0dc0SAlex Crichton impl<_T: 'static> ImportsPre<_T> { 22636435f1SJoel Dice /// Creates a new copy of `ImportsPre` bindings which can then 23636435f1SJoel Dice /// be used to instantiate into a particular store. 24636435f1SJoel Dice /// 25636435f1SJoel Dice /// This method may fail if the component behind `instance_pre` 26636435f1SJoel Dice /// does not have the required exports. new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>27636435f1SJoel Dice pub fn new( 28636435f1SJoel Dice instance_pre: wasmtime::component::InstancePre<_T>, 29636435f1SJoel Dice ) -> wasmtime::Result<Self> { 30bb77f602SPat Hickey let indices = ImportsIndices::new(&instance_pre)?; 31636435f1SJoel Dice Ok(Self { instance_pre, indices }) 32636435f1SJoel Dice } engine(&self) -> &wasmtime::Engine33636435f1SJoel Dice pub fn engine(&self) -> &wasmtime::Engine { 34636435f1SJoel Dice self.instance_pre.engine() 35636435f1SJoel Dice } instance_pre(&self) -> &wasmtime::component::InstancePre<_T>36636435f1SJoel Dice pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 37636435f1SJoel Dice &self.instance_pre 38636435f1SJoel Dice } 39636435f1SJoel Dice /// Instantiates a new instance of [`Imports`] within the 40636435f1SJoel Dice /// `store` provided. 41636435f1SJoel Dice /// 42636435f1SJoel Dice /// This function will use `self` as the pre-instantiated 43636435f1SJoel Dice /// instance to perform instantiation. Afterwards the preloaded 44636435f1SJoel Dice /// indices in `self` are used to lookup all exports on the 45636435f1SJoel Dice /// 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>57636435f1SJoel Dice pub async fn instantiate_async( 58636435f1SJoel Dice &self, 59636435f1SJoel Dice mut store: impl wasmtime::AsContextMut<Data = _T>, 601155d6dfSAlex Crichton ) -> wasmtime::Result<Imports> { 61636435f1SJoel Dice let mut store = store.as_context_mut(); 62636435f1SJoel Dice let instance = self.instance_pre.instantiate_async(&mut store).await?; 63636435f1SJoel Dice self.indices.load(&mut store, &instance) 64636435f1SJoel Dice } 65636435f1SJoel Dice } 66636435f1SJoel Dice /// Auto-generated bindings for index of the exports of 67636435f1SJoel Dice /// `imports`. 68636435f1SJoel Dice /// 69636435f1SJoel Dice /// This is an implementation detail of [`ImportsPre`] and can 70636435f1SJoel Dice /// be constructed if needed as well. 71636435f1SJoel Dice /// 72636435f1SJoel Dice /// For more information see [`Imports`] as well. 73636435f1SJoel Dice #[derive(Clone)] 74636435f1SJoel Dice pub struct ImportsIndices {} 75636435f1SJoel Dice /// Auto-generated bindings for an instance a component which 76636435f1SJoel Dice /// implements the world `imports`. 77636435f1SJoel Dice /// 78636435f1SJoel Dice /// This structure can be created through a number of means 79636435f1SJoel Dice /// depending on your requirements and what you have on hand: 80636435f1SJoel Dice /// 81636435f1SJoel Dice /// * The most convenient way is to use 821155d6dfSAlex Crichton /// [`Imports::instantiate`] which only needs a 83636435f1SJoel Dice /// [`Store`], [`Component`], and [`Linker`]. 84636435f1SJoel Dice /// 85636435f1SJoel Dice /// * Alternatively you can create a [`ImportsPre`] ahead of 86636435f1SJoel Dice /// time with a [`Component`] to front-load string lookups 87636435f1SJoel Dice /// of exports once instead of per-instantiation. This 881155d6dfSAlex Crichton /// method then uses [`ImportsPre::instantiate`] to 89636435f1SJoel Dice /// create a [`Imports`]. 90636435f1SJoel Dice /// 91636435f1SJoel Dice /// * If you've instantiated the instance yourself already 92636435f1SJoel Dice /// then you can use [`Imports::new`]. 93636435f1SJoel Dice /// 94636435f1SJoel Dice /// These methods are all equivalent to one another and move 95636435f1SJoel Dice /// around the tradeoff of what work is performed when. 96636435f1SJoel Dice /// 97636435f1SJoel Dice /// [`Store`]: wasmtime::Store 98636435f1SJoel Dice /// [`Component`]: wasmtime::component::Component 99636435f1SJoel Dice /// [`Linker`]: wasmtime::component::Linker 100636435f1SJoel Dice pub struct Imports {} 101636435f1SJoel Dice const _: () = { 102636435f1SJoel Dice impl ImportsIndices { 103636435f1SJoel Dice /// Creates a new copy of `ImportsIndices` bindings which can then 104636435f1SJoel Dice /// be used to instantiate into a particular store. 105636435f1SJoel Dice /// 106636435f1SJoel Dice /// This method may fail if the component does not have the 107636435f1SJoel Dice /// 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>, 110636435f1SJoel Dice ) -> wasmtime::Result<Self> { 111bb77f602SPat Hickey let _component = _instance_pre.component(); 112bb77f602SPat Hickey let _instance_type = _instance_pre.instance_type(); 113636435f1SJoel Dice Ok(ImportsIndices {}) 114636435f1SJoel Dice } 115636435f1SJoel Dice /// Uses the indices stored in `self` to load an instance 116636435f1SJoel Dice /// of [`Imports`] from the instance provided. 117636435f1SJoel Dice /// 118636435f1SJoel Dice /// Note that at this time this method will additionally 119636435f1SJoel Dice /// perform type-checks of all exports. load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Imports>120636435f1SJoel Dice pub fn load( 121636435f1SJoel Dice &self, 122636435f1SJoel Dice mut store: impl wasmtime::AsContextMut, 123636435f1SJoel Dice instance: &wasmtime::component::Instance, 124636435f1SJoel Dice ) -> wasmtime::Result<Imports> { 125bb77f602SPat Hickey let _ = &mut store; 126636435f1SJoel Dice let _instance = instance; 127636435f1SJoel Dice Ok(Imports {}) 128636435f1SJoel Dice } 129636435f1SJoel Dice } 130636435f1SJoel Dice impl Imports { 131636435f1SJoel Dice /// 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 151636435f1SJoel Dice /// [`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,152636435f1SJoel Dice pub async fn instantiate_async<_T>( 153bb77f602SPat Hickey store: impl wasmtime::AsContextMut<Data = _T>, 154636435f1SJoel Dice component: &wasmtime::component::Component, 155636435f1SJoel Dice linker: &wasmtime::component::Linker<_T>, 156636435f1SJoel Dice ) -> wasmtime::Result<Imports> 157636435f1SJoel Dice where 158beca86b0SAlex Crichton _T: Send, 159636435f1SJoel Dice { 160636435f1SJoel Dice let pre = linker.instantiate_pre(component)?; 161636435f1SJoel Dice ImportsPre::new(pre)?.instantiate_async(store).await 162636435f1SJoel Dice } 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,163beca86b0SAlex Crichton pub fn add_to_linker<T, D>( 164636435f1SJoel Dice linker: &mut wasmtime::component::Linker<T>, 165beca86b0SAlex Crichton host_getter: fn(&mut T) -> D::Data<'_>, 166636435f1SJoel Dice ) -> wasmtime::Result<()> 167636435f1SJoel Dice where 1681155d6dfSAlex Crichton D: a::b::interface_with_live_type::HostWithStore 1691155d6dfSAlex Crichton + a::b::interface_with_dead_type::HostWithStore + Send, 170beca86b0SAlex Crichton for<'a> D::Data< 171beca86b0SAlex Crichton 'a, 172beca86b0SAlex Crichton >: a::b::interface_with_live_type::Host 173beca86b0SAlex Crichton + a::b::interface_with_dead_type::Host + Send, 174beca86b0SAlex Crichton T: 'static + Send, 175636435f1SJoel Dice { 176beca86b0SAlex Crichton a::b::interface_with_live_type::add_to_linker::<T, D>(linker, host_getter)?; 177beca86b0SAlex Crichton a::b::interface_with_dead_type::add_to_linker::<T, D>(linker, host_getter)?; 178636435f1SJoel Dice Ok(()) 179636435f1SJoel Dice } 180636435f1SJoel Dice } 181636435f1SJoel Dice }; 182636435f1SJoel Dice pub mod a { 183636435f1SJoel Dice pub mod b { 184636435f1SJoel Dice #[allow(clippy::all)] 185636435f1SJoel Dice pub mod interface_with_live_type { 186636435f1SJoel Dice #[allow(unused_imports)] 18796e19700SNick Fitzgerald use wasmtime::component::__internal::Box; 188636435f1SJoel Dice #[derive(wasmtime::component::ComponentType)] 189636435f1SJoel Dice #[derive(wasmtime::component::Lift)] 190636435f1SJoel Dice #[derive(wasmtime::component::Lower)] 191636435f1SJoel Dice #[component(record)] 192636435f1SJoel Dice #[derive(Clone, Copy)] 193636435f1SJoel Dice pub struct LiveType { 194636435f1SJoel Dice #[component(name = "a")] 195636435f1SJoel Dice pub a: u32, 196636435f1SJoel Dice } 197636435f1SJoel Dice impl core::fmt::Debug for LiveType { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result198636435f1SJoel Dice fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 199636435f1SJoel Dice f.debug_struct("LiveType").field("a", &self.a).finish() 200636435f1SJoel Dice } 201636435f1SJoel Dice } 202636435f1SJoel Dice const _: () = { 203636435f1SJoel Dice assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); 204636435f1SJoel Dice assert!( 205636435f1SJoel Dice 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 206636435f1SJoel Dice ); 207636435f1SJoel Dice }; 2081155d6dfSAlex Crichton pub trait HostWithStore: wasmtime::component::HasData + Send { f<T: Send>( accessor: &wasmtime::component::Accessor<T, Self>, ) -> impl ::core::future::Future<Output = LiveType> + Send209*b856261dSJoel Dice fn f<T: Send>( 21064bc3bd9SAlex Crichton accessor: &wasmtime::component::Accessor<T, Self>, 2111155d6dfSAlex Crichton ) -> impl ::core::future::Future<Output = LiveType> + Send; 212636435f1SJoel Dice } 213beca86b0SAlex Crichton pub trait Host: Send {} 214beca86b0SAlex Crichton impl<_T: Host + ?Sized + Send> 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 + Send,215beca86b0SAlex Crichton pub fn add_to_linker<T, D>( 216636435f1SJoel Dice linker: &mut wasmtime::component::Linker<T>, 217beca86b0SAlex Crichton host_getter: fn(&mut T) -> D::Data<'_>, 218636435f1SJoel Dice ) -> wasmtime::Result<()> 219636435f1SJoel Dice where 2201155d6dfSAlex Crichton D: HostWithStore, 221beca86b0SAlex Crichton for<'a> D::Data<'a>: Host, 222beca86b0SAlex Crichton T: 'static + Send, 223636435f1SJoel Dice { 224636435f1SJoel Dice let mut inst = linker.instance("a:b/interface-with-live-type")?; 225636435f1SJoel Dice inst.func_wrap_concurrent( 226636435f1SJoel Dice "f", 22764bc3bd9SAlex Crichton move |caller: &wasmtime::component::Accessor<T>, (): ()| { 228beca86b0SAlex Crichton wasmtime::component::__internal::Box::pin(async move { 2299f47be2eSAlex Crichton let host = &caller.with_getter(host_getter); 2309f47be2eSAlex Crichton let r = <D as HostWithStore>::f(host).await; 231636435f1SJoel Dice Ok((r,)) 232636435f1SJoel Dice }) 233636435f1SJoel Dice }, 234636435f1SJoel Dice )?; 235636435f1SJoel Dice Ok(()) 236636435f1SJoel Dice } 237636435f1SJoel Dice } 238636435f1SJoel Dice #[allow(clippy::all)] 239636435f1SJoel Dice pub mod interface_with_dead_type { 240636435f1SJoel Dice #[allow(unused_imports)] 24196e19700SNick Fitzgerald use wasmtime::component::__internal::Box; 242af31e80dSPat Hickey pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType; 243af31e80dSPat Hickey const _: () = { 244af31e80dSPat Hickey assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); 245af31e80dSPat Hickey assert!( 246af31e80dSPat Hickey 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 247af31e80dSPat Hickey ); 248af31e80dSPat Hickey }; 249af31e80dSPat Hickey #[derive(wasmtime::component::ComponentType)] 250af31e80dSPat Hickey #[derive(wasmtime::component::Lift)] 251af31e80dSPat Hickey #[derive(wasmtime::component::Lower)] 252af31e80dSPat Hickey #[component(record)] 253af31e80dSPat Hickey #[derive(Clone, Copy)] 254af31e80dSPat Hickey pub struct DeadType { 255af31e80dSPat Hickey #[component(name = "a")] 256af31e80dSPat Hickey pub a: u32, 257af31e80dSPat Hickey } 258af31e80dSPat Hickey impl core::fmt::Debug for DeadType { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result259af31e80dSPat Hickey fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 260af31e80dSPat Hickey f.debug_struct("DeadType").field("a", &self.a).finish() 261af31e80dSPat Hickey } 262af31e80dSPat Hickey } 263af31e80dSPat Hickey const _: () = { 264af31e80dSPat Hickey assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32); 265af31e80dSPat Hickey assert!( 266af31e80dSPat Hickey 4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32 267af31e80dSPat Hickey ); 268af31e80dSPat Hickey }; 269af31e80dSPat Hickey #[derive(wasmtime::component::ComponentType)] 270af31e80dSPat Hickey #[derive(wasmtime::component::Lift)] 271af31e80dSPat Hickey #[derive(wasmtime::component::Lower)] 272af31e80dSPat Hickey #[component(variant)] 273af31e80dSPat Hickey #[derive(Clone, Copy)] 274af31e80dSPat Hickey pub enum V { 275af31e80dSPat Hickey #[component(name = "a")] 276af31e80dSPat Hickey A(LiveType), 277af31e80dSPat Hickey #[component(name = "b")] 278af31e80dSPat Hickey B(DeadType), 279af31e80dSPat Hickey } 280af31e80dSPat Hickey impl core::fmt::Debug for V { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result281af31e80dSPat Hickey fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 282af31e80dSPat Hickey match self { 283af31e80dSPat Hickey V::A(e) => f.debug_tuple("V::A").field(e).finish(), 284af31e80dSPat Hickey V::B(e) => f.debug_tuple("V::B").field(e).finish(), 285af31e80dSPat Hickey } 286af31e80dSPat Hickey } 287af31e80dSPat Hickey } 288af31e80dSPat Hickey const _: () = { 289af31e80dSPat Hickey assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32); 290af31e80dSPat Hickey assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32); 291af31e80dSPat Hickey }; 2921155d6dfSAlex Crichton pub trait HostWithStore: wasmtime::component::HasData {} 2931155d6dfSAlex Crichton impl<_T: ?Sized> HostWithStore for _T 2941155d6dfSAlex Crichton where 2951155d6dfSAlex Crichton _T: wasmtime::component::HasData, 2961155d6dfSAlex Crichton {} 2971155d6dfSAlex Crichton pub trait Host {} 2981155d6dfSAlex 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,299beca86b0SAlex Crichton pub fn add_to_linker<T, D>( 300636435f1SJoel Dice linker: &mut wasmtime::component::Linker<T>, 301beca86b0SAlex Crichton host_getter: fn(&mut T) -> D::Data<'_>, 302636435f1SJoel Dice ) -> wasmtime::Result<()> 303636435f1SJoel Dice where 3041155d6dfSAlex Crichton D: HostWithStore, 305beca86b0SAlex Crichton for<'a> D::Data<'a>: Host, 3061155d6dfSAlex Crichton T: 'static, 307636435f1SJoel Dice { 308636435f1SJoel Dice let mut inst = linker.instance("a:b/interface-with-dead-type")?; 309636435f1SJoel Dice Ok(()) 310636435f1SJoel Dice } 311636435f1SJoel Dice } 312636435f1SJoel Dice } 313636435f1SJoel Dice } 314