-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
fix(builder)!: Accept a more liberal Iterator #2857
Conversation
This replaces the most common uses of `&[]` with `IntoIterator`. This is based on clap-rs#2414. Unlike that, this stays consistent with using `Into<&'help str>`. There are some remaining functions not converted over, like defaults and requires. BREAKING CHANGE: Instances like `aliases(&[])` need to switch to `aliases([])`.
This worth it for 3.0? What more functions would we need to convert over for this to be considered "done" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one comment.
@@ -2588,8 +2604,12 @@ impl<'help> Arg<'help> { | |||
/// [`Arg::number_of_values`]: Arg::number_of_values() | |||
/// [`Arg::takes_value(true)`]: Arg::takes_value() | |||
/// [`Arg::multiple_values(true)`]: Arg::multiple_values() | |||
pub fn value_names(mut self, names: &[&'help str]) -> Self { | |||
self.val_names = names.to_vec(); | |||
pub fn value_names<I>(mut self, names: I) -> Self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub fn value_names<I>(mut self, names: I) -> Self | |
pub fn value_names<I, T>(mut self, names: I) -> Self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was generally aiming to be consistent with what the non-iterator versions accepted. In this case, value_name
accepts a &str
.
We can change that if we want.
I thought this wasn't needed. I guess that is why #2414 introduced |
More so I think its because it did where
I: IntoIterator<Item = &'help T>,
T: AsRef<str> + 'help, |
Looks like that wasn't needed and this can work
Going to go do a PR to switch Into<&str> to AsRef in prep for this one |
Do we need lifetime in that |
Bad news is we can't control the lifetime for |
Wouldn't the following work? where
I: IntoIterator<Item = T>,
T: AsRef<str> + 'help, |
That can work but what I was looking at was how to be consistent between singular and iterator functions. If we don't care about consistency, that is fine. |
I am not sure what you mean by that |
Currently, most cases that take a single value have signatures like pub fn alias<S: Into<&'help str>>(mut self, name: S) -> Self {
self.aliases.push((name.into(), false));
self
} Currently, that looks like pub fn alias<S: Into<&'help str>>(mut self, name: S) -> Self {
self.aliases.push((name.into(), false));
self
} This PR changes it to pub fn aliases<I, T>(mut self, names: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<&'help str>,
{
self.aliases
.extend(names.into_iter().map(|x| (x.into(), false)));
self
} So |
Ah, yeah we should not mind the signature being inconsistent between singular and plural apis as long as all kinds of inputs are accepted by the function. |
Something to keep in mind, https://doc.rust-lang.org/edition-guide/rust-2021/IntoIterator-for-arrays.html |
I played with it some more and, as expected, received some lifetime complaints (because I am not taking the approach of doing a We should defer this
|
This replaces the most common uses of
&[]
withIntoIterator
.This is based on #2414. Unlike that, this stays consistent with using
Into<&'help str>
.There are some remaining functions not converted over, like defaults and
requires.
BREAKING CHANGE: Instances like
aliases(&[])
need to switch toaliases([])
.