1636435f1SJoel Dice /// Auto-generated bindings for a pre-instantiated version of a 2636435f1SJoel Dice /// component which implements the world `foo`. 3636435f1SJoel Dice /// 4636435f1SJoel Dice /// This structure is created through [`FooPre::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 [`Foo`] as well. 9f81c0dc0SAlex Crichton pub struct FooPre<T: 'static> { 10636435f1SJoel Dice instance_pre: wasmtime::component::InstancePre<T>, 11636435f1SJoel Dice indices: FooIndices, 12636435f1SJoel Dice } 13f81c0dc0SAlex Crichton impl<T: 'static> Clone for FooPre<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> FooPre<_T> { 22636435f1SJoel Dice /// Creates a new copy of `FooPre` 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 = FooIndices::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 [`Foo`] 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<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>57636435f1SJoel Dice pub async fn instantiate_async( 58636435f1SJoel Dice &self, 59636435f1SJoel Dice mut store: impl wasmtime::AsContextMut<Data = _T>, 601155d6dfSAlex Crichton ) -> wasmtime::Result<Foo> { 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 /// `foo`. 68636435f1SJoel Dice /// 69636435f1SJoel Dice /// This is an implementation detail of [`FooPre`] and can 70636435f1SJoel Dice /// be constructed if needed as well. 71636435f1SJoel Dice /// 72636435f1SJoel Dice /// For more information see [`Foo`] as well. 73636435f1SJoel Dice #[derive(Clone)] 74636435f1SJoel Dice pub struct FooIndices { 75636435f1SJoel Dice new: wasmtime::component::ComponentExportIndex, 76636435f1SJoel Dice } 77636435f1SJoel Dice /// Auto-generated bindings for an instance a component which 78636435f1SJoel Dice /// implements the world `foo`. 79636435f1SJoel Dice /// 80636435f1SJoel Dice /// This structure can be created through a number of means 81636435f1SJoel Dice /// depending on your requirements and what you have on hand: 82636435f1SJoel Dice /// 83636435f1SJoel Dice /// * The most convenient way is to use 841155d6dfSAlex Crichton /// [`Foo::instantiate`] which only needs a 85636435f1SJoel Dice /// [`Store`], [`Component`], and [`Linker`]. 86636435f1SJoel Dice /// 87636435f1SJoel Dice /// * Alternatively you can create a [`FooPre`] ahead of 88636435f1SJoel Dice /// time with a [`Component`] to front-load string lookups 89636435f1SJoel Dice /// of exports once instead of per-instantiation. This 901155d6dfSAlex Crichton /// method then uses [`FooPre::instantiate`] to 91636435f1SJoel Dice /// create a [`Foo`]. 92636435f1SJoel Dice /// 93636435f1SJoel Dice /// * If you've instantiated the instance yourself already 94636435f1SJoel Dice /// then you can use [`Foo::new`]. 95636435f1SJoel Dice /// 96636435f1SJoel Dice /// These methods are all equivalent to one another and move 97636435f1SJoel Dice /// around the tradeoff of what work is performed when. 98636435f1SJoel Dice /// 99636435f1SJoel Dice /// [`Store`]: wasmtime::Store 100636435f1SJoel Dice /// [`Component`]: wasmtime::component::Component 101636435f1SJoel Dice /// [`Linker`]: wasmtime::component::Linker 102636435f1SJoel Dice pub struct Foo { 103636435f1SJoel Dice new: wasmtime::component::Func, 104636435f1SJoel Dice } 105636435f1SJoel Dice const _: () = { 106636435f1SJoel Dice impl FooIndices { 107636435f1SJoel Dice /// Creates a new copy of `FooIndices` bindings which can then 108636435f1SJoel Dice /// be used to instantiate into a particular store. 109636435f1SJoel Dice /// 110636435f1SJoel Dice /// This method may fail if the component does not have the 111636435f1SJoel Dice /// 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>, 114636435f1SJoel Dice ) -> 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") 12096e19700SNick Fitzgerald .ok_or_else(|| wasmtime::format_err!("no export `new` found"))?; 121bb77f602SPat Hickey match item { 122bb77f602SPat Hickey wasmtime::component::types::ComponentItem::ComponentFunc(func) => { 12396e19700SNick Fitzgerald wasmtime::error::Context::context( 124bb77f602SPat Hickey func.typecheck::<(), ()>(&_instance_type), 125bb77f602SPat Hickey "type-checking export func `new`", 126bb77f602SPat Hickey )?; 127bb77f602SPat Hickey index 128636435f1SJoel Dice } 12996e19700SNick Fitzgerald _ => Err(wasmtime::format_err!("export `new` is not a function"))?, 130bb77f602SPat Hickey } 131bb77f602SPat Hickey }; 132636435f1SJoel Dice Ok(FooIndices { new }) 133636435f1SJoel Dice } 134636435f1SJoel Dice /// Uses the indices stored in `self` to load an instance 135636435f1SJoel Dice /// of [`Foo`] from the instance provided. 136636435f1SJoel Dice /// 137636435f1SJoel Dice /// Note that at this time this method will additionally 138636435f1SJoel Dice /// perform type-checks of all exports. load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Foo>139636435f1SJoel Dice pub fn load( 140636435f1SJoel Dice &self, 141636435f1SJoel Dice mut store: impl wasmtime::AsContextMut, 142636435f1SJoel Dice instance: &wasmtime::component::Instance, 143636435f1SJoel Dice ) -> wasmtime::Result<Foo> { 144bb77f602SPat Hickey let _ = &mut store; 145636435f1SJoel Dice let _instance = instance; 146636435f1SJoel Dice let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func(); 147636435f1SJoel Dice Ok(Foo { new }) 148636435f1SJoel Dice } 149636435f1SJoel Dice } 150636435f1SJoel Dice impl Foo { 151636435f1SJoel Dice /// 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 171636435f1SJoel Dice /// [`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,172636435f1SJoel Dice pub async fn instantiate_async<_T>( 173bb77f602SPat Hickey store: impl wasmtime::AsContextMut<Data = _T>, 174636435f1SJoel Dice component: &wasmtime::component::Component, 175636435f1SJoel Dice linker: &wasmtime::component::Linker<_T>, 176636435f1SJoel Dice ) -> wasmtime::Result<Foo> 177636435f1SJoel Dice where 178beca86b0SAlex Crichton _T: Send, 179636435f1SJoel Dice { 180636435f1SJoel Dice let pre = linker.instantiate_pre(component)?; 181636435f1SJoel Dice FooPre::new(pre)?.instantiate_async(store).await 182636435f1SJoel Dice } call_new<_T, _D>( &self, accessor: &wasmtime::component::Accessor<_T, _D>, ) -> wasmtime::Result<()> where _T: Send, _D: wasmtime::component::HasData,18364bc3bd9SAlex Crichton pub async fn call_new<_T, _D>( 184636435f1SJoel Dice &self, 18564bc3bd9SAlex Crichton accessor: &wasmtime::component::Accessor<_T, _D>, 18664bc3bd9SAlex Crichton ) -> wasmtime::Result<()> 187636435f1SJoel Dice where 18864bc3bd9SAlex Crichton _T: Send, 18964bc3bd9SAlex Crichton _D: wasmtime::component::HasData, 190636435f1SJoel Dice { 191636435f1SJoel Dice let callee = unsafe { 192636435f1SJoel Dice wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new) 193636435f1SJoel Dice }; 194*1e0b0b46SAlex Crichton let () = callee.call_concurrent(accessor, ()).await?; 19564bc3bd9SAlex Crichton Ok(()) 196636435f1SJoel Dice } 197636435f1SJoel Dice } 198636435f1SJoel Dice }; 199