1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// component which implements the world `foo`. 3 /// 4 /// This structure is created through [`FooPre::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 [`Foo`] as well. 9 pub struct FooPre<T> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: FooIndices, 12 } 13 impl<T> Clone for FooPre<T> { 14 fn clone(&self) -> Self { 15 Self { 16 instance_pre: self.instance_pre.clone(), 17 indices: self.indices.clone(), 18 } 19 } 20 } 21 impl<_T> FooPre<_T> { 22 /// Creates a new copy of `FooPre` 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. 27 pub fn new( 28 instance_pre: wasmtime::component::InstancePre<_T>, 29 ) -> wasmtime::Result<Self> { 30 let indices = FooIndices::new(instance_pre.component())?; 31 Ok(Self { instance_pre, indices }) 32 } 33 pub fn engine(&self) -> &wasmtime::Engine { 34 self.instance_pre.engine() 35 } 36 pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 37 &self.instance_pre 38 } 39 /// Instantiates a new instance of [`Foo`] 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. 46 pub async fn instantiate_async( 47 &self, 48 mut store: impl wasmtime::AsContextMut<Data = _T>, 49 ) -> wasmtime::Result<Foo> 50 where 51 _T: Send + 'static, 52 { 53 let mut store = store.as_context_mut(); 54 let instance = self.instance_pre.instantiate_async(&mut store).await?; 55 self.indices.load(&mut store, &instance) 56 } 57 } 58 /// Auto-generated bindings for index of the exports of 59 /// `foo`. 60 /// 61 /// This is an implementation detail of [`FooPre`] and can 62 /// be constructed if needed as well. 63 /// 64 /// For more information see [`Foo`] as well. 65 #[derive(Clone)] 66 pub struct FooIndices { 67 new: wasmtime::component::ComponentExportIndex, 68 } 69 /// Auto-generated bindings for an instance a component which 70 /// implements the world `foo`. 71 /// 72 /// This structure can be created through a number of means 73 /// depending on your requirements and what you have on hand: 74 /// 75 /// * The most convenient way is to use 76 /// [`Foo::instantiate_async`] which only needs a 77 /// [`Store`], [`Component`], and [`Linker`]. 78 /// 79 /// * Alternatively you can create a [`FooPre`] ahead of 80 /// time with a [`Component`] to front-load string lookups 81 /// of exports once instead of per-instantiation. This 82 /// method then uses [`FooPre::instantiate_async`] to 83 /// create a [`Foo`]. 84 /// 85 /// * If you've instantiated the instance yourself already 86 /// then you can use [`Foo::new`]. 87 /// 88 /// * You can also access the guts of instantiation through 89 /// [`FooIndices::new_instance`] followed 90 /// by [`FooIndices::load`] to crate an instance of this 91 /// type. 92 /// 93 /// These methods are all equivalent to one another and move 94 /// around the tradeoff of what work is performed when. 95 /// 96 /// [`Store`]: wasmtime::Store 97 /// [`Component`]: wasmtime::component::Component 98 /// [`Linker`]: wasmtime::component::Linker 99 pub struct Foo { 100 new: wasmtime::component::Func, 101 } 102 const _: () = { 103 #[allow(unused_imports)] 104 use wasmtime::component::__internal::anyhow; 105 impl FooIndices { 106 /// Creates a new copy of `FooIndices` bindings which can then 107 /// be used to instantiate into a particular store. 108 /// 109 /// This method may fail if the component does not have the 110 /// required exports. 111 pub fn new( 112 component: &wasmtime::component::Component, 113 ) -> wasmtime::Result<Self> { 114 let _component = component; 115 let new = _component 116 .export_index(None, "new") 117 .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))? 118 .1; 119 Ok(FooIndices { new }) 120 } 121 /// Creates a new instance of [`FooIndices`] from an 122 /// instantiated component. 123 /// 124 /// This method of creating a [`Foo`] will perform string 125 /// lookups for all exports when this method is called. This 126 /// will only succeed if the provided instance matches the 127 /// requirements of [`Foo`]. 128 pub fn new_instance( 129 mut store: impl wasmtime::AsContextMut, 130 instance: &wasmtime::component::Instance, 131 ) -> wasmtime::Result<Self> { 132 let _instance = instance; 133 let new = _instance 134 .get_export(&mut store, None, "new") 135 .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?; 136 Ok(FooIndices { new }) 137 } 138 /// Uses the indices stored in `self` to load an instance 139 /// of [`Foo`] from the instance provided. 140 /// 141 /// Note that at this time this method will additionally 142 /// perform type-checks of all exports. 143 pub fn load( 144 &self, 145 mut store: impl wasmtime::AsContextMut, 146 instance: &wasmtime::component::Instance, 147 ) -> wasmtime::Result<Foo> { 148 let _instance = instance; 149 let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func(); 150 Ok(Foo { new }) 151 } 152 } 153 impl Foo { 154 /// Convenience wrapper around [`FooPre::new`] and 155 /// [`FooPre::instantiate_async`]. 156 pub async fn instantiate_async<_T>( 157 mut store: impl wasmtime::AsContextMut<Data = _T>, 158 component: &wasmtime::component::Component, 159 linker: &wasmtime::component::Linker<_T>, 160 ) -> wasmtime::Result<Foo> 161 where 162 _T: Send + 'static, 163 { 164 let pre = linker.instantiate_pre(component)?; 165 FooPre::new(pre)?.instantiate_async(store).await 166 } 167 /// Convenience wrapper around [`FooIndices::new_instance`] and 168 /// [`FooIndices::load`]. 169 pub fn new( 170 mut store: impl wasmtime::AsContextMut, 171 instance: &wasmtime::component::Instance, 172 ) -> wasmtime::Result<Foo> { 173 let indices = FooIndices::new_instance(&mut store, instance)?; 174 indices.load(store, instance) 175 } 176 pub async fn call_new<S: wasmtime::AsContextMut>( 177 &self, 178 mut store: S, 179 ) -> wasmtime::Result<wasmtime::component::Promise<()>> 180 where 181 <S as wasmtime::AsContext>::Data: Send + 'static, 182 { 183 let callee = unsafe { 184 wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new) 185 }; 186 let promise = callee.call_concurrent(store.as_context_mut(), ()).await?; 187 Ok(promise) 188 } 189 } 190 }; 191