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 pub struct ImportsPre<T> { 8 instance_pre: wasmtime::component::InstancePre<T>, 9 } 10 impl<T> Clone for ImportsPre<T> { 11 fn clone(&self) -> Self { 12 Self { 13 instance_pre: self.instance_pre.clone(), 14 } 15 } 16 } 17 /// Auto-generated bindings for an instance a component which 18 /// implements the world `imports`. 19 /// 20 /// This structure is created through either 21 /// [`Imports::instantiate_async`] or by first creating 22 /// a [`ImportsPre`] followed by using 23 /// [`ImportsPre::instantiate_async`]. 24 pub struct Imports {} 25 const _: () = { 26 #[allow(unused_imports)] 27 use wasmtime::component::__internal::anyhow; 28 impl<_T> ImportsPre<_T> { 29 /// Creates a new copy of `ImportsPre` bindings which can then 30 /// be used to instantiate into a particular store. 31 /// 32 /// This method may fail if the component behind `instance_pre` 33 /// does not have the required exports. 34 pub fn new( 35 instance_pre: wasmtime::component::InstancePre<_T>, 36 ) -> wasmtime::Result<Self> { 37 let _component = instance_pre.component(); 38 Ok(ImportsPre { instance_pre }) 39 } 40 /// Instantiates a new instance of [`Imports`] 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<Imports> 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 Ok(Imports {}) 57 } 58 pub fn engine(&self) -> &wasmtime::Engine { 59 self.instance_pre.engine() 60 } 61 pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 62 &self.instance_pre 63 } 64 } 65 impl Imports { 66 /// Convenience wrapper around [`ImportsPre::new`] and 67 /// [`ImportsPre::instantiate_async`]. 68 pub async fn instantiate_async<_T>( 69 mut store: impl wasmtime::AsContextMut<Data = _T>, 70 component: &wasmtime::component::Component, 71 linker: &wasmtime::component::Linker<_T>, 72 ) -> wasmtime::Result<Imports> 73 where 74 _T: Send, 75 { 76 let pre = linker.instantiate_pre(component)?; 77 ImportsPre::new(pre)?.instantiate_async(store).await 78 } 79 pub fn add_to_linker<T, U>( 80 linker: &mut wasmtime::component::Linker<T>, 81 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 82 ) -> wasmtime::Result<()> 83 where 84 T: Send, 85 U: a::b::interface_with_live_type::Host 86 + a::b::interface_with_dead_type::Host + Send, 87 { 88 a::b::interface_with_live_type::add_to_linker(linker, get)?; 89 a::b::interface_with_dead_type::add_to_linker(linker, get)?; 90 Ok(()) 91 } 92 } 93 }; 94 pub mod a { 95 pub mod b { 96 #[allow(clippy::all)] 97 pub mod interface_with_live_type { 98 #[allow(unused_imports)] 99 use wasmtime::component::__internal::anyhow; 100 #[derive(wasmtime::component::ComponentType)] 101 #[derive(wasmtime::component::Lift)] 102 #[derive(wasmtime::component::Lower)] 103 #[component(record)] 104 #[derive(Clone, Copy)] 105 pub struct LiveType { 106 #[component(name = "a")] 107 pub a: u32, 108 } 109 impl core::fmt::Debug for LiveType { 110 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 111 f.debug_struct("LiveType").field("a", &self.a).finish() 112 } 113 } 114 const _: () = { 115 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); 116 assert!( 117 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 118 ); 119 }; 120 #[wasmtime::component::__internal::async_trait] 121 pub trait Host: Send { 122 async fn f(&mut self) -> LiveType; 123 } 124 pub trait GetHost< 125 T, 126 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 127 type Host: Host + Send; 128 } 129 impl<F, T, O> GetHost<T> for F 130 where 131 F: Fn(T) -> O + Send + Sync + Copy + 'static, 132 O: Host + Send, 133 { 134 type Host = O; 135 } 136 pub fn add_to_linker_get_host<T>( 137 linker: &mut wasmtime::component::Linker<T>, 138 host_getter: impl for<'a> GetHost<&'a mut T>, 139 ) -> wasmtime::Result<()> 140 where 141 T: Send, 142 { 143 let mut inst = linker.instance("a:b/interface-with-live-type")?; 144 inst.func_wrap_async( 145 "f", 146 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| wasmtime::component::__internal::Box::new(async move { 147 let host = &mut host_getter(caller.data_mut()); 148 let r = Host::f(host).await; 149 Ok((r,)) 150 }), 151 )?; 152 Ok(()) 153 } 154 pub fn add_to_linker<T, U>( 155 linker: &mut wasmtime::component::Linker<T>, 156 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 157 ) -> wasmtime::Result<()> 158 where 159 U: Host + Send, 160 T: Send, 161 { 162 add_to_linker_get_host(linker, get) 163 } 164 #[wasmtime::component::__internal::async_trait] 165 impl<_T: Host + ?Sized + Send> Host for &mut _T { 166 async fn f(&mut self) -> LiveType { 167 Host::f(*self).await 168 } 169 } 170 } 171 #[allow(clippy::all)] 172 pub mod interface_with_dead_type { 173 #[allow(unused_imports)] 174 use wasmtime::component::__internal::anyhow; 175 #[wasmtime::component::__internal::async_trait] 176 pub trait Host: Send {} 177 pub trait GetHost< 178 T, 179 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 180 type Host: Host + Send; 181 } 182 impl<F, T, O> GetHost<T> for F 183 where 184 F: Fn(T) -> O + Send + Sync + Copy + 'static, 185 O: Host + Send, 186 { 187 type Host = O; 188 } 189 pub fn add_to_linker_get_host<T>( 190 linker: &mut wasmtime::component::Linker<T>, 191 host_getter: impl for<'a> GetHost<&'a mut T>, 192 ) -> wasmtime::Result<()> 193 where 194 T: Send, 195 { 196 let mut inst = linker.instance("a:b/interface-with-dead-type")?; 197 Ok(()) 198 } 199 pub fn add_to_linker<T, U>( 200 linker: &mut wasmtime::component::Linker<T>, 201 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 202 ) -> wasmtime::Result<()> 203 where 204 U: Host + Send, 205 T: Send, 206 { 207 add_to_linker_get_host(linker, get) 208 } 209 #[wasmtime::component::__internal::async_trait] 210 impl<_T: Host + ?Sized + Send> Host for &mut _T {} 211 } 212 } 213 } 214