1 pub struct Imports {} 2 const _: () = { 3 #[allow(unused_imports)] 4 use wasmtime::component::__internal::anyhow; 5 impl Imports { 6 pub fn add_to_linker<T, U>( 7 linker: &mut wasmtime::component::Linker<T>, 8 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 9 ) -> wasmtime::Result<()> 10 where 11 U: a::b::interface_with_live_type::Host 12 + a::b::interface_with_dead_type::Host, 13 { 14 a::b::interface_with_live_type::add_to_linker(linker, get)?; 15 a::b::interface_with_dead_type::add_to_linker(linker, get)?; 16 Ok(()) 17 } 18 /// Instantiates the provided `module` using the specified 19 /// parameters, wrapping up the result in a structure that 20 /// translates between wasm and the host. 21 pub fn instantiate<T>( 22 mut store: impl wasmtime::AsContextMut<Data = T>, 23 component: &wasmtime::component::Component, 24 linker: &wasmtime::component::Linker<T>, 25 ) -> wasmtime::Result<(Self, wasmtime::component::Instance)> { 26 let instance = linker.instantiate(&mut store, component)?; 27 Ok((Self::new(store, &instance)?, instance)) 28 } 29 /// Instantiates a pre-instantiated module using the specified 30 /// parameters, wrapping up the result in a structure that 31 /// translates between wasm and the host. 32 pub fn instantiate_pre<T>( 33 mut store: impl wasmtime::AsContextMut<Data = T>, 34 instance_pre: &wasmtime::component::InstancePre<T>, 35 ) -> wasmtime::Result<(Self, wasmtime::component::Instance)> { 36 let instance = instance_pre.instantiate(&mut store)?; 37 Ok((Self::new(store, &instance)?, instance)) 38 } 39 /// Low-level creation wrapper for wrapping up the exports 40 /// of the `instance` provided in this structure of wasm 41 /// exports. 42 /// 43 /// This function will extract exports from the `instance` 44 /// defined within `store` and wrap them all up in the 45 /// returned structure which can be used to interact with 46 /// the wasm module. 47 pub fn new( 48 mut store: impl wasmtime::AsContextMut, 49 instance: &wasmtime::component::Instance, 50 ) -> wasmtime::Result<Self> { 51 let mut store = store.as_context_mut(); 52 let mut exports = instance.exports(&mut store); 53 let mut __exports = exports.root(); 54 Ok(Imports {}) 55 } 56 } 57 }; 58 pub mod a { 59 pub mod b { 60 #[allow(clippy::all)] 61 pub mod interface_with_live_type { 62 #[allow(unused_imports)] 63 use wasmtime::component::__internal::anyhow; 64 #[derive(wasmtime::component::ComponentType)] 65 #[derive(wasmtime::component::Lift)] 66 #[derive(wasmtime::component::Lower)] 67 #[component(record)] 68 #[derive(Clone, Copy)] 69 pub struct LiveType { 70 #[component(name = "a")] 71 pub a: u32, 72 } 73 impl core::fmt::Debug for LiveType { 74 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 75 f.debug_struct("LiveType").field("a", &self.a).finish() 76 } 77 } 78 const _: () = { 79 assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); 80 assert!( 81 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 82 ); 83 }; 84 pub trait Host { 85 fn f(&mut self) -> LiveType; 86 } 87 pub trait GetHost< 88 T, 89 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 90 type Host: Host; 91 } 92 impl<F, T, O> GetHost<T> for F 93 where 94 F: Fn(T) -> O + Send + Sync + Copy + 'static, 95 O: Host, 96 { 97 type Host = O; 98 } 99 pub fn add_to_linker_get_host<T>( 100 linker: &mut wasmtime::component::Linker<T>, 101 host_getter: impl for<'a> GetHost<&'a mut T>, 102 ) -> wasmtime::Result<()> { 103 let mut inst = linker.instance("a:b/interface-with-live-type")?; 104 inst.func_wrap( 105 "f", 106 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 107 let host = &mut host_getter(caller.data_mut()); 108 let r = Host::f(host); 109 Ok((r,)) 110 }, 111 )?; 112 Ok(()) 113 } 114 pub fn add_to_linker<T, U>( 115 linker: &mut wasmtime::component::Linker<T>, 116 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 117 ) -> wasmtime::Result<()> 118 where 119 U: Host, 120 { 121 add_to_linker_get_host(linker, get) 122 } 123 impl<_T: Host + ?Sized> Host for &mut _T { 124 fn f(&mut self) -> LiveType { 125 Host::f(*self) 126 } 127 } 128 } 129 #[allow(clippy::all)] 130 pub mod interface_with_dead_type { 131 #[allow(unused_imports)] 132 use wasmtime::component::__internal::anyhow; 133 pub trait Host {} 134 pub trait GetHost< 135 T, 136 >: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static { 137 type Host: Host; 138 } 139 impl<F, T, O> GetHost<T> for F 140 where 141 F: Fn(T) -> O + Send + Sync + Copy + 'static, 142 O: Host, 143 { 144 type Host = O; 145 } 146 pub fn add_to_linker_get_host<T>( 147 linker: &mut wasmtime::component::Linker<T>, 148 host_getter: impl for<'a> GetHost<&'a mut T>, 149 ) -> wasmtime::Result<()> { 150 let mut inst = linker.instance("a:b/interface-with-dead-type")?; 151 Ok(()) 152 } 153 pub fn add_to_linker<T, U>( 154 linker: &mut wasmtime::component::Linker<T>, 155 get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, 156 ) -> wasmtime::Result<()> 157 where 158 U: Host, 159 { 160 add_to_linker_get_host(linker, get) 161 } 162 impl<_T: Host + ?Sized> Host for &mut _T {} 163 } 164 } 165 } 166