Temporary implement Hash and PartialEq for certain types that only

contain primtive types
This commit is contained in:
Maik Klein 2018-08-19 10:10:11 +02:00
parent 20d5ba9f7f
commit c4e4ab8fc2
3 changed files with 194 additions and 445 deletions

File diff suppressed because it is too large Load diff

View file

@ -5,8 +5,7 @@ authors = ["Maik Klein <maikklein@googlemail.com>"]
[dependencies] [dependencies]
vk-parse = "0.1" vk-parse = "0.1"
vkxml = {git = "https://github.com/terrybrashaw/vkxml"} vkxml = "0.3"
#vkxml = {git = "https://github.com/maikklein/vkxml"}
nom = "4.0" nom = "4.0"
heck = "0.3" heck = "0.3"
proc-macro2 = "0.2" proc-macro2 = "0.2"

View file

@ -1092,7 +1092,7 @@ fn is_static_array(field: &vkxml::Field) -> bool {
_ => false, _ => false,
}).unwrap_or(false) }).unwrap_or(false)
} }
pub fn derive_default(_struct: &vkxml::Struct, union_types: &HashSet<&str>) -> Option<Tokens> { pub fn derive_default(_struct: &vkxml::Struct) -> Option<Tokens> {
let name = name_to_tokens(&_struct.name); let name = name_to_tokens(&_struct.name);
let members = _struct.elements.iter().filter_map(|elem| match *elem { let members = _struct.elements.iter().filter_map(|elem| match *elem {
vkxml::StructElement::Member(ref field) => Some(field), vkxml::StructElement::Member(ref field) => Some(field),
@ -1219,6 +1219,27 @@ pub fn derive_debug(_struct: &vkxml::Struct, union_types: &HashSet<&str>) -> Opt
}; };
Some(q) Some(q)
} }
pub fn auto_derive_partial_eq_hash(_struct: &vkxml::Struct) -> Tokens {
// TODO: Properly detect which types can implement PartialEq and Hash.
// At the moment we only implement it for structs that contain primitivet
// types.
let is_primitive = _struct
.elements
.iter()
.filter_map(|elem| match *elem {
vkxml::StructElement::Member(ref field) => Some(field),
_ => None,
}).all(|field| match field.basetype.as_str() {
"c_float" | "uint32_t" => true,
_ => false,
});
if is_primitive {
quote!(Hash, PartialEq,)
} else {
quote!{}
}
}
pub fn generate_struct(_struct: &vkxml::Struct, union_types: &HashSet<&str>) -> Tokens { pub fn generate_struct(_struct: &vkxml::Struct, union_types: &HashSet<&str>) -> Tokens {
let name = name_to_tokens(&_struct.name); let name = name_to_tokens(&_struct.name);
let members = _struct.elements.iter().filter_map(|elem| match *elem { let members = _struct.elements.iter().filter_map(|elem| match *elem {
@ -1233,7 +1254,8 @@ pub fn generate_struct(_struct: &vkxml::Struct, union_types: &HashSet<&str>) ->
}); });
let debug_tokens = derive_debug(_struct, union_types); let debug_tokens = derive_debug(_struct, union_types);
let default_tokens = derive_default(_struct, union_types); let default_tokens = derive_default(_struct);
let partial_eq_hash_str = auto_derive_partial_eq_hash(_struct);
let dbg_str = if debug_tokens.is_none() { let dbg_str = if debug_tokens.is_none() {
quote!(Debug,) quote!(Debug,)
} else { } else {
@ -1246,7 +1268,7 @@ pub fn generate_struct(_struct: &vkxml::Struct, union_types: &HashSet<&str>) ->
}; };
quote!{ quote!{
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone, #default_str #dbg_str)] #[derive(Copy, Clone, #default_str #dbg_str #partial_eq_hash_str)]
pub struct #name { pub struct #name {
#(#params,)* #(#params,)*
} }