Skip to content

Commit

Permalink
test: Test FromStr::Err: !Send+!Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
chabapok committed Sep 30, 2023
1 parent cedbd4a commit 7c92748
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
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
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

0 comments on commit 7c92748

Please sign in to comment.