1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// component which implements the world `imports`. 3 /// 4 /// This structure is created through [`ImportsPre::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 [`Imports`] as well. 9 pub struct ImportsPre<T> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: ImportsIndices, 12 } 13 impl<T> Clone for ImportsPre<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> ImportsPre<_T> { 22 /// Creates a new copy of `ImportsPre` 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 = ImportsIndices::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 [`Imports`] 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<Imports> 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 /// `imports`. 60 /// 61 /// This is an implementation detail of [`ImportsPre`] and can 62 /// be constructed if needed as well. 63 /// 64 /// For more information see [`Imports`] as well. 65 #[derive(Clone)] 66 pub struct ImportsIndices {} 67 /// Auto-generated bindings for an instance a component which 68 /// implements the world `imports`. 69 /// 70 /// This structure can be created through a number of means 71 /// depending on your requirements and what you have on hand: 72 /// 73 /// * The most convenient way is to use 74 /// [`Imports::instantiate_async`] which only needs a 75 /// [`Store`], [`Component`], and [`Linker`]. 76 /// 77 /// * Alternatively you can create a [`ImportsPre`] ahead of 78 /// time with a [`Component`] to front-load string lookups 79 /// of exports once instead of per-instantiation. This 80 /// method then uses [`ImportsPre::instantiate_async`] to 81 /// create a [`Imports`]. 82 /// 83 /// * If you've instantiated the instance yourself already 84 /// then you can use [`Imports::new`]. 85 /// 86 /// * You can also access the guts of instantiation through 87 /// [`ImportsIndices::new_instance`] followed 88 /// by [`ImportsIndices::load`] to crate an instance of this 89 /// type. 90 /// 91 /// These methods are all equivalent to one another and move 92 /// around the tradeoff of what work is performed when. 93 /// 94 /// [`Store`]: wasmtime::Store 95 /// [`Component`]: wasmtime::component::Component 96 /// [`Linker`]: wasmtime::component::Linker 97 pub struct Imports {} 98 const _: () = { 99 #[allow(unused_imports)] 100 use wasmtime::component::__internal::anyhow; 101 impl ImportsIndices { 102 /// Creates a new copy of `ImportsIndices` bindings which can then 103 /// be used to instantiate into a particular store. 104 /// 105 /// This method may fail if the component does not have the 106 /// required exports. 107 pub fn new( 108 component: &wasmtime::component::Component, 109 ) -> wasmtime::Result<Self> { 110 let _component = component; 111 Ok(ImportsIndices {}) 112 } 113 /// Creates a new instance of [`ImportsIndices`] from an 114 /// instantiated component. 115 /// 116 /// This method of creating a [`Imports`] will perform string 117 /// lookups for all exports when this method is called. This 118 /// will only succeed if the provided instance matches the 119 /// requirements of [`Imports`]. 120 pub fn new_instance( 121 mut store: impl wasmtime::AsContextMut, 122 instance: &wasmtime::component::Instance, 123 ) -> wasmtime::Result<Self> { 124 let _instance = instance; 125 Ok(ImportsIndices {}) 126 } 127 /// Uses the indices stored in `self` to load an instance 128 /// of [`Imports`] from the instance provided. 129 /// 130 /// Note that at this time this method will additionally 131 /// perform type-checks of all exports. 132 pub fn load( 133 &self, 134 mut store: impl wasmtime::AsContextMut, 135 instance: &wasmtime::component::Instance, 136 ) -> wasmtime::Result<Imports> { 137 let _instance = instance; 138 Ok(Imports {}) 139 } 140 } 141 impl Imports { 142 /// Convenience wrapper around [`ImportsPre::new`] and 143 /// [`ImportsPre::instantiate_async`]. 144 pub async fn instantiate_async<_T>( 145 mut store: impl wasmtime::AsContextMut<Data = _T>, 146 component: &wasmtime::component::Component, 147 linker: &wasmtime::component::Linker<_T>, 148 ) -> wasmtime::Result<Imports> 149 where 150 _T: Send + 'static, 151 { 152 let pre = linker.instantiate_pre(component)?; 153 ImportsPre::new(pre)?.instantiate_async(store).await 154 } 155 /// Convenience wrapper around [`ImportsIndices::new_instance`] and 156 /// [`ImportsIndices::load`]. 157 pub fn new( 158 mut store: impl wasmtime::AsContextMut, 159 instance: &wasmtime::component::Instance, 160 ) -> wasmtime::Result<Imports> { 161 let indices = ImportsIndices::new_instance(&mut store, instance)?; 162 indices.load(store, instance) 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 T: Send + a::b::interface_with_live_type::Host<Data = T> 170 + a::b::interface_with_dead_type::Host + 'static, 171 U: Send + a::b::interface_with_live_type::Host<Data = T> 172 + a::b::interface_with_dead_type::Host, 173 { 174 a::b::interface_with_live_type::add_to_linker(linker, get)?; 175 a::b::interface_with_dead_type::add_to_linker(linker, get)?; 176 Ok(()) 177 } 178 } 179 }; 180 pub mod a { 181 pub mod b { 182 #[allow(clippy::all)] 183 pub mod interface_with_live_type { 184 #[allow(unused_imports)] 185 use wasmtime::component::__internal::{anyhow, Box}; 186 #[derive(wasmtime::component::ComponentType)] 187 #[derive(wasmtime::component::Lift)] 188 #[derive(wasmtime::component::Lower)] 189 #[component(record)] 190 #[derive(Clone, Copy)] 191 pub struct LiveType { 192 #[component(name = "a")] 193 pub a: u32, 194 } 195 impl core::fmt::Debug for LiveType { 196 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 197 f.debug_struct("LiveType").field("a", &self.a).finish() 198 } 199 } 200 const _: () = { 201 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); 202 assert!( 203 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 204 ); 205 }; 206 pub trait Host { 207 type Data; 208 fn f( 209 store: wasmtime::StoreContextMut<'_, Self::Data>, 210 ) -> impl ::core::future::Future< 211 Output = impl FnOnce( 212 wasmtime::StoreContextMut<'_, Self::Data>, 213 ) -> LiveType + Send + Sync + 'static, 214 > + Send + Sync + 'static 215 where 216 Self: Sized; 217 } 218 pub trait GetHost< 219 T, 220 D, 221 >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static { 222 type Host: Host<Data = D> + Send; 223 } 224 impl<F, T, D, O> GetHost<T, D> for F 225 where 226 F: Fn(T) -> O + Send + Sync + Copy + 'static, 227 O: Host<Data = D> + Send, 228 { 229 type Host = O; 230 } 231 pub fn add_to_linker_get_host< 232 T, 233 G: for<'a> GetHost<&'a mut T, T, Host: Host<Data = T> + Send>, 234 >( 235 linker: &mut wasmtime::component::Linker<T>, 236 host_getter: G, 237 ) -> wasmtime::Result<()> 238 where 239 T: Send + 'static, 240 { 241 let mut inst = linker.instance("a:b/interface-with-live-type")?; 242 inst.func_wrap_concurrent( 243 "f", 244 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 245 let host = caller; 246 let r = <G::Host as Host>::f(host); 247 Box::pin(async move { 248 let fun = r.await; 249 Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { 250 let r = fun(caller); 251 Ok((r,)) 252 }) 253 as Box< 254 dyn FnOnce( 255 wasmtime::StoreContextMut<'_, T>, 256 ) -> wasmtime::Result<(LiveType,)> + Send + Sync, 257 > 258 }) 259 as ::core::pin::Pin< 260 Box< 261 dyn ::core::future::Future< 262 Output = Box< 263 dyn FnOnce( 264 wasmtime::StoreContextMut<'_, T>, 265 ) -> wasmtime::Result<(LiveType,)> + Send + Sync, 266 >, 267 > + Send + Sync + 'static, 268 >, 269 > 270 }, 271 )?; 272 Ok(()) 273 } 274 pub fn add_to_linker<T, U>( 275 linker: &mut wasmtime::component::Linker<T>, 276 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 277 ) -> wasmtime::Result<()> 278 where 279 U: Host<Data = T> + Send, 280 T: Send + 'static, 281 { 282 add_to_linker_get_host(linker, get) 283 } 284 impl<_T: Host> Host for &mut _T { 285 type Data = _T::Data; 286 fn f( 287 store: wasmtime::StoreContextMut<'_, Self::Data>, 288 ) -> impl ::core::future::Future< 289 Output = impl FnOnce( 290 wasmtime::StoreContextMut<'_, Self::Data>, 291 ) -> LiveType + Send + Sync + 'static, 292 > + Send + Sync + 'static 293 where 294 Self: Sized, 295 { 296 <_T as Host>::f(store) 297 } 298 } 299 } 300 #[allow(clippy::all)] 301 pub mod interface_with_dead_type { 302 #[allow(unused_imports)] 303 use wasmtime::component::__internal::{anyhow, Box}; 304 pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType; 305 const _: () = { 306 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); 307 assert!( 308 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 309 ); 310 }; 311 #[derive(wasmtime::component::ComponentType)] 312 #[derive(wasmtime::component::Lift)] 313 #[derive(wasmtime::component::Lower)] 314 #[component(record)] 315 #[derive(Clone, Copy)] 316 pub struct DeadType { 317 #[component(name = "a")] 318 pub a: u32, 319 } 320 impl core::fmt::Debug for DeadType { 321 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 322 f.debug_struct("DeadType").field("a", &self.a).finish() 323 } 324 } 325 const _: () = { 326 assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32); 327 assert!( 328 4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32 329 ); 330 }; 331 #[derive(wasmtime::component::ComponentType)] 332 #[derive(wasmtime::component::Lift)] 333 #[derive(wasmtime::component::Lower)] 334 #[component(variant)] 335 #[derive(Clone, Copy)] 336 pub enum V { 337 #[component(name = "a")] 338 A(LiveType), 339 #[component(name = "b")] 340 B(DeadType), 341 } 342 impl core::fmt::Debug for V { 343 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 344 match self { 345 V::A(e) => f.debug_tuple("V::A").field(e).finish(), 346 V::B(e) => f.debug_tuple("V::B").field(e).finish(), 347 } 348 } 349 } 350 const _: () = { 351 assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32); 352 assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32); 353 }; 354 pub trait Host {} 355 pub trait GetHost< 356 T, 357 D, 358 >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static { 359 type Host: Host + Send; 360 } 361 impl<F, T, D, O> GetHost<T, D> for F 362 where 363 F: Fn(T) -> O + Send + Sync + Copy + 'static, 364 O: Host + Send, 365 { 366 type Host = O; 367 } 368 pub fn add_to_linker_get_host< 369 T, 370 G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>, 371 >( 372 linker: &mut wasmtime::component::Linker<T>, 373 host_getter: G, 374 ) -> wasmtime::Result<()> 375 where 376 T: Send + 'static, 377 { 378 let mut inst = linker.instance("a:b/interface-with-dead-type")?; 379 Ok(()) 380 } 381 pub fn add_to_linker<T, U>( 382 linker: &mut wasmtime::component::Linker<T>, 383 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 384 ) -> wasmtime::Result<()> 385 where 386 U: Host + Send, 387 T: Send + 'static, 388 { 389 add_to_linker_get_host(linker, get) 390 } 391 impl<_T: Host + ?Sized> Host for &mut _T {} 392 } 393 } 394 } 395