1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// copmonent 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 /// Auto-generated bindings for an instance a component which 11 /// implements the world `imports`. 12 /// 13 /// This structure is created through either 14 /// [`Imports::instantiate_async`] or by first creating 15 /// a [`ImportsPre`] followed by using 16 /// [`ImportsPre::instantiate_async`]. 17 pub struct Imports {} 18 const _: () = { 19 #[allow(unused_imports)] 20 use wasmtime::component::__internal::anyhow; 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 compoennt 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 _component = instance_pre.component(); 31 Ok(ImportsPre { instance_pre }) 32 } 33 /// Instantiates a new instance of [`Imports`] within the 34 /// `store` provided. 35 /// 36 /// This function will use `self` as the pre-instantiated 37 /// instance to perform instantiation. Afterwards the preloaded 38 /// indices in `self` are used to lookup all exports on the 39 /// resulting instance. 40 pub async fn instantiate_async( 41 &self, 42 mut store: impl wasmtime::AsContextMut<Data = _T>, 43 ) -> wasmtime::Result<Imports> 44 where 45 _T: Send, 46 { 47 let mut store = store.as_context_mut(); 48 let _instance = self.instance_pre.instantiate_async(&mut store).await?; 49 Ok(Imports {}) 50 } 51 } 52 impl Imports { 53 /// Convenience wrapper around [`ImportsPre::new`] and 54 /// [`ImportsPre::instantiate_async`]. 55 pub async fn instantiate_async<_T>( 56 mut store: impl wasmtime::AsContextMut<Data = _T>, 57 component: &wasmtime::component::Component, 58 linker: &wasmtime::component::Linker<_T>, 59 ) -> wasmtime::Result<Imports> 60 where 61 _T: Send, 62 { 63 let pre = linker.instantiate_pre(component)?; 64 ImportsPre::new(pre)?.instantiate_async(store).await 65 } 66 pub fn add_to_linker<T, U>( 67 linker: &mut wasmtime::component::Linker<T>, 68 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 69 ) -> wasmtime::Result<()> 70 where 71 T: Send, 72 U: a::b::interface_with_live_type::Host 73 + a::b::interface_with_dead_type::Host + Send, 74 { 75 a::b::interface_with_live_type::add_to_linker(linker, get)?; 76 a::b::interface_with_dead_type::add_to_linker(linker, get)?; 77 Ok(()) 78 } 79 } 80 }; 81 pub mod a { 82 pub mod b { 83 #[allow(clippy::all)] 84 pub mod interface_with_live_type { 85 #[allow(unused_imports)] 86 use wasmtime::component::__internal::anyhow; 87 #[derive(wasmtime::component::ComponentType)] 88 #[derive(wasmtime::component::Lift)] 89 #[derive(wasmtime::component::Lower)] 90 #[component(record)] 91 #[derive(Clone, Copy)] 92 pub struct LiveType { 93 #[component(name = "a")] 94 pub a: u32, 95 } 96 impl core::fmt::Debug for LiveType { 97 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 98 f.debug_struct("LiveType").field("a", &self.a).finish() 99 } 100 } 101 const _: () = { 102 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); 103 assert!( 104 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 105 ); 106 }; 107 #[wasmtime::component::__internal::async_trait] 108 pub trait Host: Send { 109 async fn f(&mut self) -> LiveType; 110 } 111 pub trait GetHost< 112 T, 113 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 114 type Host: Host + Send; 115 } 116 impl<F, T, O> GetHost<T> for F 117 where 118 F: Fn(T) -> O + Send + Sync + Copy + 'static, 119 O: Host + Send, 120 { 121 type Host = O; 122 } 123 pub fn add_to_linker_get_host<T>( 124 linker: &mut wasmtime::component::Linker<T>, 125 host_getter: impl for<'a> GetHost<&'a mut T>, 126 ) -> wasmtime::Result<()> 127 where 128 T: Send, 129 { 130 let mut inst = linker.instance("a:b/interface-with-live-type")?; 131 inst.func_wrap_async( 132 "f", 133 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| wasmtime::component::__internal::Box::new(async move { 134 let host = &mut host_getter(caller.data_mut()); 135 let r = Host::f(host).await; 136 Ok((r,)) 137 }), 138 )?; 139 Ok(()) 140 } 141 pub fn add_to_linker<T, U>( 142 linker: &mut wasmtime::component::Linker<T>, 143 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 144 ) -> wasmtime::Result<()> 145 where 146 U: Host + Send, 147 T: Send, 148 { 149 add_to_linker_get_host(linker, get) 150 } 151 #[wasmtime::component::__internal::async_trait] 152 impl<_T: Host + ?Sized + Send> Host for &mut _T { 153 async fn f(&mut self) -> LiveType { 154 Host::f(*self).await 155 } 156 } 157 } 158 #[allow(clippy::all)] 159 pub mod interface_with_dead_type { 160 #[allow(unused_imports)] 161 use wasmtime::component::__internal::anyhow; 162 #[wasmtime::component::__internal::async_trait] 163 pub trait Host: Send {} 164 pub trait GetHost< 165 T, 166 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 167 type Host: Host + Send; 168 } 169 impl<F, T, O> GetHost<T> for F 170 where 171 F: Fn(T) -> O + Send + Sync + Copy + 'static, 172 O: Host + Send, 173 { 174 type Host = O; 175 } 176 pub fn add_to_linker_get_host<T>( 177 linker: &mut wasmtime::component::Linker<T>, 178 host_getter: impl for<'a> GetHost<&'a mut T>, 179 ) -> wasmtime::Result<()> 180 where 181 T: Send, 182 { 183 let mut inst = linker.instance("a:b/interface-with-dead-type")?; 184 Ok(()) 185 } 186 pub fn add_to_linker<T, U>( 187 linker: &mut wasmtime::component::Linker<T>, 188 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 189 ) -> wasmtime::Result<()> 190 where 191 U: Host + Send, 192 T: Send, 193 { 194 add_to_linker_get_host(linker, get) 195 } 196 #[wasmtime::component::__internal::async_trait] 197 impl<_T: Host + ?Sized + Send> Host for &mut _T {} 198 } 199 } 200 } 201