Skip to content

Commit c5d8eb4

Browse files
committed
fix!: Remove lifetime from PossibleValue
Another step towards clap-rs#1041 This isn't the long term type for `PossibleValue::help`, I just wanted to get the lifetime out of the way first before figuring out how help will work.
1 parent abd4205 commit c5d8eb4

File tree

21 files changed

+103
-91
lines changed

21 files changed

+103
-91
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn flags<'help>(p: &Command<'help>) -> Vec<Arg<'help>> {
127127
}
128128

129129
/// Get the possible values for completion
130-
pub fn possible_values<'help>(a: &Arg<'help>) -> Option<Vec<clap::builder::PossibleValue<'help>>> {
130+
pub fn possible_values(a: &Arg<'_>) -> Option<Vec<clap::builder::PossibleValue>> {
131131
if !a.get_num_args().expect("built").takes_values() {
132132
None
133133
} else {

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{fmt::Write as _, io::Write};
22

3-
use clap::builder::PossibleValue;
43
use clap::*;
54

65
use crate::generator::{utils, Generator};
@@ -180,7 +179,7 @@ fn vals_for(o: &Arg) -> String {
180179
"$(compgen -W \"{}\" -- \"${{cur}}\")",
181180
vals.iter()
182181
.filter(|pv| !pv.is_hide_set())
183-
.map(PossibleValue::get_name)
182+
.map(|n| n.get_name().as_str())
184183
.collect::<Vec<_>>()
185184
.join(" ")
186185
)

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ fn value_completion(option: &Arg) -> String {
164164
Some(format!(
165165
"{}\t{}",
166166
escape_string(value.get_name()).as_str(),
167-
escape_string(value.get_help().unwrap_or_default()).as_str()
167+
escape_string(value.get_help().map(|s| s.as_str()).unwrap_or_default())
168+
.as_str()
168169
))
169170
})
170171
.collect::<Vec<_>>()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl ValueEnum for Shell {
5757
]
5858
}
5959

60-
fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>> {
60+
fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
6161
Some(match self {
6262
Shell::Bash => PossibleValue::new("bash"),
6363
Shell::Elvish => PossibleValue::new("elvish"),

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::io::Write;
22

3-
use clap::builder::PossibleValue;
43
use clap::*;
54

65
use crate::generator::{utils, Generator};
@@ -375,8 +374,12 @@ fn value_completion(arg: &Arg) -> Option<String> {
375374
} else {
376375
Some(format!(
377376
r#"{name}\:"{tooltip}""#,
378-
name = escape_value(value.get_name()),
379-
tooltip = value.get_help().map(escape_help).unwrap_or_default()
377+
name = escape_value(value.get_name().as_str()),
378+
tooltip = value
379+
.get_help()
380+
.map(|s| s.as_str())
381+
.map(escape_help)
382+
.unwrap_or_default()
380383
))
381384
}
382385
})
@@ -389,7 +392,7 @@ fn value_completion(arg: &Arg) -> Option<String> {
389392
values
390393
.iter()
391394
.filter(|pv| !pv.is_hide_set())
392-
.map(PossibleValue::get_name)
395+
.map(|n| n.get_name().as_str())
393396
.collect::<Vec<_>>()
394397
.join(" ")
395398
))

Diff for: clap_derive/src/attrs.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl Attrs {
499499
quote_spanned!(ident.span()=> {
500500
{
501501
let val: #ty = #val;
502-
clap::ValueEnum::to_possible_value(&val).unwrap().get_name()
502+
clap::ValueEnum::to_possible_value(&val).unwrap().get_name().to_owned()
503503
}
504504
})
505505
} else {
@@ -544,17 +544,15 @@ impl Attrs {
544544
let val = if parsed.iter().any(|a| matches!(a, ValueEnum(_))) {
545545
quote_spanned!(ident.span()=> {
546546
{
547-
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> Vec<&'static str>
547+
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> impl Iterator<Item=clap::Str>
548548
where
549549
T: ::std::borrow::Borrow<#inner_type>
550550
{
551551
iterable
552552
.into_iter()
553553
.map(|val| {
554-
clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name()
554+
clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name().to_owned()
555555
})
556-
.collect()
557-
558556
}
559557

560558
iter_to_vals(#expr)
@@ -603,7 +601,7 @@ impl Attrs {
603601
quote_spanned!(ident.span()=> {
604602
{
605603
let val: #ty = #val;
606-
clap::ValueEnum::to_possible_value(&val).unwrap().get_name()
604+
clap::ValueEnum::to_possible_value(&val).unwrap().get_name().to_owned()
607605
}
608606
})
609607
} else {
@@ -648,18 +646,15 @@ impl Attrs {
648646
let val = if parsed.iter().any(|a| matches!(a, ValueEnum(_))) {
649647
quote_spanned!(ident.span()=> {
650648
{
651-
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> Vec<&'static ::std::ffi::OsStr>
649+
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> impl Iterator<Item=clap::Str>
652650
where
653651
T: ::std::borrow::Borrow<#inner_type>
654652
{
655653
iterable
656654
.into_iter()
657655
.map(|val| {
658-
clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name()
656+
clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name().to_owned()
659657
})
660-
.map(::std::ffi::OsStr::new)
661-
.collect()
662-
663658
}
664659

665660
iter_to_vals(#expr)

Diff for: clap_derive/src/derives/value_enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn gen_to_possible_value(lits: &[(TokenStream, Ident)]) -> TokenStream {
114114
let (lit, variant): (Vec<TokenStream>, Vec<Ident>) = lits.iter().cloned().unzip();
115115

116116
quote! {
117-
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue<'a>> {
117+
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue> {
118118
match self {
119119
#(Self::#variant => Some(#lit),)*
120120
_ => None

Diff for: clap_derive/src/dummies.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn value_enum(name: &Ident) {
8282
fn from_str(_input: &str, _ignore_case: bool) -> ::std::result::Result<Self, String> {
8383
unimplemented!()
8484
}
85-
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue<'a>>{
85+
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue>{
8686
unimplemented!()
8787
}
8888
}

Diff for: examples/tutorial_builder/04_01_enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl ValueEnum for Mode {
1212
&[Mode::Fast, Mode::Slow]
1313
}
1414

15-
fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>> {
15+
fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
1616
Some(match self {
1717
Mode::Fast => PossibleValue::new("fast"),
1818
Mode::Slow => PossibleValue::new("slow"),

Diff for: src/builder/arg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3659,7 +3659,7 @@ impl<'help> Arg<'help> {
36593659
Some(longs)
36603660
}
36613661

3662-
pub(crate) fn get_possible_values(&self) -> Vec<PossibleValue<'help>> {
3662+
pub(crate) fn get_possible_values(&self) -> Vec<PossibleValue> {
36633663
if !self.is_takes_value_set() {
36643664
vec![]
36653665
} else {

Diff for: src/builder/possible_value.rs

+28-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{borrow::Cow, iter};
22

33
use crate::util::eq_ignore_case;
4+
use crate::Str;
45

56
/// A possible value of an argument.
67
///
@@ -27,14 +28,14 @@ use crate::util::eq_ignore_case;
2728
/// [hide]: PossibleValue::hide()
2829
/// [help]: PossibleValue::help()
2930
#[derive(Debug, Default, Clone, PartialEq, Eq)]
30-
pub struct PossibleValue<'help> {
31-
name: &'help str,
32-
help: Option<&'help str>,
33-
aliases: Vec<&'help str>, // (name, visible)
31+
pub struct PossibleValue {
32+
name: Str,
33+
help: Option<Str>,
34+
aliases: Vec<Str>, // (name, visible)
3435
hide: bool,
3536
}
3637

37-
impl<'help> PossibleValue<'help> {
38+
impl PossibleValue {
3839
/// Create a [`PossibleValue`] with its name.
3940
///
4041
/// The name will be used to decide whether this value was provided by the user to an argument.
@@ -52,9 +53,9 @@ impl<'help> PossibleValue<'help> {
5253
/// [hidden]: PossibleValue::hide
5354
/// [possible value]: crate::builder::PossibleValuesParser
5455
/// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values()
55-
pub fn new(name: &'help str) -> Self {
56+
pub fn new(name: impl Into<Str>) -> Self {
5657
PossibleValue {
57-
name,
58+
name: name.into(),
5859
..Default::default()
5960
}
6061
}
@@ -74,8 +75,8 @@ impl<'help> PossibleValue<'help> {
7475
/// ```
7576
#[inline]
7677
#[must_use]
77-
pub fn help(mut self, help: &'help str) -> Self {
78-
self.help = Some(help);
78+
pub fn help(mut self, help: impl Into<Str>) -> Self {
79+
self.help = Some(help.into());
7980
self
8081
}
8182

@@ -111,8 +112,8 @@ impl<'help> PossibleValue<'help> {
111112
/// # ;
112113
/// ```
113114
#[must_use]
114-
pub fn alias(mut self, name: &'help str) -> Self {
115-
self.aliases.push(name);
115+
pub fn alias(mut self, name: impl Into<Str>) -> Self {
116+
self.aliases.push(name.into());
116117
self
117118
}
118119

@@ -127,35 +128,32 @@ impl<'help> PossibleValue<'help> {
127128
/// # ;
128129
/// ```
129130
#[must_use]
130-
pub fn aliases<I>(mut self, names: I) -> Self
131-
where
132-
I: IntoIterator<Item = &'help str>,
133-
{
134-
self.aliases.extend(names.into_iter());
131+
pub fn aliases(mut self, names: impl IntoIterator<Item = impl Into<Str>>) -> Self {
132+
self.aliases.extend(names.into_iter().map(|a| a.into()));
135133
self
136134
}
137135
}
138136

139137
/// Reflection
140-
impl<'help> PossibleValue<'help> {
138+
impl PossibleValue {
141139
/// Get the name of the argument value
142140
#[inline]
143-
pub fn get_name(&self) -> &'help str {
144-
self.name
141+
pub fn get_name(&self) -> &Str {
142+
&self.name
145143
}
146144

147145
/// Get the help specified for this argument, if any
148146
#[inline]
149-
pub fn get_help(&self) -> Option<&'help str> {
150-
self.help
147+
pub fn get_help(&self) -> Option<&Str> {
148+
self.help.as_ref()
151149
}
152150

153151
/// Get the help specified for this argument, if any and the argument
154152
/// value is not hidden
155153
#[inline]
156-
pub(crate) fn get_visible_help(&self) -> Option<&'help str> {
154+
pub(crate) fn get_visible_help(&self) -> Option<&str> {
157155
if !self.hide {
158-
self.help
156+
self.help.as_deref()
159157
} else {
160158
None
161159
}
@@ -174,12 +172,12 @@ impl<'help> PossibleValue<'help> {
174172

175173
/// Get the name if argument value is not hidden, `None` otherwise,
176174
/// but wrapped in quotes if it contains whitespace
177-
pub(crate) fn get_visible_quoted_name(&self) -> Option<Cow<'help, str>> {
175+
pub(crate) fn get_visible_quoted_name(&self) -> Option<Cow<'_, str>> {
178176
if !self.hide {
179177
Some(if self.name.contains(char::is_whitespace) {
180178
format!("{:?}", self.name).into()
181179
} else {
182-
self.name.into()
180+
self.name.as_str().into()
183181
})
184182
} else {
185183
None
@@ -189,8 +187,8 @@ impl<'help> PossibleValue<'help> {
189187
/// Returns all valid values of the argument value.
190188
///
191189
/// Namely the name and all aliases.
192-
pub fn get_name_and_aliases(&self) -> impl Iterator<Item = &'help str> + '_ {
193-
iter::once(&self.name).chain(&self.aliases).copied()
190+
pub fn get_name_and_aliases(&self) -> impl Iterator<Item = &Str> + '_ {
191+
iter::once(&self.name).chain(self.aliases.iter())
194192
}
195193

196194
/// Tests if the value is valid for this argument value
@@ -212,21 +210,15 @@ impl<'help> PossibleValue<'help> {
212210
pub fn matches(&self, value: &str, ignore_case: bool) -> bool {
213211
if ignore_case {
214212
self.get_name_and_aliases()
215-
.any(|name| eq_ignore_case(name, value))
213+
.any(|name| eq_ignore_case(name.as_str(), value))
216214
} else {
217215
self.get_name_and_aliases().any(|name| name == value)
218216
}
219217
}
220218
}
221219

222-
impl<'help> From<&'help str> for PossibleValue<'help> {
223-
fn from(s: &'help str) -> Self {
224-
Self::new(s)
225-
}
226-
}
227-
228-
impl<'help> From<&'_ &'help str> for PossibleValue<'help> {
229-
fn from(s: &'_ &'help str) -> Self {
220+
impl<S: Into<Str>> From<S> for PossibleValue {
221+
fn from(s: S) -> Self {
230222
Self::new(s)
231223
}
232224
}

0 commit comments

Comments
 (0)