From 23cbec85e9628cb1e50bd1300fed59abd9b0c5ff Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Thu, 14 Mar 2019 19:22:52 -0400 Subject: [PATCH] Use BTreeMap only where absolutely necessary --- generator/src/lib.rs | 56 ++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/generator/src/lib.rs b/generator/src/lib.rs index c9656c8..63d6c7a 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -14,7 +14,7 @@ use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; use itertools::Itertools; use proc_macro2::Term; use quote::Tokens; -use std::collections::{BTreeMap, BTreeSet}; +use std::collections::{BTreeMap, HashMap, HashSet}; use std::path::Path; use syn::Ident; pub trait ExtensionExt {} @@ -681,12 +681,12 @@ impl FieldExt for vkxml::Field { } } -pub type CommandMap<'a> = BTreeMap; +pub type CommandMap<'a> = HashMap; fn generate_function_pointers<'a>( ident: Ident, commands: &[&'a vkxml::Command], - fn_cache: &mut BTreeSet<&'a str>, + fn_cache: &mut HashSet<&'a str>, ) -> quote::Tokens { let names: Vec<_> = commands.iter().map(|cmd| cmd.command_ident()).collect(); let names_ref = &names; @@ -874,7 +874,7 @@ pub fn generate_extension_constants<'a>( extension_name: &str, extension_number: i64, extension_items: &'a [vk_parse::ExtensionChild], - const_cache: &mut BTreeSet<&'a str>, + const_cache: &mut HashSet<&'a str>, const_values: &mut BTreeMap>, ) -> quote::Tokens { use vk_parse::EnumSpec; @@ -939,7 +939,7 @@ pub fn generate_extension_commands<'a>( extension_name: &str, items: &[vk_parse::ExtensionChild], cmd_map: &CommandMap<'a>, - fn_cache: &mut BTreeSet<&'a str>, + fn_cache: &mut HashSet<&'a str>, ) -> Tokens { let commands = items .iter() @@ -978,9 +978,9 @@ pub fn generate_extension_commands<'a>( pub fn generate_extension<'a>( extension: &'a vk_parse::Extension, cmd_map: &CommandMap<'a>, - const_cache: &mut BTreeSet<&'a str>, + const_cache: &mut HashSet<&'a str>, const_values: &mut BTreeMap>, - fn_cache: &mut BTreeSet<&'a str>, + fn_cache: &mut HashSet<&'a str>, ) -> Option { // Okay this is a little bit odd. We need to generate all extensions, even disabled ones, // because otherwise some StructureTypes won't get generated. But we don't generate extensions @@ -1011,7 +1011,7 @@ pub fn generate_typedef(typedef: &vkxml::Typedef) -> Tokens { } pub fn generate_bitmask( bitmask: &vkxml::Bitmask, - bitflags_cache: &mut BTreeSet, + bitflags_cache: &mut HashSet, ) -> Option { // Workaround for empty bitmask if bitmask.name.is_empty() { @@ -1113,9 +1113,9 @@ pub fn bitflags_impl_block( pub fn generate_enum<'a>( _enum: &'a vkxml::Enumeration, - const_cache: &mut BTreeSet<&'a str>, + const_cache: &mut HashSet<&'a str>, const_values: &mut BTreeMap>, - bitflags_cache: &mut BTreeSet, + bitflags_cache: &mut HashSet, ) -> EnumType { let name = &_enum.name[2..]; let _name = name.replace("FlagBits", "Flags"); @@ -1328,7 +1328,7 @@ pub fn derive_default(_struct: &vkxml::Struct) -> Option { }; Some(q) } -pub fn derive_debug(_struct: &vkxml::Struct, union_types: &BTreeSet<&str>) -> Option { +pub fn derive_debug(_struct: &vkxml::Struct, union_types: &HashSet<&str>) -> Option { let name = name_to_tokens(&_struct.name); let members = _struct.elements.iter().filter_map(|elem| match *elem { vkxml::StructElement::Member(ref field) => Some(field), @@ -1389,7 +1389,7 @@ pub fn derive_debug(_struct: &vkxml::Struct, union_types: &BTreeSet<&str>) -> Op pub fn derive_setters( _struct: &vkxml::Struct, - root_struct_names: &BTreeSet, + root_struct_names: &HashSet, ) -> Option { if &_struct.name == "VkBaseInStructure" || &_struct.name == "VkBaseOutStructure" { return None; @@ -1716,8 +1716,8 @@ pub fn manual_derives(_struct: &vkxml::Struct) -> Tokens { } pub fn generate_struct( _struct: &vkxml::Struct, - root_struct_names: &BTreeSet, - union_types: &BTreeSet<&str>, + root_struct_names: &HashSet, + union_types: &HashSet<&str>, ) -> Tokens { let name = name_to_tokens(&_struct.name); let members = _struct.elements.iter().filter_map(|elem| match *elem { @@ -1819,7 +1819,7 @@ fn generate_union(union: &vkxml::Union) -> Tokens { } } } -pub fn root_struct_names(definitions: &[&vkxml::DefinitionsElement]) -> BTreeSet { +pub fn root_struct_names(definitions: &[&vkxml::DefinitionsElement]) -> HashSet { definitions .iter() .filter_map(|definition| match *definition { @@ -1837,9 +1837,9 @@ pub fn root_struct_names(definitions: &[&vkxml::DefinitionsElement]) -> BTreeSet } pub fn generate_definition( definition: &vkxml::DefinitionsElement, - union_types: &BTreeSet<&str>, - root_structs: &BTreeSet, - bitflags_cache: &mut BTreeSet, + union_types: &HashSet<&str>, + root_structs: &HashSet, + bitflags_cache: &mut HashSet, ) -> Option { match *definition { vkxml::DefinitionsElement::Typedef(ref typedef) => Some(generate_typedef(typedef)), @@ -1856,7 +1856,7 @@ pub fn generate_definition( pub fn generate_feature<'a>( feature: &vkxml::Feature, commands: &CommandMap<'a>, - fn_cache: &mut BTreeSet<&'a str>, + fn_cache: &mut HashSet<&'a str>, ) -> quote::Tokens { let (static_commands, entry_commands, device_commands, instance_commands) = feature .elements @@ -1933,7 +1933,7 @@ pub fn constant_name(name: &str) -> String { pub fn generate_constant<'a>( constant: &'a vkxml::Constant, - cache: &mut BTreeSet<&'a str>, + cache: &mut HashSet<&'a str>, ) -> Tokens { cache.insert(constant.name.as_str()); let c = Constant::from_constant(constant); @@ -1953,7 +1953,7 @@ pub fn generate_constant<'a>( pub fn generate_feature_extension<'a>( registry: &'a vk_parse::Registry, - const_cache: &mut BTreeSet<&'a str>, + const_cache: &mut HashSet<&'a str>, const_values: &mut BTreeMap>, ) -> Tokens { let constants = registry.0.iter().filter_map(|item| match item { @@ -2035,7 +2035,7 @@ pub fn generate_const_displays<'a>(const_values: &BTreeMap>) - } pub fn generate_aliases_of_types<'a>( types: &'a vk_parse::Types, - ty_cache: &mut BTreeSet, + ty_cache: &mut HashSet, ) -> Tokens { let aliases = types .children @@ -2073,7 +2073,7 @@ pub fn write_source_code(path: &Path) { }) .nth(0) .expect("extension"); - let mut ty_cache = BTreeSet::new(); + let mut ty_cache = HashSet::new(); let aliases: Vec<_> = spec2 .0 .iter() @@ -2086,7 +2086,7 @@ pub fn write_source_code(path: &Path) { .collect(); let spec = vk_parse::parse_file_as_vkxml(path); - let commands: BTreeMap = spec + let commands: HashMap = spec .elements .iter() .filter_map(|elem| match elem { @@ -2141,9 +2141,9 @@ pub fn write_source_code(path: &Path) { .flat_map(|constants| constants.elements.iter()) .collect(); - let mut fn_cache = BTreeSet::new(); - let mut bitflags_cache = BTreeSet::new(); - let mut const_cache = BTreeSet::new(); + let mut fn_cache = HashSet::new(); + let mut bitflags_cache = HashSet::new(); + let mut const_cache = HashSet::new(); let mut const_values: BTreeMap> = BTreeMap::new(); @@ -2181,7 +2181,7 @@ pub fn write_source_code(path: &Path) { vkxml::DefinitionsElement::Union(ref union) => Some(union.name.as_str()), _ => None, }) - .collect::>(); + .collect::>(); let root_names = root_struct_names(&definitions); let definition_code: Vec<_> = definitions