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