Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve iterator use #2038

Merged
merged 5 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions clap_generate/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,12 @@ pub trait Generator {
.flatten()
.collect();

if shorts_and_visible_aliases
.iter()
.find(|x| **x == 'h')
.is_none()
{
if !shorts_and_visible_aliases.iter().any(|&x| x == 'h') {
shorts_and_visible_aliases.push('h');
}

if !p.is_set(AppSettings::DisableVersion)
&& shorts_and_visible_aliases
.iter()
.find(|x| **x == 'V')
.is_none()
&& !shorts_and_visible_aliases.iter().any(|&x| x == 'V')
{
shorts_and_visible_aliases.push('V');
}
Expand All @@ -176,13 +169,11 @@ pub trait Generator {
})
.collect();

if longs.iter().find(|x| **x == "help").is_none() {
if !longs.iter().any(|x| *x == "help") {
longs.push(String::from("help"));
}

if !p.is_set(AppSettings::DisableVersion)
&& longs.iter().find(|x| **x == "version").is_none()
{
if !p.is_set(AppSettings::DisableVersion) && !longs.iter().any(|x| *x == "version") {
longs.push(String::from("version"));
}

Expand All @@ -196,7 +187,7 @@ pub trait Generator {

let mut flags: Vec<_> = p.get_flags_no_heading().cloned().collect();

if flags.iter().find(|x| x.get_name() == "help").is_none() {
if !flags.iter().any(|x| x.get_name() == "help") {
flags.push(
Arg::new("help")
.short('h')
Expand All @@ -206,7 +197,7 @@ pub trait Generator {
}

if !p.is_set(AppSettings::DisableVersion)
&& flags.iter().find(|x| x.get_name() == "version").is_none()
&& !flags.iter().any(|x| x.get_name() == "version")
{
flags.push(
Arg::new("version")
Expand Down
3 changes: 1 addition & 2 deletions src/build/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2549,8 +2549,7 @@ impl<'help> App<'help> {
pub(crate) fn has_visible_subcommands(&self) -> bool {
self.subcommands
.iter()
.filter(|sc| sc.name != "help")
.any(|sc| !sc.is_set(AppSettings::Hidden))
.any(|sc| sc.name != "help" && !sc.is_set(AppSettings::Hidden))
}

/// Check if this subcommand can be referred to as `name`. In other words,
Expand Down
33 changes: 11 additions & 22 deletions src/output/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,28 +391,17 @@ impl<'help, 'app, 'parser> Usage<'help, 'app, 'parser> {
.flat_map(|g| self.p.app.unroll_args_in_group(&g.id))
.collect::<Vec<_>>();

let pmap = if let Some(m) = matcher {
unrolled_reqs
.iter()
.chain(incls.iter())
.filter(|a| self.p.app.get_positionals().any(|p| &&p.id == a))
.filter(|&pos| !m.contains(pos))
.filter_map(|pos| self.p.app.find(pos))
.filter(|&pos| incl_last || !pos.is_set(ArgSettings::Last))
.filter(|pos| !args_in_groups.contains(&pos.id))
.map(|pos| (pos.index.unwrap(), pos))
.collect::<BTreeMap<u64, &Arg>>() // sort by index
} else {
unrolled_reqs
.iter()
.chain(incls.iter())
.filter(|a| self.p.app.get_positionals().any(|p| &&p.id == a))
.filter_map(|pos| self.p.app.find(pos))
.filter(|&pos| incl_last || !pos.is_set(ArgSettings::Last))
.filter(|pos| !args_in_groups.contains(&pos.id))
.map(|pos| (pos.index.unwrap(), pos))
.collect::<BTreeMap<u64, &Arg>>() // sort by index
};
let pmap = unrolled_reqs
.iter()
.chain(incls.iter())
.filter(|a| self.p.app.get_positionals().any(|p| &&p.id == a))
.filter(|&pos| matcher.map_or(true, |m| !m.contains(pos)))
.filter_map(|pos| self.p.app.find(pos))
.filter(|&pos| incl_last || !pos.is_set(ArgSettings::Last))
.filter(|pos| !args_in_groups.contains(&pos.id))
.map(|pos| (pos.index.unwrap(), pos))
.collect::<BTreeMap<u64, &Arg>>(); // sort by index

for p in pmap.values() {
debug!("Usage::get_required_usage_from:iter:{:?}", p.id);
if args_in_groups.is_empty() || !args_in_groups.contains(&p.id) {
Expand Down
3 changes: 1 addition & 2 deletions src/parse/matches/matched_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ impl MatchedArg {
pub(crate) fn contains_val(&self, val: &str) -> bool {
self.vals
.iter()
.map(OsString::as_os_str)
.any(|v| v == OsStr::new(val))
.any(|v| OsString::as_os_str(v) == OsStr::new(val))
}
}
3 changes: 1 addition & 2 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ impl<'help, 'app> Parser<'help, 'app> {
.args
.args
.iter()
.filter(|a| a.is_positional())
.find(|p| p.index == Some(pos_counter as u64))
.find(|a| a.is_positional() && a.index == Some(pos_counter as u64))
{
if p.is_set(ArgSettings::Last) && !self.is_set(AS::TrailingValues) {
return Err(ClapError::unknown_argument(
Expand Down
9 changes: 3 additions & 6 deletions src/parse/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
.app
.groups
.iter()
.filter(|g| !g.multiple)
.find(|g| &g.id == name)
.find(|g| !g.multiple && &g.id == name)
{
let conf_with_self = self
.p
Expand Down Expand Up @@ -311,8 +310,7 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
.app
.groups
.iter()
.filter(|g| !g.multiple)
.find(|g| g.id == grp)
.find(|g| !g.multiple && g.id == grp)
{
// for g_arg in self.p.app.unroll_args_in_group(&g.name) {
// if &g_arg != name {
Expand All @@ -326,8 +324,7 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
.app
.groups
.iter()
.filter(|g| !g.multiple)
.find(|grp| grp.id == *name)
.find(|g| !g.multiple && g.id == *name)
{
debug!("Validator::gather_conflicts:iter:{:?}:group", name);
self.c.insert(g.id.clone());
Expand Down
10 changes: 1 addition & 9 deletions src/util/argstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,7 @@ impl<'a> ArgStr<'a> {
pub(crate) fn trim_start_n_matches(&self, n: usize, ch: u8) -> ArgStr {
assert!(ch.is_ascii());

let i = self
.0
.iter()
.take(n)
.take_while(|c| **c == ch)
.enumerate()
.last()
.map(|(i, _)| i + 1)
.unwrap_or(0);
let i = self.0.iter().take(n).take_while(|c| **c == ch).count();

self.split_at_unchecked(i).1
}
Expand Down
7 changes: 1 addition & 6 deletions src/util/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ where
self.0.push(Child::new(req));
idx
} else {
self.0
.iter()
.enumerate()
.find(|(_, e)| e.id == req)
.map(|(i, _)| i)
.unwrap()
self.0.iter().position(|e| e.id == req).unwrap()
}
}

Expand Down