-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imp: Make
clap_derive
call FromStr::from_str
only once per argument.
Currently the way `clap_derive` uses a `from_str` function is to call it once as a validator, discarding the parsed value, and then to call it again to recompute the parsed value. This is unfortunate in cases where `from_str` is expensive or has side effects. This PR changes `clap_derive` to not register `from_str` as a validator so that it doesn't do the first of these two calls. Then, instead of doing `unwrap()` on the other call, it handles the error. This eliminates the redundancy, and also avoids the small performance hit mentioned in [the documentation about validators]. [the documentation about validators]: https://docs.rs/clap-v3/3.0.0-beta.1/clap_v3/struct.Arg.html#method.validator This PR doesn't yet use colorized messages for errors generated during parsing because the `ColorWhen` setting isn't currently available. That's fixable with some refactoring, but I'm interested in getting feedback on the overall approach here first.
- Loading branch information
1 parent
37889c6
commit d892f6d
Showing
7 changed files
with
159 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use clap::Clap; | ||
use std::str::FromStr; | ||
use std::sync::atomic::{AtomicU32, Ordering::SeqCst}; | ||
|
||
static NUM_CALLS: AtomicU32 = AtomicU32::new(0); | ||
|
||
#[derive(Debug)] | ||
struct Effectful {} | ||
|
||
impl FromStr for Effectful { | ||
type Err = String; | ||
|
||
fn from_str(_s: &str) -> Result<Self, Self::Err> { | ||
NUM_CALLS.fetch_add(1, SeqCst); | ||
Ok(Self {}) | ||
} | ||
} | ||
|
||
#[derive(Clap, Debug)] | ||
struct Opt { | ||
effectful: Effectful, | ||
} | ||
|
||
#[test] | ||
fn effectful() { | ||
let _opt = Opt::parse_from(&["test", "arg"]); | ||
assert_eq!(NUM_CALLS.load(SeqCst), 1); | ||
} |
Oops, something went wrong.