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