Skip to content

Commit fe78015

Browse files
committed
fix: Switch to owned types
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
1 parent fefeb7e commit fe78015

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+799
-620
lines changed

Diff for: clap_bench/benches/04_new_help.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn build_help(cmd: &mut Command) -> String {
1010
String::from_utf8(content).unwrap()
1111
}
1212

13-
fn app_example1<'c>() -> Command<'c> {
13+
fn app_example1() -> Command {
1414
Command::new("MyApp")
1515
.version("1.0")
1616
.author("Kevin K. <[email protected]>")
@@ -30,14 +30,14 @@ fn app_example1<'c>() -> Command<'c> {
3030
)
3131
}
3232

33-
fn app_example2<'c>() -> Command<'c> {
33+
fn app_example2() -> Command {
3434
Command::new("MyApp")
3535
.version("1.0")
3636
.author("Kevin K. <[email protected]>")
3737
.about("Does awesome things")
3838
}
3939

40-
fn app_example3<'c>() -> Command<'c> {
40+
fn app_example3() -> Command {
4141
Command::new("MyApp")
4242
.arg(
4343
Arg::new("debug")
@@ -65,7 +65,7 @@ fn app_example3<'c>() -> Command<'c> {
6565
)
6666
}
6767

68-
fn app_example4<'c>() -> Command<'c> {
68+
fn app_example4() -> Command {
6969
Command::new("MyApp")
7070
.about("Parses an input file to do awesome things")
7171
.version("1.0")
@@ -91,7 +91,7 @@ fn app_example4<'c>() -> Command<'c> {
9191
)
9292
}
9393

94-
fn app_example5<'c>() -> Command<'c> {
94+
fn app_example5() -> Command {
9595
Command::new("MyApp").arg(
9696
Arg::new("awesome")
9797
.help("turns up the awesome")
@@ -101,7 +101,7 @@ fn app_example5<'c>() -> Command<'c> {
101101
)
102102
}
103103

104-
fn app_example6<'c>() -> Command<'c> {
104+
fn app_example6() -> Command {
105105
Command::new("MyApp")
106106
.arg(
107107
Arg::new("input")
@@ -113,7 +113,7 @@ fn app_example6<'c>() -> Command<'c> {
113113
.arg(Arg::new("config").help("the config file to use").index(2))
114114
}
115115

116-
fn app_example7<'c>() -> Command<'c> {
116+
fn app_example7() -> Command {
117117
Command::new("MyApp")
118118
.arg(Arg::new("config"))
119119
.arg(Arg::new("output"))
@@ -130,7 +130,7 @@ fn app_example7<'c>() -> Command<'c> {
130130
)
131131
}
132132

133-
fn app_example8<'c>() -> Command<'c> {
133+
fn app_example8() -> Command {
134134
Command::new("MyApp")
135135
.arg(Arg::new("config"))
136136
.arg(Arg::new("output"))
@@ -147,7 +147,7 @@ fn app_example8<'c>() -> Command<'c> {
147147
)
148148
}
149149

150-
fn app_example10<'c>() -> Command<'c> {
150+
fn app_example10() -> Command {
151151
Command::new("myapp").about("does awesome things").arg(
152152
Arg::new("CONFIG")
153153
.help("The config file to use (default is \"config.json\")")

Diff for: clap_bench/benches/05_ripgrep.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,12 @@ OPTIONS:
270270
{options}";
271271

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

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

@@ -294,7 +294,7 @@ fn build_help(cmd: &mut Command) -> String {
294294
///
295295
/// This is an intentionally stand-alone module so that it can be used easily
296296
/// in a `build.rs` script to build shell completion files.
297-
fn cmd<F>(_next_line_help: bool, doc: F) -> Command<'static>
297+
fn cmd<F>(_next_line_help: bool, doc: F) -> Command
298298
where
299299
F: Fn(&'static str) -> &'static str,
300300
{

Diff for: clap_bench/benches/06_rustup.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn parse_rustup_with_sc(c: &mut Criterion) {
2121
});
2222
}
2323

24-
fn build_cli() -> Command<'static> {
24+
fn build_cli() -> Command {
2525
Command::new("rustup")
2626
.version("0.9.0") // Simulating
2727
.about("The Rust toolchain installer")

Diff for: clap_complete/examples/completion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use clap::{value_parser, Arg, Command, ValueHint};
1616
use clap_complete::{generate, Generator, Shell};
1717
use std::io;
1818

19-
fn build_cli() -> Command<'static> {
19+
fn build_cli() -> Command {
2020
Command::new("value_hints")
2121
// AppSettings::TrailingVarArg is required to use ValueHint::CommandWithArguments
2222
.trailing_var_arg(true)

Diff for: clap_complete/examples/dynamic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clap::FromArgMatches;
22
use clap::Subcommand;
33

4-
fn command() -> clap::Command<'static> {
4+
fn command() -> clap::Command {
55
let cmd = clap::Command::new("dynamic")
66
.arg(
77
clap::Arg::new("input")

Diff for: clap_complete/src/dynamic.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ complete OPTIONS -F _clap_complete_NAME EXECUTABLES
374374
if let Some((flag, value)) = arg.to_long() {
375375
if let Ok(flag) = flag {
376376
if let Some(value) = value {
377-
if let Some(arg) = cmd.get_arguments().find(|a| a.get_long() == Some(flag))
377+
if let Some(arg) = cmd
378+
.get_arguments()
379+
.find(|a| a.get_long().map(|s| s.as_str()) == Some(flag))
378380
{
379381
completions.extend(
380382
complete_arg_value(value.to_str().ok_or(value), arg, current_dir)
@@ -431,7 +433,7 @@ complete OPTIONS -F _clap_complete_NAME EXECUTABLES
431433

432434
fn complete_arg_value(
433435
value: Result<&str, &clap_lex::RawOsStr>,
434-
arg: &clap::Arg<'_>,
436+
arg: &clap::Arg,
435437
current_dir: Option<&std::path::Path>,
436438
) -> Vec<OsString> {
437439
let mut values = Vec::new();

Diff for: clap_complete/src/generator/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub trait Generator {
8484
/// ```
8585
/// // src/cli.rs
8686
/// # use clap::{Command, Arg, ArgAction};
87-
/// pub fn build_cli() -> Command<'static> {
87+
/// pub fn build_cli() -> Command {
8888
/// Command::new("compl")
8989
/// .about("Tests completions")
9090
/// .arg(Arg::new("file")
@@ -170,7 +170,7 @@ pub fn generate_to<G, S, T>(
170170
) -> Result<PathBuf, Error>
171171
where
172172
G: Generator,
173-
S: Into<String>,
173+
S: Into<clap::Str>,
174174
T: Into<OsString>,
175175
{
176176
cmd.set_bin_name(bin_name);
@@ -223,7 +223,7 @@ where
223223
pub fn generate<G, S>(gen: G, cmd: &mut clap::Command, bin_name: S, buf: &mut dyn Write)
224224
where
225225
G: Generator,
226-
S: Into<String>,
226+
S: Into<clap::Str>,
227227
{
228228
cmd.set_bin_name(bin_name);
229229
_generate::<G, S>(gen, cmd, buf)
@@ -232,7 +232,7 @@ where
232232
fn _generate<G, S>(gen: G, cmd: &mut clap::Command, buf: &mut dyn Write)
233233
where
234234
G: Generator,
235-
S: Into<String>,
235+
S: Into<clap::Str>,
236236
{
237237
cmd.build();
238238

Diff for: clap_complete/src/generator/utils.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ pub fn all_subcommands(cmd: &Command) -> Vec<(String, String)> {
1919
/// Finds the subcommand [`clap::Command`] from the given [`clap::Command`] with the given path.
2020
///
2121
/// **NOTE:** `path` should not contain the root `bin_name`.
22-
pub fn find_subcommand_with_path<'help, 'cmd>(
23-
p: &'cmd Command<'help>,
24-
path: Vec<&str>,
25-
) -> &'cmd Command<'help> {
22+
pub fn find_subcommand_with_path<'cmd>(p: &'cmd Command, path: Vec<&str>) -> &'cmd Command {
2623
let mut cmd = p;
2724

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

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

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

147-
fn common_app() -> Command<'static> {
144+
fn common_app() -> Command {
148145
Command::new("myapp")
149146
.subcommand(
150147
Command::new("test").subcommand(Command::new("config")).arg(
@@ -161,14 +158,14 @@ mod tests {
161158
.bin_name("my-cmd")
162159
}
163160

164-
fn built() -> Command<'static> {
161+
fn built() -> Command {
165162
let mut cmd = common_app();
166163

167164
cmd.build();
168165
cmd
169166
}
170167

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

174171
cmd.build();
@@ -219,14 +216,17 @@ mod tests {
219216
let actual_flags = flags(&cmd);
220217

221218
assert_eq!(actual_flags.len(), 2);
222-
assert_eq!(actual_flags[0].get_long(), Some("help"));
223-
assert_eq!(actual_flags[1].get_long(), Some("version"));
219+
assert_eq!(actual_flags[0].get_long().map(|s| s.as_str()), Some("help"));
220+
assert_eq!(
221+
actual_flags[1].get_long().map(|s| s.as_str()),
222+
Some("version")
223+
);
224224

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

227227
assert_eq!(sc_flags.len(), 2);
228-
assert_eq!(sc_flags[0].get_long(), Some("file"));
229-
assert_eq!(sc_flags[1].get_long(), Some("help"));
228+
assert_eq!(sc_flags[0].get_long().map(|s| s.as_str()), Some("file"));
229+
assert_eq!(sc_flags[1].get_long().map(|s| s.as_str()), Some("help"));
230230
}
231231

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

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

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

242242
assert_eq!(sc_flags.len(), 2);
243-
assert_eq!(sc_flags[0].get_long(), Some("file"));
244-
assert_eq!(sc_flags[1].get_long(), Some("help"));
243+
assert_eq!(sc_flags[0].get_long().map(|s| s.as_str()), Some("file"));
244+
assert_eq!(sc_flags[1].get_long().map(|s| s.as_str()), Some("help"));
245245
}
246246

247247
#[test]

Diff for: clap_complete/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! use clap_complete::{generate, Generator, Shell};
2727
//! use std::io;
2828
//!
29-
//! fn build_cli() -> Command<'static> {
29+
//! fn build_cli() -> Command {
3030
//! Command::new("example")
3131
//! .arg(Arg::new("file")
3232
//! .help("some input file")

Diff for: clap_complete/src/shells/elvish.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn get_tooltip<T: ToString>(help: Option<&str>, data: T) -> String {
6767
}
6868

6969
fn generate_inner<'help>(
70-
p: &Command<'help>,
70+
p: &Command,
7171
previous_command_name: &str,
7272
names: &mut Vec<&'help str>,
7373
) -> String {
@@ -84,15 +84,15 @@ fn generate_inner<'help>(
8484

8585
for option in p.get_opts() {
8686
if let Some(shorts) = option.get_short_and_visible_aliases() {
87-
let tooltip = get_tooltip(option.get_help(), shorts[0]);
87+
let tooltip = get_tooltip(option.get_help().map(|s| s.as_str()), shorts[0]);
8888
for short in shorts {
8989
completions.push_str(&preamble);
9090
completions.push_str(format!("-{} '{}'", short, tooltip).as_str());
9191
}
9292
}
9393

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

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

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

121121
for subcommand in p.get_subcommands() {
122122
let data = &subcommand.get_name();
123-
let tooltip = get_tooltip(subcommand.get_about(), data);
123+
let tooltip = get_tooltip(subcommand.get_about().map(|s| s.as_str()), data);
124124

125125
completions.push_str(&preamble);
126126
completions.push_str(format!("{} '{}'", data, tooltip).as_str());

Diff for: clap_complete/src/shells/powershell.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn get_tooltip<T: ToString>(help: Option<&str>, data: T) -> String {
7272
}
7373

7474
fn generate_inner<'help>(
75-
p: &Command<'help>,
75+
p: &Command,
7676
previous_command_name: &str,
7777
names: &mut Vec<&'help str>,
7878
) -> String {
@@ -89,7 +89,7 @@ fn generate_inner<'help>(
8989

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

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

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

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

150150
for subcommand in p.get_subcommands() {
151151
let data = &subcommand.get_name();
152-
let tooltip = get_tooltip(subcommand.get_about(), data);
152+
let tooltip = get_tooltip(subcommand.get_about().map(|s| s.as_str()), data);
153153

154154
completions.push_str(&preamble);
155155
completions.push_str(

0 commit comments

Comments
 (0)