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, Box}; 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::trait_variant_make(::core::marker::Send)] 206 pub trait Host: Send { 207 async fn f(&mut self) -> LiveType; 208 } 209 pub trait GetHost< 210 T, 211 D, 212 >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static { 213 type Host: Host + Send; 214 } 215 impl<F, T, D, O> GetHost<T, D> for F 216 where 217 F: Fn(T) -> O + Send + Sync + Copy + 'static, 218 O: Host + Send, 219 { 220 type Host = O; 221 } 222 pub fn add_to_linker_get_host< 223 T, 224 G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>, 225 >( 226 linker: &mut wasmtime::component::Linker<T>, 227 host_getter: G, 228 ) -> wasmtime::Result<()> 229 where 230 T: Send, 231 { 232 let mut inst = linker.instance("a:b/interface-with-live-type")?; 233 inst.func_wrap_async( 234 "f", 235 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 236 use tracing::Instrument; 237 let span = tracing::span!( 238 tracing::Level::TRACE, "wit-bindgen import", module = 239 "interface-with-live-type", function = "f", 240 ); 241 wasmtime::component::__internal::Box::new( 242 async move { 243 tracing::event!(tracing::Level::TRACE, "call"); 244 let host = &mut host_getter(caller.data_mut()); 245 let r = Host::f(host).await; 246 tracing::event!( 247 tracing::Level::TRACE, result = tracing::field::debug(& r), 248 "return" 249 ); 250 Ok((r,)) 251 } 252 .instrument(span), 253 ) 254 }, 255 )?; 256 Ok(()) 257 } 258 pub fn add_to_linker<T, U>( 259 linker: &mut wasmtime::component::Linker<T>, 260 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 261 ) -> wasmtime::Result<()> 262 where 263 U: Host + Send, 264 T: Send, 265 { 266 add_to_linker_get_host(linker, get) 267 } 268 impl<_T: Host + ?Sized + Send> Host for &mut _T { 269 async fn f(&mut self) -> LiveType { 270 Host::f(*self).await 271 } 272 } 273 } 274 #[allow(clippy::all)] 275 pub mod interface_with_dead_type { 276 #[allow(unused_imports)] 277 use wasmtime::component::__internal::{anyhow, Box}; 278 #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] 279 pub trait Host: Send {} 280 pub trait GetHost< 281 T, 282 D, 283 >: Fn(T) -> <Self as GetHost<T, D>>::Host + Send + Sync + Copy + 'static { 284 type Host: Host + Send; 285 } 286 impl<F, T, D, O> GetHost<T, D> for F 287 where 288 F: Fn(T) -> O + Send + Sync + Copy + 'static, 289 O: Host + Send, 290 { 291 type Host = O; 292 } 293 pub fn add_to_linker_get_host< 294 T, 295 G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>, 296 >( 297 linker: &mut wasmtime::component::Linker<T>, 298 host_getter: G, 299 ) -> wasmtime::Result<()> 300 where 301 T: Send, 302 { 303 let mut inst = linker.instance("a:b/interface-with-dead-type")?; 304 Ok(()) 305 } 306 pub fn add_to_linker<T, U>( 307 linker: &mut wasmtime::component::Linker<T>, 308 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 309 ) -> wasmtime::Result<()> 310 where 311 U: Host + Send, 312 T: Send, 313 { 314 add_to_linker_get_host(linker, get) 315 } 316 impl<_T: Host + ?Sized + Send> Host for &mut _T {} 317 } 318 } 319 } 320