1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// component which implements the world `neptune`. 3 /// 4 /// This structure is created through [`NeptunePre::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 [`Neptune`] as well. 9 pub struct NeptunePre<T: 'static> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: NeptuneIndices, 12 } 13 impl<T: 'static> Clone for NeptunePre<T> { clone(&self) -> Self14 fn clone(&self) -> Self { 15 Self { 16 instance_pre: self.instance_pre.clone(), 17 indices: self.indices.clone(), 18 } 19 } 20 } 21 impl<_T: 'static> NeptunePre<_T> { 22 /// Creates a new copy of `NeptunePre` 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. new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>27 pub fn new( 28 instance_pre: wasmtime::component::InstancePre<_T>, 29 ) -> wasmtime::Result<Self> { 30 let indices = NeptuneIndices::new(&instance_pre)?; 31 Ok(Self { instance_pre, indices }) 32 } engine(&self) -> &wasmtime::Engine33 pub fn engine(&self) -> &wasmtime::Engine { 34 self.instance_pre.engine() 35 } instance_pre(&self) -> &wasmtime::component::InstancePre<_T>36 pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 37 &self.instance_pre 38 } 39 /// Instantiates a new instance of [`Neptune`] 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. instantiate( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Neptune>46 pub fn instantiate( 47 &self, 48 mut store: impl wasmtime::AsContextMut<Data = _T>, 49 ) -> wasmtime::Result<Neptune> { 50 let mut store = store.as_context_mut(); 51 let instance = self.instance_pre.instantiate(&mut store)?; 52 self.indices.load(&mut store, &instance) 53 } 54 } 55 impl<_T: Send + 'static> NeptunePre<_T> { 56 /// Same as [`Self::instantiate`], except with `async`. instantiate_async( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Neptune>57 pub async fn instantiate_async( 58 &self, 59 mut store: impl wasmtime::AsContextMut<Data = _T>, 60 ) -> wasmtime::Result<Neptune> { 61 let mut store = store.as_context_mut(); 62 let instance = self.instance_pre.instantiate_async(&mut store).await?; 63 self.indices.load(&mut store, &instance) 64 } 65 } 66 /// Auto-generated bindings for index of the exports of 67 /// `neptune`. 68 /// 69 /// This is an implementation detail of [`NeptunePre`] and can 70 /// be constructed if needed as well. 71 /// 72 /// For more information see [`Neptune`] as well. 73 #[derive(Clone)] 74 pub struct NeptuneIndices {} 75 /// Auto-generated bindings for an instance a component which 76 /// implements the world `neptune`. 77 /// 78 /// This structure can be created through a number of means 79 /// depending on your requirements and what you have on hand: 80 /// 81 /// * The most convenient way is to use 82 /// [`Neptune::instantiate`] which only needs a 83 /// [`Store`], [`Component`], and [`Linker`]. 84 /// 85 /// * Alternatively you can create a [`NeptunePre`] ahead of 86 /// time with a [`Component`] to front-load string lookups 87 /// of exports once instead of per-instantiation. This 88 /// method then uses [`NeptunePre::instantiate`] to 89 /// create a [`Neptune`]. 90 /// 91 /// * If you've instantiated the instance yourself already 92 /// then you can use [`Neptune::new`]. 93 /// 94 /// These methods are all equivalent to one another and move 95 /// around the tradeoff of what work is performed when. 96 /// 97 /// [`Store`]: wasmtime::Store 98 /// [`Component`]: wasmtime::component::Component 99 /// [`Linker`]: wasmtime::component::Linker 100 pub struct Neptune {} 101 const _: () = { 102 impl NeptuneIndices { 103 /// Creates a new copy of `NeptuneIndices` bindings which can then 104 /// be used to instantiate into a particular store. 105 /// 106 /// This method may fail if the component does not have the 107 /// required exports. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>108 pub fn new<_T>( 109 _instance_pre: &wasmtime::component::InstancePre<_T>, 110 ) -> wasmtime::Result<Self> { 111 let _component = _instance_pre.component(); 112 let _instance_type = _instance_pre.instance_type(); 113 Ok(NeptuneIndices {}) 114 } 115 /// Uses the indices stored in `self` to load an instance 116 /// of [`Neptune`] from the instance provided. 117 /// 118 /// Note that at this time this method will additionally 119 /// perform type-checks of all exports. load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Neptune>120 pub fn load( 121 &self, 122 mut store: impl wasmtime::AsContextMut, 123 instance: &wasmtime::component::Instance, 124 ) -> wasmtime::Result<Neptune> { 125 let _ = &mut store; 126 let _instance = instance; 127 Ok(Neptune {}) 128 } 129 } 130 impl Neptune { 131 /// Convenience wrapper around [`NeptunePre::new`] and 132 /// [`NeptunePre::instantiate`]. instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Neptune>133 pub fn instantiate<_T>( 134 store: impl wasmtime::AsContextMut<Data = _T>, 135 component: &wasmtime::component::Component, 136 linker: &wasmtime::component::Linker<_T>, 137 ) -> wasmtime::Result<Neptune> { 138 let pre = linker.instantiate_pre(component)?; 139 NeptunePre::new(pre)?.instantiate(store) 140 } 141 /// Convenience wrapper around [`NeptuneIndices::new`] and 142 /// [`NeptuneIndices::load`]. new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Neptune>143 pub fn new( 144 mut store: impl wasmtime::AsContextMut, 145 instance: &wasmtime::component::Instance, 146 ) -> wasmtime::Result<Neptune> { 147 let indices = NeptuneIndices::new(&instance.instance_pre(&store))?; 148 indices.load(&mut store, instance) 149 } 150 /// Convenience wrapper around [`NeptunePre::new`] and 151 /// [`NeptunePre::instantiate_async`]. instantiate_async<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Neptune> where _T: Send,152 pub async fn instantiate_async<_T>( 153 store: impl wasmtime::AsContextMut<Data = _T>, 154 component: &wasmtime::component::Component, 155 linker: &wasmtime::component::Linker<_T>, 156 ) -> wasmtime::Result<Neptune> 157 where 158 _T: Send, 159 { 160 let pre = linker.instantiate_pre(component)?; 161 NeptunePre::new(pre)?.instantiate_async(store).await 162 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: foo::foo::green::HostWithStore + foo::foo::red::HostWithStore + Send, for<'a> D::Data<'a>: foo::foo::green::Host + foo::foo::red::Host + Send, T: 'static + Send,163 pub fn add_to_linker<T, D>( 164 linker: &mut wasmtime::component::Linker<T>, 165 host_getter: fn(&mut T) -> D::Data<'_>, 166 ) -> wasmtime::Result<()> 167 where 168 D: foo::foo::green::HostWithStore + foo::foo::red::HostWithStore + Send, 169 for<'a> D::Data<'a>: foo::foo::green::Host + foo::foo::red::Host + Send, 170 T: 'static + Send, 171 { 172 foo::foo::green::add_to_linker::<T, D>(linker, host_getter)?; 173 foo::foo::red::add_to_linker::<T, D>(linker, host_getter)?; 174 Ok(()) 175 } 176 } 177 }; 178 pub mod foo { 179 pub mod foo { 180 #[allow(clippy::all)] 181 pub mod green { 182 #[allow(unused_imports)] 183 use wasmtime::component::__internal::Box; 184 pub type Thing = i32; 185 const _: () = { 186 assert!(4 == < Thing as wasmtime::component::ComponentType >::SIZE32); 187 assert!(4 == < Thing as wasmtime::component::ComponentType >::ALIGN32); 188 }; 189 pub trait HostWithStore: wasmtime::component::HasData {} 190 impl<_T: ?Sized> HostWithStore for _T 191 where 192 _T: wasmtime::component::HasData, 193 {} 194 pub trait Host {} 195 impl<_T: Host + ?Sized> Host for &mut _T {} add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static,196 pub fn add_to_linker<T, D>( 197 linker: &mut wasmtime::component::Linker<T>, 198 host_getter: fn(&mut T) -> D::Data<'_>, 199 ) -> wasmtime::Result<()> 200 where 201 D: HostWithStore, 202 for<'a> D::Data<'a>: Host, 203 T: 'static, 204 { 205 let mut inst = linker.instance("foo:foo/green")?; 206 Ok(()) 207 } 208 } 209 #[allow(clippy::all)] 210 pub mod red { 211 #[allow(unused_imports)] 212 use wasmtime::component::__internal::Box; 213 pub type Thing = super::super::super::foo::foo::green::Thing; 214 const _: () = { 215 assert!(4 == < Thing as wasmtime::component::ComponentType >::SIZE32); 216 assert!(4 == < Thing as wasmtime::component::ComponentType >::ALIGN32); 217 }; 218 pub trait HostWithStore: wasmtime::component::HasData + Send {} 219 impl<_T: ?Sized> HostWithStore for _T 220 where 221 _T: wasmtime::component::HasData + Send, 222 {} 223 pub trait Host: Send { foo(&mut self) -> impl ::core::future::Future<Output = Thing> + Send224 fn foo(&mut self) -> impl ::core::future::Future<Output = Thing> + Send; 225 } 226 impl<_T: Host + ?Sized + Send> Host for &mut _T { foo(&mut self) -> impl ::core::future::Future<Output = Thing> + Send227 fn foo(&mut self) -> impl ::core::future::Future<Output = Thing> + Send { 228 async move { Host::foo(*self).await } 229 } 230 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static + Send,231 pub fn add_to_linker<T, D>( 232 linker: &mut wasmtime::component::Linker<T>, 233 host_getter: fn(&mut T) -> D::Data<'_>, 234 ) -> wasmtime::Result<()> 235 where 236 D: HostWithStore, 237 for<'a> D::Data<'a>: Host, 238 T: 'static + Send, 239 { 240 let mut inst = linker.instance("foo:foo/red")?; 241 inst.func_wrap_async( 242 "foo", 243 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 244 wasmtime::component::__internal::Box::new(async move { 245 let host = &mut host_getter(caller.data_mut()); 246 let r = Host::foo(host).await; 247 Ok((r,)) 248 }) 249 }, 250 )?; 251 Ok(()) 252 } 253 } 254 } 255 } 256