Skip to content

Commit

Permalink
perf(App): changes BTreeMap to VecMap in some instances
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Nov 8, 2015
1 parent 111745c commit 64b921d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -12,7 +12,8 @@ license = "MIT"
keywords = ["argument", "command", "arg", "parser", "parse"]

[dependencies]
bitflags = "0.3.2"
bitflags = "0.3.2"
vec_map = "0.3"
ansi_term = { version = "~0.7", optional = true }
strsim = { version = "~0.4.0", optional = true }
yaml-rust = { version = "~0.2.2", optional = true }
Expand Down
17 changes: 9 additions & 8 deletions src/app/app.rs
Expand Up @@ -8,6 +8,7 @@ use std::borrow::Borrow;

#[cfg(feature = "yaml")]
use yaml_rust::Yaml;
use vec_map::VecMap;

use args::{Arg, ArgMatches, MatchedArg, SubCommand};
use args::{FlagBuilder, OptBuilder, PosBuilder};
Expand Down Expand Up @@ -71,8 +72,8 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
// A list of possible options
opts: BTreeMap<&'ar str, OptBuilder<'ar>>,
// A list of positional arguments
positionals_idx: BTreeMap<u8, PosBuilder<'ar>>,
positionals_name: HashMap<&'ar str, u8>,
positionals_idx: VecMap<PosBuilder<'ar>>,
positionals_name: HashMap<&'ar str, usize>,
// A list of subcommands
subcommands: BTreeMap<String, App<'a, 'v, 'ab, 'u, 'h, 'ar>>,
help_short: Option<char>,
Expand Down Expand Up @@ -114,7 +115,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
version: None,
flags: BTreeMap::new(),
opts: BTreeMap::new(),
positionals_idx: BTreeMap::new(),
positionals_idx: VecMap::new(),
positionals_name: HashMap::new(),
subcommands: BTreeMap::new(),
help_short: None,
Expand Down Expand Up @@ -767,17 +768,17 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
if a.index.is_some() || (a.short.is_none() && a.long.is_none()) {
let i = if a.index.is_none() {
(self.positionals_idx.len() + 1) as u8
(self.positionals_idx.len() + 1)
} else {
a.index.unwrap()
a.index.unwrap() as usize
};
if self.positionals_idx.contains_key(&i) {
panic!("Argument \"{}\" has the same index as another positional \
argument\n\n\tPerhaps try .multiple(true) to allow one positional argument \
to take multiple values",
a.name);
}
let pb = PosBuilder::from_arg(&a, i, &mut self.required);
let pb = PosBuilder::from_arg(&a, i as u8, &mut self.required);
self.positionals_name.insert(pb.name, i);
self.positionals_idx.insert(i, pb);
} else if a.takes_value {
Expand Down Expand Up @@ -1901,7 +1902,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{

if !pos_only &&
(self.settings.is_set(&AppSettings::TrailingVarArg) &&
pos_counter == self.positionals_idx.len() as u8) {
pos_counter == self.positionals_idx.len()) {
pos_only = true;
}
} else {
Expand Down Expand Up @@ -2388,7 +2389,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
//
// Next we verify that only the highest index has a .multiple(true) (if any)
if let Some((idx, ref p)) = self.positionals_idx.iter().rev().next() {
if *idx as usize != self.positionals_idx.len() {
if idx != self.positionals_idx.len() {
panic!("Found positional argument \"{}\" who's index is {} but there are only {} \
positional arguments defined",
p.name,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -641,6 +641,7 @@ extern crate ansi_term;
extern crate yaml_rust;
#[macro_use]
extern crate bitflags;
extern crate vec_map;

#[cfg(feature = "yaml")]
pub use yaml_rust::YamlLoader;
Expand Down

0 comments on commit 64b921d

Please sign in to comment.