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, 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, 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, 170 U: a::b::interface_with_live_type::Host 171 + a::b::interface_with_dead_type::Host + Send, 172 { 173 a::b::interface_with_live_type::add_to_linker(linker, get)?; 174 a::b::interface_with_dead_type::add_to_linker(linker, get)?; 175 Ok(()) 176 } 177 } 178 }; 179 pub mod a { 180 pub mod b { 181 #[allow(clippy::all)] 182 pub mod interface_with_live_type { 183 #[allow(unused_imports)] 184 use wasmtime::component::__internal::anyhow; 185 #[derive(wasmtime::component::ComponentType)] 186 #[derive(wasmtime::component::Lift)] 187 #[derive(wasmtime::component::Lower)] 188 #[component(record)] 189 #[derive(Clone, Copy)] 190 pub struct LiveType { 191 #[component(name = "a")] 192 pub a: u32, 193 } 194 impl core::fmt::Debug for LiveType { 195 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 196 f.debug_struct("LiveType").field("a", &self.a).finish() 197 } 198 } 199 const _: () = { 200 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); 201 assert!( 202 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 203 ); 204 }; 205 #[wasmtime::component::__internal::async_trait] 206 pub trait Host: Send { 207 async fn f(&mut self) -> LiveType; 208 } 209 pub trait GetHost< 210 T, 211 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 212 type Host: Host + Send; 213 } 214 impl<F, T, O> GetHost<T> for F 215 where 216 F: Fn(T) -> O + Send + Sync + Copy + 'static, 217 O: Host + Send, 218 { 219 type Host = O; 220 } 221 pub fn add_to_linker_get_host<T>( 222 linker: &mut wasmtime::component::Linker<T>, 223 host_getter: impl for<'a> GetHost<&'a mut T>, 224 ) -> wasmtime::Result<()> 225 where 226 T: Send, 227 { 228 let mut inst = linker.instance("a:b/interface-with-live-type")?; 229 inst.func_wrap_async( 230 "f", 231 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 232 wasmtime::component::__internal::Box::new(async move { 233 let host = &mut host_getter(caller.data_mut()); 234 let r = Host::f(host).await; 235 Ok((r,)) 236 }) 237 }, 238 )?; 239 Ok(()) 240 } 241 pub fn add_to_linker<T, U>( 242 linker: &mut wasmtime::component::Linker<T>, 243 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 244 ) -> wasmtime::Result<()> 245 where 246 U: Host + Send, 247 T: Send, 248 { 249 add_to_linker_get_host(linker, get) 250 } 251 #[wasmtime::component::__internal::async_trait] 252 impl<_T: Host + ?Sized + Send> Host for &mut _T { 253 async fn f(&mut self) -> LiveType { 254 Host::f(*self).await 255 } 256 } 257 } 258 #[allow(clippy::all)] 259 pub mod interface_with_dead_type { 260 #[allow(unused_imports)] 261 use wasmtime::component::__internal::anyhow; 262 #[wasmtime::component::__internal::async_trait] 263 pub trait Host: Send {} 264 pub trait GetHost< 265 T, 266 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 267 type Host: Host + Send; 268 } 269 impl<F, T, O> GetHost<T> for F 270 where 271 F: Fn(T) -> O + Send + Sync + Copy + 'static, 272 O: Host + Send, 273 { 274 type Host = O; 275 } 276 pub fn add_to_linker_get_host<T>( 277 linker: &mut wasmtime::component::Linker<T>, 278 host_getter: impl for<'a> GetHost<&'a mut T>, 279 ) -> wasmtime::Result<()> 280 where 281 T: Send, 282 { 283 let mut inst = linker.instance("a:b/interface-with-dead-type")?; 284 Ok(()) 285 } 286 pub fn add_to_linker<T, U>( 287 linker: &mut wasmtime::component::Linker<T>, 288 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 289 ) -> wasmtime::Result<()> 290 where 291 U: Host + Send, 292 T: Send, 293 { 294 add_to_linker_get_host(linker, get) 295 } 296 #[wasmtime::component::__internal::async_trait] 297 impl<_T: Host + ?Sized + Send> Host for &mut _T {} 298 } 299 } 300 } 301