13171ef6dSAlex Crichton /// Auto-generated bindings for a pre-instantiated version of a 25393c2bfSBruce Mitchener /// component which implements the world `foo`. 33171ef6dSAlex Crichton /// 43171ef6dSAlex Crichton /// This structure is created through [`FooPre::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 [`Foo`] as well. 9f81c0dc0SAlex Crichton pub struct FooPre<T: 'static> { 103171ef6dSAlex Crichton instance_pre: wasmtime::component::InstancePre<T>, 1199d861cbSAlex Crichton indices: FooIndices, 123171ef6dSAlex Crichton } 13f81c0dc0SAlex Crichton impl<T: 'static> Clone for FooPre<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> FooPre<_T> { 223171ef6dSAlex Crichton /// Creates a new copy of `FooPre` bindings which can then 233171ef6dSAlex Crichton /// be used to instantiate into a particular store. 24eea68f59SLann /// 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>27eea68f59SLann pub fn new( 283171ef6dSAlex Crichton instance_pre: wasmtime::component::InstancePre<_T>, 29eea68f59SLann ) -> wasmtime::Result<Self> { 30bb77f602SPat Hickey let indices = FooIndices::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 [`Foo`] 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<Foo>461155d6dfSAlex Crichton pub fn instantiate( 471155d6dfSAlex Crichton &self, 481155d6dfSAlex Crichton mut store: impl wasmtime::AsContextMut<Data = _T>, 491155d6dfSAlex Crichton ) -> wasmtime::Result<Foo> { 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> FooPre<_T> { 561155d6dfSAlex Crichton /// Same as [`Self::instantiate`], except with `async`. instantiate_async( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Foo>573171ef6dSAlex Crichton pub async fn instantiate_async( 583171ef6dSAlex Crichton &self, 593171ef6dSAlex Crichton mut store: impl wasmtime::AsContextMut<Data = _T>, 601155d6dfSAlex Crichton ) -> wasmtime::Result<Foo> { 61eea68f59SLann 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 /// `foo`. 6899d861cbSAlex Crichton /// 6999d861cbSAlex Crichton /// This is an implementation detail of [`FooPre`] and can 7099d861cbSAlex Crichton /// be constructed if needed as well. 7199d861cbSAlex Crichton /// 7299d861cbSAlex Crichton /// For more information see [`Foo`] as well. 7399d861cbSAlex Crichton #[derive(Clone)] 7499d861cbSAlex Crichton pub struct FooIndices { 7599d861cbSAlex Crichton new: wasmtime::component::ComponentExportIndex, 7699d861cbSAlex Crichton } 7799d861cbSAlex Crichton /// Auto-generated bindings for an instance a component which 7899d861cbSAlex Crichton /// implements the world `foo`. 7999d861cbSAlex Crichton /// 8099d861cbSAlex Crichton /// This structure can be created through a number of means 8199d861cbSAlex Crichton /// depending on your requirements and what you have on hand: 8299d861cbSAlex Crichton /// 8399d861cbSAlex Crichton /// * The most convenient way is to use 841155d6dfSAlex Crichton /// [`Foo::instantiate`] which only needs a 8599d861cbSAlex Crichton /// [`Store`], [`Component`], and [`Linker`]. 8699d861cbSAlex Crichton /// 8799d861cbSAlex Crichton /// * Alternatively you can create a [`FooPre`] ahead of 8899d861cbSAlex Crichton /// time with a [`Component`] to front-load string lookups 8999d861cbSAlex Crichton /// of exports once instead of per-instantiation. This 901155d6dfSAlex Crichton /// method then uses [`FooPre::instantiate`] to 9199d861cbSAlex Crichton /// create a [`Foo`]. 9299d861cbSAlex Crichton /// 9399d861cbSAlex Crichton /// * If you've instantiated the instance yourself already 9499d861cbSAlex Crichton /// then you can use [`Foo::new`]. 9599d861cbSAlex Crichton /// 9699d861cbSAlex Crichton /// These methods are all equivalent to one another and move 9799d861cbSAlex Crichton /// around the tradeoff of what work is performed when. 9899d861cbSAlex Crichton /// 9999d861cbSAlex Crichton /// [`Store`]: wasmtime::Store 10099d861cbSAlex Crichton /// [`Component`]: wasmtime::component::Component 10199d861cbSAlex Crichton /// [`Linker`]: wasmtime::component::Linker 10299d861cbSAlex Crichton pub struct Foo { 10399d861cbSAlex Crichton new: wasmtime::component::Func, 10499d861cbSAlex Crichton } 10599d861cbSAlex Crichton const _: () = { 10699d861cbSAlex Crichton impl FooIndices { 10799d861cbSAlex Crichton /// Creates a new copy of `FooIndices` bindings which can then 10899d861cbSAlex Crichton /// be used to instantiate into a particular store. 10999d861cbSAlex Crichton /// 11099d861cbSAlex Crichton /// This method may fail if the component does not have the 11199d861cbSAlex Crichton /// required exports. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>112bb77f602SPat Hickey pub fn new<_T>( 113bb77f602SPat Hickey _instance_pre: &wasmtime::component::InstancePre<_T>, 11499d861cbSAlex Crichton ) -> wasmtime::Result<Self> { 115bb77f602SPat Hickey let _component = _instance_pre.component(); 116bb77f602SPat Hickey let _instance_type = _instance_pre.instance_type(); 117bb77f602SPat Hickey let new = { 118bb77f602SPat Hickey let (item, index) = _component 119bb77f602SPat Hickey .get_export(None, "new") 120*96e19700SNick Fitzgerald .ok_or_else(|| wasmtime::format_err!("no export `new` found"))?; 121bb77f602SPat Hickey match item { 122bb77f602SPat Hickey wasmtime::component::types::ComponentItem::ComponentFunc(func) => { 123*96e19700SNick Fitzgerald wasmtime::error::Context::context( 124bb77f602SPat Hickey func.typecheck::<(), ()>(&_instance_type), 125bb77f602SPat Hickey "type-checking export func `new`", 126bb77f602SPat Hickey )?; 127bb77f602SPat Hickey index 12899d861cbSAlex Crichton } 129*96e19700SNick Fitzgerald _ => Err(wasmtime::format_err!("export `new` is not a function"))?, 130bb77f602SPat Hickey } 131bb77f602SPat Hickey }; 13299d861cbSAlex Crichton Ok(FooIndices { new }) 13399d861cbSAlex Crichton } 13499d861cbSAlex Crichton /// Uses the indices stored in `self` to load an instance 13599d861cbSAlex Crichton /// of [`Foo`] from the instance provided. 13699d861cbSAlex Crichton /// 13799d861cbSAlex Crichton /// Note that at this time this method will additionally 13899d861cbSAlex Crichton /// perform type-checks of all exports. load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Foo>13999d861cbSAlex Crichton pub fn load( 14099d861cbSAlex Crichton &self, 14199d861cbSAlex Crichton mut store: impl wasmtime::AsContextMut, 14299d861cbSAlex Crichton instance: &wasmtime::component::Instance, 14399d861cbSAlex Crichton ) -> wasmtime::Result<Foo> { 144bb77f602SPat Hickey let _ = &mut store; 14599d861cbSAlex Crichton let _instance = instance; 1463171ef6dSAlex Crichton let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func(); 147eea68f59SLann Ok(Foo { new }) 148eea68f59SLann } 1493171ef6dSAlex Crichton } 1503171ef6dSAlex Crichton impl Foo { 1513171ef6dSAlex Crichton /// Convenience wrapper around [`FooPre::new`] and 1521155d6dfSAlex Crichton /// [`FooPre::instantiate`]. instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Foo>1531155d6dfSAlex Crichton pub fn instantiate<_T>( 1541155d6dfSAlex Crichton store: impl wasmtime::AsContextMut<Data = _T>, 1551155d6dfSAlex Crichton component: &wasmtime::component::Component, 1561155d6dfSAlex Crichton linker: &wasmtime::component::Linker<_T>, 1571155d6dfSAlex Crichton ) -> wasmtime::Result<Foo> { 1581155d6dfSAlex Crichton let pre = linker.instantiate_pre(component)?; 1591155d6dfSAlex Crichton FooPre::new(pre)?.instantiate(store) 1601155d6dfSAlex Crichton } 1611155d6dfSAlex Crichton /// Convenience wrapper around [`FooIndices::new`] and 1621155d6dfSAlex Crichton /// [`FooIndices::load`]. new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Foo>1631155d6dfSAlex Crichton pub fn new( 1641155d6dfSAlex Crichton mut store: impl wasmtime::AsContextMut, 1651155d6dfSAlex Crichton instance: &wasmtime::component::Instance, 1661155d6dfSAlex Crichton ) -> wasmtime::Result<Foo> { 1671155d6dfSAlex Crichton let indices = FooIndices::new(&instance.instance_pre(&store))?; 1681155d6dfSAlex Crichton indices.load(&mut store, instance) 1691155d6dfSAlex Crichton } 1701155d6dfSAlex Crichton /// Convenience wrapper around [`FooPre::new`] and 1713171ef6dSAlex Crichton /// [`FooPre::instantiate_async`]. instantiate_async<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Foo> where _T: Send,1723171ef6dSAlex Crichton pub async fn instantiate_async<_T>( 173bb77f602SPat Hickey store: impl wasmtime::AsContextMut<Data = _T>, 1743171ef6dSAlex Crichton component: &wasmtime::component::Component, 1753171ef6dSAlex Crichton linker: &wasmtime::component::Linker<_T>, 1763171ef6dSAlex Crichton ) -> wasmtime::Result<Foo> 1773171ef6dSAlex Crichton where 1783171ef6dSAlex Crichton _T: Send, 1793171ef6dSAlex Crichton { 1803171ef6dSAlex Crichton let pre = linker.instantiate_pre(component)?; 1813171ef6dSAlex Crichton FooPre::new(pre)?.instantiate_async(store).await 1823171ef6dSAlex Crichton } call_new<S: wasmtime::AsContextMut>( &self, mut store: S, ) -> wasmtime::Result<()> where <S as wasmtime::AsContext>::Data: Send,183eea68f59SLann pub async fn call_new<S: wasmtime::AsContextMut>( 184eea68f59SLann &self, 185eea68f59SLann mut store: S, 186eea68f59SLann ) -> wasmtime::Result<()> 187eea68f59SLann where 188eea68f59SLann <S as wasmtime::AsContext>::Data: Send, 189eea68f59SLann { 190eea68f59SLann let callee = unsafe { 191eea68f59SLann wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new) 192eea68f59SLann }; 193eea68f59SLann let () = callee.call_async(store.as_context_mut(), ()).await?; 194eea68f59SLann Ok(()) 195eea68f59SLann } 196eea68f59SLann } 197eea68f59SLann }; 198