Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: Remove lifetime from PossibleValue
Browse files Browse the repository at this point in the history
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.
epage committed Aug 16, 2022
1 parent abd4205 commit c5d8eb4
Showing 21 changed files with 103 additions and 91 deletions.
2 changes: 1 addition & 1 deletion clap_complete/src/generator/utils.rs
Original file line number Diff line number Diff line change
@@ -127,7 +127,7 @@ pub fn flags<'help>(p: &Command<'help>) -> Vec<Arg<'help>> {
}

/// Get the possible values for completion
pub fn possible_values<'help>(a: &Arg<'help>) -> Option<Vec<clap::builder::PossibleValue<'help>>> {
pub fn possible_values(a: &Arg<'_>) -> Option<Vec<clap::builder::PossibleValue>> {
if !a.get_num_args().expect("built").takes_values() {
None
} else {
3 changes: 1 addition & 2 deletions clap_complete/src/shells/bash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{fmt::Write as _, io::Write};

use clap::builder::PossibleValue;
use clap::*;

use crate::generator::{utils, Generator};
@@ -180,7 +179,7 @@ fn vals_for(o: &Arg) -> String {
"$(compgen -W \"{}\" -- \"${{cur}}\")",
vals.iter()
.filter(|pv| !pv.is_hide_set())
.map(PossibleValue::get_name)
.map(|n| n.get_name().as_str())
.collect::<Vec<_>>()
.join(" ")
)
3 changes: 2 additions & 1 deletion clap_complete/src/shells/fish.rs
Original file line number Diff line number Diff line change
@@ -164,7 +164,8 @@ fn value_completion(option: &Arg) -> String {
Some(format!(
"{}\t{}",
escape_string(value.get_name()).as_str(),
escape_string(value.get_help().unwrap_or_default()).as_str()
escape_string(value.get_help().map(|s| s.as_str()).unwrap_or_default())
.as_str()
))
})
.collect::<Vec<_>>()
2 changes: 1 addition & 1 deletion clap_complete/src/shells/shell.rs
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ impl ValueEnum for Shell {
]
}

fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>> {
fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
Some(match self {
Shell::Bash => PossibleValue::new("bash"),
Shell::Elvish => PossibleValue::new("elvish"),
11 changes: 7 additions & 4 deletions clap_complete/src/shells/zsh.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::io::Write;

use clap::builder::PossibleValue;
use clap::*;

use crate::generator::{utils, Generator};
@@ -375,8 +374,12 @@ fn value_completion(arg: &Arg) -> Option<String> {
} else {
Some(format!(
r#"{name}\:"{tooltip}""#,
name = escape_value(value.get_name()),
tooltip = value.get_help().map(escape_help).unwrap_or_default()
name = escape_value(value.get_name().as_str()),
tooltip = value
.get_help()
.map(|s| s.as_str())
.map(escape_help)
.unwrap_or_default()
))
}
})
@@ -389,7 +392,7 @@ fn value_completion(arg: &Arg) -> Option<String> {
values
.iter()
.filter(|pv| !pv.is_hide_set())
.map(PossibleValue::get_name)
.map(|n| n.get_name().as_str())
.collect::<Vec<_>>()
.join(" ")
))
17 changes: 6 additions & 11 deletions clap_derive/src/attrs.rs
Original file line number Diff line number Diff line change
@@ -499,7 +499,7 @@ impl Attrs {
quote_spanned!(ident.span()=> {
{
let val: #ty = #val;
clap::ValueEnum::to_possible_value(&val).unwrap().get_name()
clap::ValueEnum::to_possible_value(&val).unwrap().get_name().to_owned()
}
})
} else {
@@ -544,17 +544,15 @@ impl Attrs {
let val = if parsed.iter().any(|a| matches!(a, ValueEnum(_))) {
quote_spanned!(ident.span()=> {
{
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> Vec<&'static str>
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> impl Iterator<Item=clap::Str>
where
T: ::std::borrow::Borrow<#inner_type>
{
iterable
.into_iter()
.map(|val| {
clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name()
clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name().to_owned()
})
.collect()

}

iter_to_vals(#expr)
@@ -603,7 +601,7 @@ impl Attrs {
quote_spanned!(ident.span()=> {
{
let val: #ty = #val;
clap::ValueEnum::to_possible_value(&val).unwrap().get_name()
clap::ValueEnum::to_possible_value(&val).unwrap().get_name().to_owned()
}
})
} else {
@@ -648,18 +646,15 @@ impl Attrs {
let val = if parsed.iter().any(|a| matches!(a, ValueEnum(_))) {
quote_spanned!(ident.span()=> {
{
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> Vec<&'static ::std::ffi::OsStr>
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> impl Iterator<Item=clap::Str>
where
T: ::std::borrow::Borrow<#inner_type>
{
iterable
.into_iter()
.map(|val| {
clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name()
clap::ValueEnum::to_possible_value(val.borrow()).unwrap().get_name().to_owned()
})
.map(::std::ffi::OsStr::new)
.collect()

}

iter_to_vals(#expr)
2 changes: 1 addition & 1 deletion clap_derive/src/derives/value_enum.rs
Original file line number Diff line number Diff line change
@@ -114,7 +114,7 @@ fn gen_to_possible_value(lits: &[(TokenStream, Ident)]) -> TokenStream {
let (lit, variant): (Vec<TokenStream>, Vec<Ident>) = lits.iter().cloned().unzip();

quote! {
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue<'a>> {
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue> {
match self {
#(Self::#variant => Some(#lit),)*
_ => None
2 changes: 1 addition & 1 deletion clap_derive/src/dummies.rs
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ pub fn value_enum(name: &Ident) {
fn from_str(_input: &str, _ignore_case: bool) -> ::std::result::Result<Self, String> {
unimplemented!()
}
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue<'a>>{
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue>{
unimplemented!()
}
}
2 changes: 1 addition & 1 deletion examples/tutorial_builder/04_01_enum.rs
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ impl ValueEnum for Mode {
&[Mode::Fast, Mode::Slow]
}

fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>> {
fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
Some(match self {
Mode::Fast => PossibleValue::new("fast"),
Mode::Slow => PossibleValue::new("slow"),
2 changes: 1 addition & 1 deletion src/builder/arg.rs
Original file line number Diff line number Diff line change
@@ -3659,7 +3659,7 @@ impl<'help> Arg<'help> {
Some(longs)
}

pub(crate) fn get_possible_values(&self) -> Vec<PossibleValue<'help>> {
pub(crate) fn get_possible_values(&self) -> Vec<PossibleValue> {
if !self.is_takes_value_set() {
vec![]
} else {
64 changes: 28 additions & 36 deletions src/builder/possible_value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{borrow::Cow, iter};

use crate::util::eq_ignore_case;
use crate::Str;

/// A possible value of an argument.
///
@@ -27,14 +28,14 @@ use crate::util::eq_ignore_case;
/// [hide]: PossibleValue::hide()
/// [help]: PossibleValue::help()
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct PossibleValue<'help> {
name: &'help str,
help: Option<&'help str>,
aliases: Vec<&'help str>, // (name, visible)
pub struct PossibleValue {
name: Str,
help: Option<Str>,
aliases: Vec<Str>, // (name, visible)
hide: bool,
}

impl<'help> PossibleValue<'help> {
impl PossibleValue {
/// Create a [`PossibleValue`] with its name.
///
/// 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> {
/// [hidden]: PossibleValue::hide
/// [possible value]: crate::builder::PossibleValuesParser
/// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values()
pub fn new(name: &'help str) -> Self {
pub fn new(name: impl Into<Str>) -> Self {
PossibleValue {
name,
name: name.into(),
..Default::default()
}
}
@@ -74,8 +75,8 @@ impl<'help> PossibleValue<'help> {
/// ```
#[inline]
#[must_use]
pub fn help(mut self, help: &'help str) -> Self {
self.help = Some(help);
pub fn help(mut self, help: impl Into<Str>) -> Self {
self.help = Some(help.into());
self
}

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

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

/// Reflection
impl<'help> PossibleValue<'help> {
impl PossibleValue {
/// Get the name of the argument value
#[inline]
pub fn get_name(&self) -> &'help str {
self.name
pub fn get_name(&self) -> &Str {
&self.name
}

/// Get the help specified for this argument, if any
#[inline]
pub fn get_help(&self) -> Option<&'help str> {
self.help
pub fn get_help(&self) -> Option<&Str> {
self.help.as_ref()
}

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

/// Get the name if argument value is not hidden, `None` otherwise,
/// but wrapped in quotes if it contains whitespace
pub(crate) fn get_visible_quoted_name(&self) -> Option<Cow<'help, str>> {
pub(crate) fn get_visible_quoted_name(&self) -> Option<Cow<'_, str>> {
if !self.hide {
Some(if self.name.contains(char::is_whitespace) {
format!("{:?}", self.name).into()
} else {
self.name.into()
self.name.as_str().into()
})
} else {
None
@@ -189,8 +187,8 @@ impl<'help> PossibleValue<'help> {
/// Returns all valid values of the argument value.
///
/// Namely the name and all aliases.
pub fn get_name_and_aliases(&self) -> impl Iterator<Item = &'help str> + '_ {
iter::once(&self.name).chain(&self.aliases).copied()
pub fn get_name_and_aliases(&self) -> impl Iterator<Item = &Str> + '_ {
iter::once(&self.name).chain(self.aliases.iter())
}

/// Tests if the value is valid for this argument value
@@ -212,21 +210,15 @@ impl<'help> PossibleValue<'help> {
pub fn matches(&self, value: &str, ignore_case: bool) -> bool {
if ignore_case {
self.get_name_and_aliases()
.any(|name| eq_ignore_case(name, value))
.any(|name| eq_ignore_case(name.as_str(), value))
} else {
self.get_name_and_aliases().any(|name| name == value)
}
}
}

impl<'help> From<&'help str> for PossibleValue<'help> {
fn from(s: &'help str) -> Self {
Self::new(s)
}
}

impl<'help> From<&'_ &'help str> for PossibleValue<'help> {
fn from(s: &'_ &'help str) -> Self {
impl<S: Into<Str>> From<S> for PossibleValue {
fn from(s: S) -> Self {
Self::new(s)
}
}
42 changes: 21 additions & 21 deletions src/builder/value_parser.rs
Original file line number Diff line number Diff line change
@@ -244,7 +244,7 @@ impl ValueParser {
/// applications like errors and completion.
pub fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> {
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
self.any_value_parser().possible_values()
}

@@ -503,7 +503,7 @@ impl From<std::ops::RangeFull> for ValueParser {
/// ```
impl<P, const C: usize> From<[P; C]> for ValueParser
where
P: Into<super::PossibleValue<'static>>,
P: Into<super::PossibleValue>,
{
fn from(values: [P; C]) -> Self {
let inner = PossibleValuesParser::from(values);
@@ -556,7 +556,7 @@ trait AnyValueParser: Send + Sync + 'static {

fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>>;
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>>;

fn clone_any(&self) -> Box<dyn AnyValueParser>;
}
@@ -592,7 +592,7 @@ where

fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> {
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
P::possible_values(self)
}

@@ -634,7 +634,7 @@ pub trait TypedValueParser: Clone + Send + Sync + 'static {
/// applications like errors and completion.
fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> {
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
None
}
}
@@ -833,7 +833,7 @@ impl Default for PathBufValueParser {
/// &[Self::Always, Self::Auto, Self::Never]
/// }
///
/// fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue<'a>> {
/// fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue> {
/// match self {
/// Self::Always => Some(clap::builder::PossibleValue::new("always")),
/// Self::Auto => Some(clap::builder::PossibleValue::new("auto")),
@@ -893,7 +893,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for E
.iter()
.filter_map(|v| v.to_possible_value())
.filter(|v| !v.is_hide_set())
.map(|v| v.get_name())
.map(|v| v.get_name().as_str().to_owned())
.collect::<Vec<_>>()
};

@@ -928,7 +928,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for E

fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> {
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
Some(Box::new(
E::value_variants()
.iter()
@@ -979,7 +979,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> Default for EnumValueP
/// assert_eq!(value_parser.parse_ref(&cmd, arg, OsStr::new("never")).unwrap(), "never");
/// ```
#[derive(Clone, Debug)]
pub struct PossibleValuesParser(Vec<super::PossibleValue<'static>>);
pub struct PossibleValuesParser(Vec<super::PossibleValue>);

impl PossibleValuesParser {
/// Verify the value is from an enumerated set pf [`PossibleValue`][crate::builder::PossibleValue].
@@ -1021,7 +1021,7 @@ impl TypedValueParser for PossibleValuesParser {
.0
.iter()
.filter(|v| !v.is_hide_set())
.map(crate::builder::PossibleValue::get_name)
.map(|v| v.get_name().as_str().to_owned())
.collect::<Vec<_>>();

Err(crate::Error::invalid_value(
@@ -1036,15 +1036,15 @@ impl TypedValueParser for PossibleValuesParser {

fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> {
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
Some(Box::new(self.0.iter().cloned()))
}
}

impl<I, T> From<I> for PossibleValuesParser
where
I: IntoIterator<Item = T>,
T: Into<super::PossibleValue<'static>>,
T: Into<super::PossibleValue>,
{
fn from(values: I) -> Self {
Self(values.into_iter().map(|t| t.into()).collect())
@@ -1067,7 +1067,7 @@ where
/// );
///
/// let m = cmd.try_get_matches_from_mut(["cmd", "--port", "3001"]).unwrap();
/// let port: u16 = *m.get_one("port")
/// let port: u16 = *m.et_one("port")
/// .expect("required");
/// assert_eq!(port, 3001);
/// ```
@@ -1458,7 +1458,7 @@ impl BoolValueParser {
Self {}
}

fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue<'static>> {
fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> {
["true", "false"]
.iter()
.copied()
@@ -1482,7 +1482,7 @@ impl TypedValueParser for BoolValueParser {
} else {
// Intentionally showing hidden as we hide all of them
let possible_vals = Self::possible_values()
.map(|v| v.get_name())
.map(|v| v.get_name().as_str().to_owned())
.collect::<Vec<_>>();

return Err(crate::Error::invalid_value(
@@ -1498,7 +1498,7 @@ impl TypedValueParser for BoolValueParser {

fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> {
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
Some(Box::new(Self::possible_values()))
}
}
@@ -1557,7 +1557,7 @@ impl FalseyValueParser {
Self {}
}

fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue<'static>> {
fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> {
crate::util::TRUE_LITERALS
.iter()
.chain(crate::util::FALSE_LITERALS.iter())
@@ -1591,7 +1591,7 @@ impl TypedValueParser for FalseyValueParser {

fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> {
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
Some(Box::new(Self::possible_values()))
}
}
@@ -1654,7 +1654,7 @@ impl BoolishValueParser {
Self {}
}

fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue<'static>> {
fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> {
crate::util::TRUE_LITERALS
.iter()
.chain(crate::util::FALSE_LITERALS.iter())
@@ -1690,7 +1690,7 @@ impl TypedValueParser for BoolishValueParser {

fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue<'static>> + '_>> {
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> {
Some(Box::new(Self::possible_values()))
}
}
@@ -2028,7 +2028,7 @@ pub mod via_prelude {
/// # fn value_variants<'a>() -> &'a [Self] {
/// # &[Self::Always, Self::Auto, Self::Never]
/// # }
/// # fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue<'a>> {
/// # fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue> {
/// # match self {
/// # Self::Always => Some(clap::builder::PossibleValue::new("always")),
/// # Self::Auto => Some(clap::builder::PossibleValue::new("auto")),
2 changes: 1 addition & 1 deletion src/derive.rs
Original file line number Diff line number Diff line change
@@ -387,7 +387,7 @@ pub trait ValueEnum: Sized + Clone {
/// The canonical argument value.
///
/// The value is `None` for skipped variants.
fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>>;
fn to_possible_value(&self) -> Option<PossibleValue>;
}

impl<T: Parser> Parser for Box<T> {
4 changes: 2 additions & 2 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -243,7 +243,7 @@ impl Error {
])
}

pub(crate) fn empty_value(cmd: &Command, good_vals: &[&str], arg: String) -> Self {
pub(crate) fn empty_value(cmd: &Command, good_vals: &[String], arg: String) -> Self {
Self::invalid_value(cmd, "".to_owned(), good_vals, arg)
}

@@ -259,7 +259,7 @@ impl Error {
pub(crate) fn invalid_value(
cmd: &Command,
bad_val: String,
good_vals: &[&str],
good_vals: &[String],
arg: String,
) -> Self {
let suggestion = suggestions::did_you_mean(&bad_val, good_vals.iter()).pop();
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -110,7 +110,7 @@ pub use crate::util::color::ColorChoice;
pub(crate) use crate::util::color::ColorChoice;
pub use crate::util::Id;
pub use crate::util::OsStr;
pub(crate) use crate::util::Str;
pub use crate::util::Str;

pub use crate::derive::{Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum};

2 changes: 1 addition & 1 deletion src/output/help.rs
Original file line number Diff line number Diff line change
@@ -504,7 +504,7 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> {
self.none("\n")?;
self.spaces(spaces)?;
self.none("- ")?;
self.good(pv.get_name())?;
self.good(pv.get_name().as_str())?;
if let Some(help) = pv.get_help() {
debug!("Help::help: Possible Value help");

3 changes: 1 addition & 2 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ use clap_lex::RawOsStr;
use clap_lex::RawOsString;

// Internal
use crate::builder::PossibleValue;
use crate::builder::{Arg, Command};
use crate::error::Error as ClapError;
use crate::error::Result as ClapResult;
@@ -1266,7 +1265,7 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
&super::get_possible_values_cli(arg)
.iter()
.filter(|pv| !pv.is_hide_set())
.map(PossibleValue::get_name)
.map(|n| n.get_name().as_str().to_owned())
.collect::<Vec<_>>(),
arg.to_string(),
));
4 changes: 2 additions & 2 deletions src/parser/validator.rs
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ impl<'help, 'cmd> Validator<'help, 'cmd> {
&get_possible_values_cli(o)
.iter()
.filter(|pv| !pv.is_hide_set())
.map(PossibleValue::get_name)
.map(|n| n.get_name().as_str().to_owned())
.collect::<Vec<_>>(),
o.to_string(),
));
@@ -455,7 +455,7 @@ impl Conflicts {
}
}

pub(crate) fn get_possible_values_cli<'help>(a: &Arg<'help>) -> Vec<PossibleValue<'help>> {
pub(crate) fn get_possible_values_cli(a: &Arg<'_>) -> Vec<PossibleValue> {
if !a.is_takes_value_set() {
vec![]
} else {
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ pub(crate) use self::flat_map::Entry;
pub(crate) use self::flat_map::FlatMap;
pub(crate) use self::flat_set::FlatSet;
pub(crate) use self::graph::ChildGraph;
pub(crate) use self::str::Inner as StrInner;
pub(crate) use self::str_to_bool::str_to_bool;
pub(crate) use self::str_to_bool::FALSE_LITERALS;
pub(crate) use self::str_to_bool::TRUE_LITERALS;
18 changes: 18 additions & 0 deletions src/util/os_str.rs
Original file line number Diff line number Diff line change
@@ -40,6 +40,24 @@ impl From<&'_ OsStr> for OsStr {
}
}

impl From<crate::Str> for OsStr {
fn from(id: crate::Str) -> Self {
match id.into_inner() {
crate::util::StrInner::Static(s) => Self::from_static_ref(std::ffi::OsStr::new(s)),
crate::util::StrInner::Owned(s) => Self::from_ref(std::ffi::OsStr::new(s.as_ref())),
}
}
}

impl From<&'_ crate::Str> for OsStr {
fn from(id: &'_ crate::Str) -> Self {
match id.clone().into_inner() {
crate::util::StrInner::Static(s) => Self::from_static_ref(std::ffi::OsStr::new(s)),
crate::util::StrInner::Owned(s) => Self::from_ref(std::ffi::OsStr::new(s.as_ref())),
}
}
}

impl From<std::ffi::OsString> for OsStr {
fn from(name: std::ffi::OsString) -> Self {
Self::from_string(name)
6 changes: 5 additions & 1 deletion src/util/str.rs
Original file line number Diff line number Diff line change
@@ -23,6 +23,10 @@ impl Str {
}
}

pub(crate) fn into_inner(self) -> Inner {
self.name
}

/// Get the raw string of the `Str`
pub fn as_str(&self) -> &str {
self.name.as_str()
@@ -139,7 +143,7 @@ impl PartialEq<std::string::String> for Str {
}

#[derive(Clone)]
enum Inner {
pub(crate) enum Inner {
Static(&'static str),
Owned(Box<str>),
}

0 comments on commit c5d8eb4

Please sign in to comment.