diff --git a/src/builder/str.rs b/src/builder/str.rs index 8dd9a7f4..e4750c74 100644 --- a/src/builder/str.rs +++ b/src/builder/str.rs @@ -131,7 +131,7 @@ impl AsRef<[u8]> for Str { impl AsRef for Str { #[inline] fn as_ref(&self) -> &std::ffi::OsStr { - (&**self).as_ref() + (**self).as_ref() } } diff --git a/src/builder/value_parser.rs b/src/builder/value_parser.rs index c5b97061..8a5ae1be 100644 --- a/src/builder/value_parser.rs +++ b/src/builder/value_parser.rs @@ -1930,25 +1930,25 @@ pub trait ValueParserFactory { impl ValueParserFactory for String { type Parser = ValueParser; fn value_parser() -> Self::Parser { - ValueParser::string() + ValueParser::string() // Default `clap_derive` to optimized implementation } } impl ValueParserFactory for std::ffi::OsString { type Parser = ValueParser; fn value_parser() -> Self::Parser { - ValueParser::os_string() + ValueParser::os_string() // Default `clap_derive` to optimized implementation } } impl ValueParserFactory for std::path::PathBuf { type Parser = ValueParser; fn value_parser() -> Self::Parser { - ValueParser::path_buf() + ValueParser::path_buf() // Default `clap_derive` to optimized implementation } } impl ValueParserFactory for bool { type Parser = ValueParser; fn value_parser() -> Self::Parser { - ValueParser::bool() + ValueParser::bool() // Default `clap_derive` to optimized implementation } } impl ValueParserFactory for u8 { @@ -2040,7 +2040,7 @@ pub mod via_prelude { type Parser; fn value_parser(&self) -> Self::Parser; } - impl _ValueParserViaFactory for &&_AutoValueParser

{ + impl _ValueParserViaFactory for &&&&&&_AutoValueParser

{ type Parser = P::Parser; fn value_parser(&self) -> Self::Parser { P::value_parser() @@ -2054,7 +2054,7 @@ pub mod via_prelude { fn value_parser(&self) -> Self::Output; } impl _ValueParserViaValueEnum - for &_AutoValueParser + for &&&&&_AutoValueParser { type Output = EnumValueParser; @@ -2063,19 +2063,79 @@ pub mod via_prelude { } } + #[doc(hidden)] + pub trait _ValueParserViaFromOsString: private::_ValueParserViaFromOsStringSealed { + fn value_parser(&self) -> _AnonymousValueParser; + } + impl _ValueParserViaFromOsString for &&&&_AutoValueParser + where + FromOsString: From + std::any::Any + Clone + Send + Sync + 'static, + { + fn value_parser(&self) -> _AnonymousValueParser { + _AnonymousValueParser( + OsStringValueParser::new() + .map(|s| FromOsString::from(s)) + .into(), + ) + } + } + + #[doc(hidden)] + pub trait _ValueParserViaFromOsStr: private::_ValueParserViaFromOsStrSealed { + fn value_parser(&self) -> _AnonymousValueParser; + } + impl _ValueParserViaFromOsStr for &&&_AutoValueParser + where + FromOsStr: + for<'s> From<&'s std::ffi::OsStr> + std::any::Any + Clone + Send + Sync + 'static, + { + fn value_parser(&self) -> _AnonymousValueParser { + _AnonymousValueParser( + OsStringValueParser::new() + .map(|s| FromOsStr::from(&s)) + .into(), + ) + } + } + + #[doc(hidden)] + pub trait _ValueParserViaFromString: private::_ValueParserViaFromStringSealed { + fn value_parser(&self) -> _AnonymousValueParser; + } + impl _ValueParserViaFromString for &&_AutoValueParser + where + FromString: From + std::any::Any + Clone + Send + Sync + 'static, + { + fn value_parser(&self) -> _AnonymousValueParser { + _AnonymousValueParser(StringValueParser::new().map(|s| FromString::from(s)).into()) + } + } + #[doc(hidden)] pub trait _ValueParserViaFromStr: private::_ValueParserViaFromStrSealed { fn value_parser(&self) -> _AnonymousValueParser; } - impl _ValueParserViaFromStr for _AutoValueParser + impl _ValueParserViaFromStr for &_AutoValueParser where - FromStr: std::str::FromStr + std::any::Any + Clone + Send + Sync + 'static, - ::Err: - Into>, + FromStr: for<'s> From<&'s str> + std::any::Any + Clone + Send + Sync + 'static, { fn value_parser(&self) -> _AnonymousValueParser { - let func: fn(&str) -> Result::Err> = - FromStr::from_str; + _AnonymousValueParser(StringValueParser::new().map(|s| FromStr::from(&s)).into()) + } + } + + #[doc(hidden)] + pub trait _ValueParserViaParse: private::_ValueParserViaParseSealed { + fn value_parser(&self) -> _AnonymousValueParser; + } + impl _ValueParserViaParse for _AutoValueParser + where + Parse: std::str::FromStr + std::any::Any + Clone + Send + Sync + 'static, + ::Err: Into>, + { + fn value_parser(&self) -> _AnonymousValueParser { + let func: fn(&str) -> Result::Err> = + Parse::from_str; _AnonymousValueParser(ValueParser::new(func)) } } @@ -2088,6 +2148,8 @@ pub mod via_prelude { /// - [Native types][ValueParser]: `bool`, `String`, `OsString`, `PathBuf` /// - [Ranged numeric types][RangedI64ValueParser]: `u8`, `i8`, `u16`, `i16, `u32`, `i32`, `u64`, `i64` /// - [`ValueEnum` types][crate::ValueEnum] +/// - [`From<&OsString>` types][std::convert::From] and [`From<&OsStr>` types][std::convert::From] +/// - [`From<&String>` types][std::convert::From] and [`From<&str>` types][std::convert::From] /// - [`FromStr` types][std::str::FromStr], including usize, isize /// /// # Example @@ -2155,28 +2217,52 @@ macro_rules! value_parser { ($name:ty) => {{ use $crate::builder::via_prelude::*; let auto = $crate::builder::_AutoValueParser::<$name>::new(); - (&&&auto).value_parser() + (&&&&&&auto).value_parser() }}; } mod private { use super::*; + // Prefer these so `clap_derive` defaults to optimized implementations pub trait _ValueParserViaSelfSealed {} - impl> _ValueParserViaSelfSealed for &&&_AutoValueParser

{} + impl> _ValueParserViaSelfSealed for &&&&&&&_AutoValueParser

{} pub trait _ValueParserViaFactorySealed {} - impl _ValueParserViaFactorySealed for &&_AutoValueParser

{} + impl _ValueParserViaFactorySealed for &&&&&&_AutoValueParser

{} pub trait _ValueParserViaValueEnumSealed {} - impl _ValueParserViaValueEnumSealed for &_AutoValueParser {} + impl _ValueParserViaValueEnumSealed for &&&&&_AutoValueParser {} + + pub trait _ValueParserViaFromOsStringSealed {} + impl _ValueParserViaFromOsStringSealed for &&&&_AutoValueParser where + FromOsString: From + std::any::Any + Send + Sync + 'static + { + } + + pub trait _ValueParserViaFromOsStrSealed {} + impl _ValueParserViaFromOsStrSealed for &&&_AutoValueParser where + FromOsStr: for<'s> From<&'s std::ffi::OsStr> + std::any::Any + Send + Sync + 'static + { + } + + pub trait _ValueParserViaFromStringSealed {} + impl _ValueParserViaFromStringSealed for &&_AutoValueParser where + FromString: From + std::any::Any + Send + Sync + 'static + { + } pub trait _ValueParserViaFromStrSealed {} - impl _ValueParserViaFromStrSealed for _AutoValueParser + impl _ValueParserViaFromStrSealed for &_AutoValueParser where + FromStr: for<'s> From<&'s str> + std::any::Any + Send + Sync + 'static + { + } + + pub trait _ValueParserViaParseSealed {} + impl _ValueParserViaParseSealed for _AutoValueParser where - FromStr: std::str::FromStr + std::any::Any + Send + Sync + 'static, - ::Err: - Into>, + Parse: std::str::FromStr + std::any::Any + Send + Sync + 'static, + ::Err: Into>, { } }