1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
use crate::*; use squote::{quote, TokenStream}; #[derive(Debug)] pub struct Interface { pub name: TypeName, pub guid: TypeGuid, pub interfaces: Vec<RequiredInterface>, } impl Interface { pub fn from_type_name(name: TypeName) -> Self { let guid = TypeGuid::from_type_def(&name.def); let mut interfaces = Vec::new(); add_type( &mut interfaces, &name.def, &name.namespace, InterfaceKind::Default, ); add_dependencies(&mut interfaces, &name, &name.namespace, true); rename_collisions(&mut interfaces); Self { name, guid, interfaces, } } pub fn dependencies(&self) -> Vec<winmd::TypeDef> { let mut dependencies = Vec::new(); for interface in &self.interfaces { dependencies.append(&mut interface.name.dependencies()); if interface.kind == InterfaceKind::Default { for method in &interface.methods { dependencies.append(&mut method.dependencies()); } } } dependencies } pub fn default_interface(&self) -> &RequiredInterface { self.interfaces .iter() .find(|i| i.kind == InterfaceKind::Default) .unwrap() } pub fn gen(&self) -> TokenStream { let definition = self.name.gen_definition(); let abi_definition = self.name.gen_abi_definition(); let name = self.name.gen(); let phantoms = self.name.phantoms(); let constraints = self.name.gen_constraint(); let default_interface = self.default_interface(); let guid = self.name.gen_guid(&self.guid); let signature = self.name.gen_signature(&format!("{{{:#?}}}", &self.guid)); let conversions = self .interfaces .iter() .filter(|interface| interface.kind != InterfaceKind::Default) .map(|interface| interface.gen_conversions(&name, &constraints)); let methods = gen_method(&self.interfaces); let abi_methods = default_interface.methods.iter().map(|method| { let signature = method.gen_abi(); quote! { pub unsafe extern "system" fn #signature } }); let iterator = gen_iterator(&self.name, &self.interfaces); let (async_get, future) = gen_async(&self.name, &self.interfaces); quote! { #[repr(transparent)] pub struct #definition(::windows::Object, #phantoms) where #constraints; impl<#constraints> ::std::clone::Clone for #name { fn clone(&self) -> Self { Self(self.0.clone(), #phantoms) } } impl<#constraints> ::std::fmt::Debug for #name { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { write!(f, "{:?}", self.0) } } impl<#constraints> ::std::cmp::PartialEq for #name { fn eq(&self, other: &Self) -> bool { self.0 == other.0 } } impl<#constraints> ::std::cmp::Eq for #name {} impl<#constraints> #name { #methods #async_get } unsafe impl<#constraints> ::windows::Interface for #name { type Vtable = #abi_definition; const IID: ::windows::Guid = #guid; } #[repr(C)] #[doc(hidden)] pub struct #abi_definition( pub unsafe extern "system" fn(this: ::windows::RawPtr, iid: &::windows::Guid, interface: *mut ::windows::RawPtr) -> ::windows::ErrorCode, pub unsafe extern "system" fn(this: ::windows::RawPtr) -> u32, pub unsafe extern "system" fn(this: ::windows::RawPtr) -> u32, pub unsafe extern "system" fn(this: ::windows::RawPtr, count: *mut u32, values: *mut *mut ::windows::Guid) -> ::windows::ErrorCode, pub unsafe extern "system" fn(this: ::windows::RawPtr, value: *mut ::windows::RawPtr) -> ::windows::ErrorCode, pub unsafe extern "system" fn(this: ::windows::RawPtr, value: *mut i32) -> ::windows::ErrorCode, #(#abi_methods,)* #phantoms ) where #constraints; unsafe impl<#constraints> ::windows::RuntimeType for #name { type DefaultType = ::std::option::Option<Self>; const SIGNATURE: ::windows::ConstBuffer = { #signature }; } impl<#constraints> ::std::convert::From<#name> for ::windows::Object { fn from(value: #name) -> Self { value.0 } } impl<#constraints> ::std::convert::From<&#name> for ::windows::Object { fn from(value: &#name) -> Self { ::std::convert::From::from(::std::clone::Clone::clone(value)) } } impl<'a, #constraints> ::std::convert::Into<::windows::Param<'a, ::windows::Object>> for #name { fn into(self) -> ::windows::Param<'a, ::windows::Object> { ::windows::Param::Owned(::std::convert::Into::<::windows::Object>::into(self)) } } impl<'a, #constraints> ::std::convert::Into<::windows::Param<'a, ::windows::Object>> for &'a #name { fn into(self) -> ::windows::Param<'a, ::windows::Object> { ::windows::Param::Owned(::std::convert::Into::<::windows::Object>::into(::std::clone::Clone::clone(self))) } } #(#conversions)* #iterator #future } } } #[cfg(test)] mod tests { use crate::*; fn interface((namespace, type_name): (&str, &str)) -> Interface { let reader = &winmd::TypeReader::get(); let t = reader.expect_type_def((namespace, type_name)); let t = TypeDefinition::from_type_def(&t); match t { TypeDefinition::Interface(t) => t, _ => panic!("TypeDefinition not an interface"), } } #[test] fn test_stringable() { let t = interface(("Windows.Foundation", "IStringable")); assert!(t.name.runtime_name() == "Windows.Foundation.IStringable"); assert!(t.interfaces.len() == 1); assert!(format!("{:#?}", &t.guid) == "96369f54-8eb6-48f0-abce-c1b211e627c3"); let t = t .interfaces .iter() .find(|i| i.kind == InterfaceKind::Default) .unwrap(); assert!(t.name.runtime_name() == "Windows.Foundation.IStringable"); assert!(t.kind == InterfaceKind::Default); assert!(t.methods.len() == 1); let method = &t.methods[0]; assert!(method.name == "to_string"); assert!(method.signature.params.is_empty()); let param = method.signature.return_type.as_ref().unwrap(); assert!(param.kind == TypeKind::String); } #[test] fn test_async_action() { let t = interface(("Windows.Foundation", "IAsyncAction")); assert!(t.name.runtime_name() == "Windows.Foundation.IAsyncAction"); assert!(t.interfaces.len() == 2); let interface = t .interfaces .iter() .find(|interface| interface.name.name == "IAsyncInfo") .unwrap(); assert!(interface.kind == InterfaceKind::NonDefault); assert!(interface.name.runtime_name() == "Windows.Foundation.IAsyncInfo"); let interface = t .interfaces .iter() .find(|interface| interface.name.name == "IAsyncAction") .unwrap(); assert!(interface.kind == InterfaceKind::Default); assert!(interface.name.runtime_name() == "Windows.Foundation.IAsyncAction"); } #[test] fn test_observable_map() { let t = interface(("Windows.Foundation.Collections", "IObservableMap`2")); assert!(t.name.runtime_name() == "Windows.Foundation.Collections.IObservableMap`2<K, V>"); assert!(t.interfaces.len() == 3); let default_interface = t .interfaces .iter() .find(|required| required.name.name == "IObservableMap`2") .unwrap(); assert!(default_interface.kind == InterfaceKind::Default); assert!(default_interface.methods.len() == 2); assert!(default_interface.methods[0].name == "map_changed"); assert!(default_interface.methods[1].name == "remove_map_changed"); let map = t .interfaces .iter() .find(|required| required.name.name == "IMap`2") .unwrap(); assert!(map.kind == InterfaceKind::NonDefault); assert!(map.name.runtime_name() == "Windows.Foundation.Collections.IMap`2<K, V>"); let iterable = t .interfaces .iter() .find(|required| required.name.name == "IIterable`1") .unwrap(); assert!(iterable.kind == InterfaceKind::NonDefault); assert!(iterable.name.runtime_name() == "Windows.Foundation.Collections.IIterable`1<Windows.Foundation.Collections.IKeyValuePair`2<K, V>>"); } }