Skip to content
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

Make FromStr::Err not need to be a Send+Sync+'static #5149

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ name = "04_02_parse_derive"
path = "examples/tutorial_derive/04_02_parse.rs"
required-features = ["derive"]

[[example]]
name = "04_02_01_parse_from_str"
path = "examples/tutorial_derive/04_02_01_parse_from_str.rs"
required-features = ["derive"]

[[example]]
name = "04_02_validate_derive"
path = "examples/tutorial_derive/04_02_validate.rs"
Expand Down
6 changes: 3 additions & 3 deletions clap_builder/src/builder/value_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ pub trait TypedValueParser: Clone + Send + Sync + 'static {
impl<F, T, E> TypedValueParser for F
where
F: Fn(&str) -> Result<T, E> + Clone + Send + Sync + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
E: Into<Box<dyn std::error::Error>>,
T: Send + Sync + Clone,
{
type Value = T;
Expand Down Expand Up @@ -2576,7 +2576,7 @@ pub mod via_prelude {
impl<Parse> _ValueParserViaParse for _AutoValueParser<Parse>
where
Parse: std::str::FromStr + std::any::Any + Clone + Send + Sync + 'static,
<Parse as std::str::FromStr>::Err: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
<Parse as std::str::FromStr>::Err: Into<Box<dyn std::error::Error>>,
{
fn value_parser(&self) -> _AnonymousValueParser {
let func: fn(&str) -> Result<Parse, <Parse as std::str::FromStr>::Err> =
Expand Down Expand Up @@ -2688,7 +2688,7 @@ mod private {
impl<Parse> _ValueParserViaParseSealed for _AutoValueParser<Parse>
where
Parse: std::str::FromStr + std::any::Any + Send + Sync + 'static,
<Parse as std::str::FromStr>::Err: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
<Parse as std::str::FromStr>::Err: Into<Box<dyn std::error::Error>>,
{
}
}
Expand Down
8 changes: 4 additions & 4 deletions clap_builder/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct ErrorInner {
#[cfg(feature = "error-context")]
context: FlatMap<ContextKind, ContextValue>,
message: Option<Message>,
source: Option<Box<dyn error::Error + Send + Sync>>,
source: Option<Box<dyn error::Error>>,
help_flag: Option<&'static str>,
styles: Styles,
color_when: ColorChoice,
Expand Down Expand Up @@ -299,7 +299,7 @@ impl<F: ErrorFormatter> Error<F> {
self
}

pub(crate) fn set_source(mut self, source: Box<dyn error::Error + Send + Sync>) -> Self {
pub(crate) fn set_source(mut self, source: Box<dyn error::Error>) -> Self {
self.inner.source = Some(source);
self
}
Expand Down Expand Up @@ -632,7 +632,7 @@ impl<F: ErrorFormatter> Error<F> {
pub(crate) fn value_validation(
arg: String,
val: String,
err: Box<dyn error::Error + Send + Sync>,
err: Box<dyn error::Error>,
) -> Self {
let mut err = Self::new(ErrorKind::ValueValidation).set_source(err);

Expand Down Expand Up @@ -919,5 +919,5 @@ impl Display for Backtrace {

#[test]
fn check_auto_traits() {
static_assertions::assert_impl_all!(Error: Send, Sync, Unpin);
static_assertions::assert_impl_all!(Error: Unpin);
}
47 changes: 47 additions & 0 deletions examples/tutorial_derive/04_02_01_parse_from_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::fmt::{Display, Formatter};
use std::marker::PhantomData;

#[derive(Clone, Debug)]
struct Foo {
v: u32
}

impl std::str::FromStr for Foo {
type Err = NotSendSyncError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s=="error" {
Err(NotSendSyncError{_pd: PhantomData })
} else {
Ok(Foo{v: 42})
}
}
}

#[derive(Debug)]
struct NotSendSyncError {
_pd: PhantomData<std::cell::RefCell<()>>,
}

impl Display for NotSendSyncError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{:?}", self)
}
}

impl std::error::Error for NotSendSyncError {

}

#[derive(clap::Parser, Debug, Clone)]
#[structopt(name = "use FromStr")]
struct Config {
//#[arg(long, value_parser=clap::value_parser!(Foo))]
#[arg(long)]
foo: Foo
}

fn main() {
let config = <Config as clap::Parser>::parse();
println!("foo.v={}", config.foo.v);
}
6 changes: 3 additions & 3 deletions examples/typed-derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ struct Args {
}

/// Parse a single key-value pair
fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error>>
where
T: std::str::FromStr,
T::Err: Error + Send + Sync + 'static,
T::Err: Error,
U: std::str::FromStr,
U::Err: Error + Send + Sync + 'static,
U::Err: Error,
{
let pos = s
.find('=')
Expand Down