Skip to content

Commit

Permalink
fix: Switch to owned types
Browse files Browse the repository at this point in the history
Impact:
- Binary size: 556.6 KiB to 578.4 KiB
- build time: 6.4950 us (7% slower)
- parse time: 7.7256 us
- parse sc time: 8.1580 us (5% faster)

Fixes clap-rs#1041
Fixes clap-rs#2150
  • Loading branch information
epage committed Aug 22, 2022
1 parent fefeb7e commit fe78015
Show file tree
Hide file tree
Showing 61 changed files with 799 additions and 620 deletions.
18 changes: 9 additions & 9 deletions clap_bench/benches/04_new_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn build_help(cmd: &mut Command) -> String {
String::from_utf8(content).unwrap()
}

fn app_example1<'c>() -> Command<'c> {
fn app_example1() -> Command {
Command::new("MyApp")
.version("1.0")
.author("Kevin K. <[email protected]>")
Expand All @@ -30,14 +30,14 @@ fn app_example1<'c>() -> Command<'c> {
)
}

fn app_example2<'c>() -> Command<'c> {
fn app_example2() -> Command {
Command::new("MyApp")
.version("1.0")
.author("Kevin K. <[email protected]>")
.about("Does awesome things")
}

fn app_example3<'c>() -> Command<'c> {
fn app_example3() -> Command {
Command::new("MyApp")
.arg(
Arg::new("debug")
Expand Down Expand Up @@ -65,7 +65,7 @@ fn app_example3<'c>() -> Command<'c> {
)
}

fn app_example4<'c>() -> Command<'c> {
fn app_example4() -> Command {
Command::new("MyApp")
.about("Parses an input file to do awesome things")
.version("1.0")
Expand All @@ -91,7 +91,7 @@ fn app_example4<'c>() -> Command<'c> {
)
}

fn app_example5<'c>() -> Command<'c> {
fn app_example5() -> Command {
Command::new("MyApp").arg(
Arg::new("awesome")
.help("turns up the awesome")
Expand All @@ -101,7 +101,7 @@ fn app_example5<'c>() -> Command<'c> {
)
}

fn app_example6<'c>() -> Command<'c> {
fn app_example6() -> Command {
Command::new("MyApp")
.arg(
Arg::new("input")
Expand All @@ -113,7 +113,7 @@ fn app_example6<'c>() -> Command<'c> {
.arg(Arg::new("config").help("the config file to use").index(2))
}

fn app_example7<'c>() -> Command<'c> {
fn app_example7() -> Command {
Command::new("MyApp")
.arg(Arg::new("config"))
.arg(Arg::new("output"))
Expand All @@ -130,7 +130,7 @@ fn app_example7<'c>() -> Command<'c> {
)
}

fn app_example8<'c>() -> Command<'c> {
fn app_example8() -> Command {
Command::new("MyApp")
.arg(Arg::new("config"))
.arg(Arg::new("output"))
Expand All @@ -147,7 +147,7 @@ fn app_example8<'c>() -> Command<'c> {
)
}

fn app_example10<'c>() -> Command<'c> {
fn app_example10() -> Command {
Command::new("myapp").about("does awesome things").arg(
Arg::new("CONFIG")
.help("The config file to use (default is \"config.json\")")
Expand Down
6 changes: 3 additions & 3 deletions clap_bench/benches/05_ripgrep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,12 @@ OPTIONS:
{options}";

/// Build a clap application with short help strings.
fn app_short() -> Command<'static> {
fn app_short() -> Command {
cmd(false, |k| USAGES[k].short)
}

/// Build a clap application with long help strings.
fn app_long() -> Command<'static> {
fn app_long() -> Command {
cmd(true, |k| USAGES[k].long)
}

Expand All @@ -294,7 +294,7 @@ fn build_help(cmd: &mut Command) -> String {
///
/// This is an intentionally stand-alone module so that it can be used easily
/// in a `build.rs` script to build shell completion files.
fn cmd<F>(_next_line_help: bool, doc: F) -> Command<'static>
fn cmd<F>(_next_line_help: bool, doc: F) -> Command
where
F: Fn(&'static str) -> &'static str,
{
Expand Down
2 changes: 1 addition & 1 deletion clap_bench/benches/06_rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn parse_rustup_with_sc(c: &mut Criterion) {
});
}

fn build_cli() -> Command<'static> {
fn build_cli() -> Command {
Command::new("rustup")
.version("0.9.0") // Simulating
.about("The Rust toolchain installer")
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/examples/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use clap::{value_parser, Arg, Command, ValueHint};
use clap_complete::{generate, Generator, Shell};
use std::io;

fn build_cli() -> Command<'static> {
fn build_cli() -> Command {
Command::new("value_hints")
// AppSettings::TrailingVarArg is required to use ValueHint::CommandWithArguments
.trailing_var_arg(true)
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/examples/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::FromArgMatches;
use clap::Subcommand;

fn command() -> clap::Command<'static> {
fn command() -> clap::Command {
let cmd = clap::Command::new("dynamic")
.arg(
clap::Arg::new("input")
Expand Down
6 changes: 4 additions & 2 deletions clap_complete/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ complete OPTIONS -F _clap_complete_NAME EXECUTABLES
if let Some((flag, value)) = arg.to_long() {
if let Ok(flag) = flag {
if let Some(value) = value {
if let Some(arg) = cmd.get_arguments().find(|a| a.get_long() == Some(flag))
if let Some(arg) = cmd
.get_arguments()
.find(|a| a.get_long().map(|s| s.as_str()) == Some(flag))
{
completions.extend(
complete_arg_value(value.to_str().ok_or(value), arg, current_dir)
Expand Down Expand Up @@ -431,7 +433,7 @@ complete OPTIONS -F _clap_complete_NAME EXECUTABLES

fn complete_arg_value(
value: Result<&str, &clap_lex::RawOsStr>,
arg: &clap::Arg<'_>,
arg: &clap::Arg,
current_dir: Option<&std::path::Path>,
) -> Vec<OsString> {
let mut values = Vec::new();
Expand Down
8 changes: 4 additions & 4 deletions clap_complete/src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub trait Generator {
/// ```
/// // src/cli.rs
/// # use clap::{Command, Arg, ArgAction};
/// pub fn build_cli() -> Command<'static> {
/// pub fn build_cli() -> Command {
/// Command::new("compl")
/// .about("Tests completions")
/// .arg(Arg::new("file")
Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn generate_to<G, S, T>(
) -> Result<PathBuf, Error>
where
G: Generator,
S: Into<String>,
S: Into<clap::Str>,
T: Into<OsString>,
{
cmd.set_bin_name(bin_name);
Expand Down Expand Up @@ -223,7 +223,7 @@ where
pub fn generate<G, S>(gen: G, cmd: &mut clap::Command, bin_name: S, buf: &mut dyn Write)
where
G: Generator,
S: Into<String>,
S: Into<clap::Str>,
{
cmd.set_bin_name(bin_name);
_generate::<G, S>(gen, cmd, buf)
Expand All @@ -232,7 +232,7 @@ where
fn _generate<G, S>(gen: G, cmd: &mut clap::Command, buf: &mut dyn Write)
where
G: Generator,
S: Into<String>,
S: Into<clap::Str>,
{
cmd.build();

Expand Down
32 changes: 16 additions & 16 deletions clap_complete/src/generator/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ pub fn all_subcommands(cmd: &Command) -> Vec<(String, String)> {
/// Finds the subcommand [`clap::Command`] from the given [`clap::Command`] with the given path.
///
/// **NOTE:** `path` should not contain the root `bin_name`.
pub fn find_subcommand_with_path<'help, 'cmd>(
p: &'cmd Command<'help>,
path: Vec<&str>,
) -> &'cmd Command<'help> {
pub fn find_subcommand_with_path<'cmd>(p: &'cmd Command, path: Vec<&str>) -> &'cmd Command {
let mut cmd = p;

for sc in path {
Expand Down Expand Up @@ -118,7 +115,7 @@ pub fn longs_and_visible_aliases(p: &Command) -> Vec<String> {

/// Gets all the flags of a [`clap::Command`](Command).
/// Includes `help` and `version` depending on the [`clap::Command`] settings.
pub fn flags<'help>(p: &Command<'help>) -> Vec<Arg<'help>> {
pub fn flags(p: &Command) -> Vec<Arg> {
debug!("flags: name={}", p.get_name());
p.get_arguments()
.filter(|a| !a.get_num_args().expect("built").takes_values() && !a.is_positional())
Expand All @@ -127,7 +124,7 @@ pub fn flags<'help>(p: &Command<'help>) -> Vec<Arg<'help>> {
}

/// Get the possible values for completion
pub fn possible_values(a: &Arg<'_>) -> Option<Vec<clap::builder::PossibleValue>> {
pub fn possible_values(a: &Arg) -> Option<Vec<clap::builder::PossibleValue>> {
if !a.get_num_args().expect("built").takes_values() {
None
} else {
Expand All @@ -144,7 +141,7 @@ mod tests {
use clap::ArgAction;
use pretty_assertions::assert_eq;

fn common_app() -> Command<'static> {
fn common_app() -> Command {
Command::new("myapp")
.subcommand(
Command::new("test").subcommand(Command::new("config")).arg(
Expand All @@ -161,14 +158,14 @@ mod tests {
.bin_name("my-cmd")
}

fn built() -> Command<'static> {
fn built() -> Command {
let mut cmd = common_app();

cmd.build();
cmd
}

fn built_with_version() -> Command<'static> {
fn built_with_version() -> Command {
let mut cmd = common_app().version("3.0");

cmd.build();
Expand Down Expand Up @@ -219,14 +216,17 @@ mod tests {
let actual_flags = flags(&cmd);

assert_eq!(actual_flags.len(), 2);
assert_eq!(actual_flags[0].get_long(), Some("help"));
assert_eq!(actual_flags[1].get_long(), Some("version"));
assert_eq!(actual_flags[0].get_long().map(|s| s.as_str()), Some("help"));
assert_eq!(
actual_flags[1].get_long().map(|s| s.as_str()),
Some("version")
);

let sc_flags = flags(find_subcommand_with_path(&cmd, vec!["test"]));

assert_eq!(sc_flags.len(), 2);
assert_eq!(sc_flags[0].get_long(), Some("file"));
assert_eq!(sc_flags[1].get_long(), Some("help"));
assert_eq!(sc_flags[0].get_long().map(|s| s.as_str()), Some("file"));
assert_eq!(sc_flags[1].get_long().map(|s| s.as_str()), Some("help"));
}

#[test]
Expand All @@ -235,13 +235,13 @@ mod tests {
let actual_flags = flags(&cmd);

assert_eq!(actual_flags.len(), 1);
assert_eq!(actual_flags[0].get_long(), Some("help"));
assert_eq!(actual_flags[0].get_long().map(|s| s.as_str()), Some("help"));

let sc_flags = flags(find_subcommand_with_path(&cmd, vec!["test"]));

assert_eq!(sc_flags.len(), 2);
assert_eq!(sc_flags[0].get_long(), Some("file"));
assert_eq!(sc_flags[1].get_long(), Some("help"));
assert_eq!(sc_flags[0].get_long().map(|s| s.as_str()), Some("file"));
assert_eq!(sc_flags[1].get_long().map(|s| s.as_str()), Some("help"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//! use clap_complete::{generate, Generator, Shell};
//! use std::io;
//!
//! fn build_cli() -> Command<'static> {
//! fn build_cli() -> Command {
//! Command::new("example")
//! .arg(Arg::new("file")
//! .help("some input file")
Expand Down
12 changes: 6 additions & 6 deletions clap_complete/src/shells/elvish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn get_tooltip<T: ToString>(help: Option<&str>, data: T) -> String {
}

fn generate_inner<'help>(
p: &Command<'help>,
p: &Command,
previous_command_name: &str,
names: &mut Vec<&'help str>,
) -> String {
Expand All @@ -84,15 +84,15 @@ fn generate_inner<'help>(

for option in p.get_opts() {
if let Some(shorts) = option.get_short_and_visible_aliases() {
let tooltip = get_tooltip(option.get_help(), shorts[0]);
let tooltip = get_tooltip(option.get_help().map(|s| s.as_str()), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(format!("-{} '{}'", short, tooltip).as_str());
}
}

if let Some(longs) = option.get_long_and_visible_aliases() {
let tooltip = get_tooltip(option.get_help(), longs[0]);
let tooltip = get_tooltip(option.get_help().map(|s| s.as_str()), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(format!("--{} '{}'", long, tooltip).as_str());
Expand All @@ -102,15 +102,15 @@ fn generate_inner<'help>(

for flag in utils::flags(p) {
if let Some(shorts) = flag.get_short_and_visible_aliases() {
let tooltip = get_tooltip(flag.get_help(), shorts[0]);
let tooltip = get_tooltip(flag.get_help().map(|s| s.as_str()), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(format!("-{} '{}'", short, tooltip).as_str());
}
}

if let Some(longs) = flag.get_long_and_visible_aliases() {
let tooltip = get_tooltip(flag.get_help(), longs[0]);
let tooltip = get_tooltip(flag.get_help().map(|s| s.as_str()), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(format!("--{} '{}'", long, tooltip).as_str());
Expand All @@ -120,7 +120,7 @@ fn generate_inner<'help>(

for subcommand in p.get_subcommands() {
let data = &subcommand.get_name();
let tooltip = get_tooltip(subcommand.get_about(), data);
let tooltip = get_tooltip(subcommand.get_about().map(|s| s.as_str()), data);

completions.push_str(&preamble);
completions.push_str(format!("{} '{}'", data, tooltip).as_str());
Expand Down
12 changes: 6 additions & 6 deletions clap_complete/src/shells/powershell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn get_tooltip<T: ToString>(help: Option<&str>, data: T) -> String {
}

fn generate_inner<'help>(
p: &Command<'help>,
p: &Command,
previous_command_name: &str,
names: &mut Vec<&'help str>,
) -> String {
Expand All @@ -89,7 +89,7 @@ fn generate_inner<'help>(

for option in p.get_opts() {
if let Some(shorts) = option.get_short_and_visible_aliases() {
let tooltip = get_tooltip(option.get_help(), shorts[0]);
let tooltip = get_tooltip(option.get_help().map(|s| s.as_str()), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(
Expand All @@ -103,7 +103,7 @@ fn generate_inner<'help>(
}

if let Some(longs) = option.get_long_and_visible_aliases() {
let tooltip = get_tooltip(option.get_help(), longs[0]);
let tooltip = get_tooltip(option.get_help().map(|s| s.as_str()), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(
Expand All @@ -119,7 +119,7 @@ fn generate_inner<'help>(

for flag in utils::flags(p) {
if let Some(shorts) = flag.get_short_and_visible_aliases() {
let tooltip = get_tooltip(flag.get_help(), shorts[0]);
let tooltip = get_tooltip(flag.get_help().map(|s| s.as_str()), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(
Expand All @@ -133,7 +133,7 @@ fn generate_inner<'help>(
}

if let Some(longs) = flag.get_long_and_visible_aliases() {
let tooltip = get_tooltip(flag.get_help(), longs[0]);
let tooltip = get_tooltip(flag.get_help().map(|s| s.as_str()), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(
Expand All @@ -149,7 +149,7 @@ fn generate_inner<'help>(

for subcommand in p.get_subcommands() {
let data = &subcommand.get_name();
let tooltip = get_tooltip(subcommand.get_about(), data);
let tooltip = get_tooltip(subcommand.get_about().map(|s| s.as_str()), data);

completions.push_str(&preamble);
completions.push_str(
Expand Down
Loading

0 comments on commit fe78015

Please sign in to comment.