1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// copmonent which implements the world `the-world`. 3 /// 4 /// This structure is created through [`TheWorldPre::new`] which 5 /// takes a [`InstancePre`](wasmtime::component::InstancePre) that 6 /// has been created through a [`Linker`](wasmtime::component::Linker). 7 pub struct TheWorldPre<T> { 8 instance_pre: wasmtime::component::InstancePre<T>, 9 interface0: exports::foo::foo::strings::GuestPre, 10 } 11 /// Auto-generated bindings for an instance a component which 12 /// implements the world `the-world`. 13 /// 14 /// This structure is created through either 15 /// [`TheWorld::instantiate_async`] or by first creating 16 /// a [`TheWorldPre`] followed by using 17 /// [`TheWorldPre::instantiate_async`]. 18 pub struct TheWorld { 19 interface0: exports::foo::foo::strings::Guest, 20 } 21 const _: () = { 22 #[allow(unused_imports)] 23 use wasmtime::component::__internal::anyhow; 24 impl<_T> TheWorldPre<_T> { 25 /// Creates a new copy of `TheWorldPre` bindings which can then 26 /// be used to instantiate into a particular store. 27 /// 28 /// This method may fail if the compoennt behind `instance_pre` 29 /// does not have the required exports. 30 pub fn new( 31 instance_pre: wasmtime::component::InstancePre<_T>, 32 ) -> wasmtime::Result<Self> { 33 let _component = instance_pre.component(); 34 let interface0 = exports::foo::foo::strings::GuestPre::new(_component)?; 35 Ok(TheWorldPre { 36 instance_pre, 37 interface0, 38 }) 39 } 40 /// Instantiates a new instance of [`TheWorld`] within the 41 /// `store` provided. 42 /// 43 /// This function will use `self` as the pre-instantiated 44 /// instance to perform instantiation. Afterwards the preloaded 45 /// indices in `self` are used to lookup all exports on the 46 /// resulting instance. 47 pub async fn instantiate_async( 48 &self, 49 mut store: impl wasmtime::AsContextMut<Data = _T>, 50 ) -> wasmtime::Result<TheWorld> 51 where 52 _T: Send, 53 { 54 let mut store = store.as_context_mut(); 55 let _instance = self.instance_pre.instantiate_async(&mut store).await?; 56 let interface0 = self.interface0.load(&mut store, &_instance)?; 57 Ok(TheWorld { interface0 }) 58 } 59 } 60 impl TheWorld { 61 /// Convenience wrapper around [`TheWorldPre::new`] and 62 /// [`TheWorldPre::instantiate_async`]. 63 pub async fn instantiate_async<_T>( 64 mut store: impl wasmtime::AsContextMut<Data = _T>, 65 component: &wasmtime::component::Component, 66 linker: &wasmtime::component::Linker<_T>, 67 ) -> wasmtime::Result<TheWorld> 68 where 69 _T: Send, 70 { 71 let pre = linker.instantiate_pre(component)?; 72 TheWorldPre::new(pre)?.instantiate_async(store).await 73 } 74 pub fn add_to_linker<T, U>( 75 linker: &mut wasmtime::component::Linker<T>, 76 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 77 ) -> wasmtime::Result<()> 78 where 79 T: Send, 80 U: foo::foo::strings::Host + Send, 81 { 82 foo::foo::strings::add_to_linker(linker, get)?; 83 Ok(()) 84 } 85 pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { 86 &self.interface0 87 } 88 } 89 }; 90 pub mod foo { 91 pub mod foo { 92 #[allow(clippy::all)] 93 pub mod strings { 94 #[allow(unused_imports)] 95 use wasmtime::component::__internal::anyhow; 96 #[wasmtime::component::__internal::async_trait] 97 pub trait Host: Send { 98 async fn a(&mut self, x: wasmtime::component::__internal::String) -> (); 99 async fn b(&mut self) -> wasmtime::component::__internal::String; 100 async fn c( 101 &mut self, 102 a: wasmtime::component::__internal::String, 103 b: wasmtime::component::__internal::String, 104 ) -> wasmtime::component::__internal::String; 105 } 106 pub trait GetHost< 107 T, 108 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 109 type Host: Host + Send; 110 } 111 impl<F, T, O> GetHost<T> for F 112 where 113 F: Fn(T) -> O + Send + Sync + Copy + 'static, 114 O: Host + Send, 115 { 116 type Host = O; 117 } 118 pub fn add_to_linker_get_host<T>( 119 linker: &mut wasmtime::component::Linker<T>, 120 host_getter: impl for<'a> GetHost<&'a mut T>, 121 ) -> wasmtime::Result<()> 122 where 123 T: Send, 124 { 125 let mut inst = linker.instance("foo:foo/strings")?; 126 inst.func_wrap_async( 127 "a", 128 move | 129 mut caller: wasmtime::StoreContextMut<'_, T>, 130 (arg0,): (wasmtime::component::__internal::String,)| 131 wasmtime::component::__internal::Box::new(async move { 132 let host = &mut host_getter(caller.data_mut()); 133 let r = Host::a(host, arg0).await; 134 Ok(r) 135 }), 136 )?; 137 inst.func_wrap_async( 138 "b", 139 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| wasmtime::component::__internal::Box::new(async move { 140 let host = &mut host_getter(caller.data_mut()); 141 let r = Host::b(host).await; 142 Ok((r,)) 143 }), 144 )?; 145 inst.func_wrap_async( 146 "c", 147 move | 148 mut caller: wasmtime::StoreContextMut<'_, T>, 149 ( 150 arg0, 151 arg1, 152 ): ( 153 wasmtime::component::__internal::String, 154 wasmtime::component::__internal::String, 155 )| 156 wasmtime::component::__internal::Box::new(async move { 157 let host = &mut host_getter(caller.data_mut()); 158 let r = Host::c(host, arg0, arg1).await; 159 Ok((r,)) 160 }), 161 )?; 162 Ok(()) 163 } 164 pub fn add_to_linker<T, U>( 165 linker: &mut wasmtime::component::Linker<T>, 166 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 167 ) -> wasmtime::Result<()> 168 where 169 U: Host + Send, 170 T: Send, 171 { 172 add_to_linker_get_host(linker, get) 173 } 174 #[wasmtime::component::__internal::async_trait] 175 impl<_T: Host + ?Sized + Send> Host for &mut _T { 176 async fn a(&mut self, x: wasmtime::component::__internal::String) -> () { 177 Host::a(*self, x).await 178 } 179 async fn b(&mut self) -> wasmtime::component::__internal::String { 180 Host::b(*self).await 181 } 182 async fn c( 183 &mut self, 184 a: wasmtime::component::__internal::String, 185 b: wasmtime::component::__internal::String, 186 ) -> wasmtime::component::__internal::String { 187 Host::c(*self, a, b).await 188 } 189 } 190 } 191 } 192 } 193 pub mod exports { 194 pub mod foo { 195 pub mod foo { 196 #[allow(clippy::all)] 197 pub mod strings { 198 #[allow(unused_imports)] 199 use wasmtime::component::__internal::anyhow; 200 pub struct Guest { 201 a: wasmtime::component::Func, 202 b: wasmtime::component::Func, 203 c: wasmtime::component::Func, 204 } 205 pub struct GuestPre { 206 a: wasmtime::component::ComponentExportIndex, 207 b: wasmtime::component::ComponentExportIndex, 208 c: wasmtime::component::ComponentExportIndex, 209 } 210 impl GuestPre { 211 pub fn new( 212 component: &wasmtime::component::Component, 213 ) -> wasmtime::Result<GuestPre> { 214 let _component = component; 215 let (_, instance) = component 216 .export_index(None, "foo:foo/strings") 217 .ok_or_else(|| { 218 anyhow::anyhow!( 219 "no exported instance named `foo:foo/strings`" 220 ) 221 })?; 222 let _lookup = |name: &str| { 223 _component 224 .export_index(Some(&instance), name) 225 .map(|p| p.1) 226 .ok_or_else(|| { 227 anyhow::anyhow!( 228 "instance export `foo:foo/strings` does \ 229 not have export `{name}`" 230 ) 231 }) 232 }; 233 let a = _lookup("a")?; 234 let b = _lookup("b")?; 235 let c = _lookup("c")?; 236 Ok(GuestPre { a, b, c }) 237 } 238 pub fn load( 239 &self, 240 mut store: impl wasmtime::AsContextMut, 241 instance: &wasmtime::component::Instance, 242 ) -> wasmtime::Result<Guest> { 243 let mut store = store.as_context_mut(); 244 let _ = &mut store; 245 let _instance = instance; 246 let a = *_instance 247 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 248 .func(); 249 let b = *_instance 250 .get_typed_func::< 251 (), 252 (wasmtime::component::__internal::String,), 253 >(&mut store, &self.b)? 254 .func(); 255 let c = *_instance 256 .get_typed_func::< 257 (&str, &str), 258 (wasmtime::component::__internal::String,), 259 >(&mut store, &self.c)? 260 .func(); 261 Ok(Guest { a, b, c }) 262 } 263 } 264 impl Guest { 265 pub async fn call_a<S: wasmtime::AsContextMut>( 266 &self, 267 mut store: S, 268 arg0: &str, 269 ) -> wasmtime::Result<()> 270 where 271 <S as wasmtime::AsContext>::Data: Send, 272 { 273 let callee = unsafe { 274 wasmtime::component::TypedFunc::< 275 (&str,), 276 (), 277 >::new_unchecked(self.a) 278 }; 279 let () = callee 280 .call_async(store.as_context_mut(), (arg0,)) 281 .await?; 282 callee.post_return_async(store.as_context_mut()).await?; 283 Ok(()) 284 } 285 pub async fn call_b<S: wasmtime::AsContextMut>( 286 &self, 287 mut store: S, 288 ) -> wasmtime::Result<wasmtime::component::__internal::String> 289 where 290 <S as wasmtime::AsContext>::Data: Send, 291 { 292 let callee = unsafe { 293 wasmtime::component::TypedFunc::< 294 (), 295 (wasmtime::component::__internal::String,), 296 >::new_unchecked(self.b) 297 }; 298 let (ret0,) = callee 299 .call_async(store.as_context_mut(), ()) 300 .await?; 301 callee.post_return_async(store.as_context_mut()).await?; 302 Ok(ret0) 303 } 304 pub async fn call_c<S: wasmtime::AsContextMut>( 305 &self, 306 mut store: S, 307 arg0: &str, 308 arg1: &str, 309 ) -> wasmtime::Result<wasmtime::component::__internal::String> 310 where 311 <S as wasmtime::AsContext>::Data: Send, 312 { 313 let callee = unsafe { 314 wasmtime::component::TypedFunc::< 315 (&str, &str), 316 (wasmtime::component::__internal::String,), 317 >::new_unchecked(self.c) 318 }; 319 let (ret0,) = callee 320 .call_async(store.as_context_mut(), (arg0, arg1)) 321 .await?; 322 callee.post_return_async(store.as_context_mut()).await?; 323 Ok(ret0) 324 } 325 } 326 } 327 } 328 } 329 } 330