-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Derive requires field types to impl Clone
#4286
Comments
Can you provide a complete reproduction case? For example, I want to make sure I'm digging into this with the same error type you are using. |
This compiles in the playground with clap 3: use clap::Parser;
use anyhow::Error;
#[derive(Debug, Parser)]
#[clap(name = "topfew")]
struct Options {
/// Fields to use as part of the line's key
#[clap(long, short)]
fields: FieldSpec,
}
#[derive(Debug)]
struct FieldSpec {
indices: Vec<usize>,
}
impl std::str::FromStr for FieldSpec {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut indices = Vec::new();
for f in s.split(',') {
indices.push(usize::from_str(f)?);
}
Ok(FieldSpec { indices })
}
} |
My first step was to opt-in to clap v4's behavior with clap 3 by adding the #!/usr/bin/env -S rust-script
//! ```cargo
//! [dependencies]
//! clap = { version = "3", features = ["derive"] }
//! anyhow = "1"
//! ```
use anyhow::Error;
use clap::Parser;
#[derive(Debug, Parser)]
#[clap(name = "topfew")]
struct Options {
/// Fields to use as part of the line's key
#[clap(long, short, value_parser)]
fields: FieldSpec,
}
#[derive(Debug, Clone)]
struct FieldSpec {
indices: Vec<usize>,
}
impl std::str::FromStr for FieldSpec {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut indices = Vec::new();
for f in s.split(',') {
indices.push(usize::from_str(f)?);
}
Ok(FieldSpec { indices })
}
} |
And it turns out that was what was needed to get it working with clap v4 #!/usr/bin/env -S rust-script
//! ```cargo
//! [dependencies]
//! clap = { version = "4", features = ["derive"] }
//! anyhow = "1"
//! ```
use anyhow::Error;
use clap::Parser;
#[derive(Debug, Parser)]
#[clap(name = "topfew")]
struct Options {
/// Fields to use as part of the line's key
#[clap(long, short)]
fields: FieldSpec,
}
#[derive(Debug, Clone)]
struct FieldSpec {
indices: Vec<usize>,
}
impl std::str::FromStr for FieldSpec {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut indices = Vec::new();
for f in s.split(',') {
indices.push(usize::from_str(f)?);
}
Ok(FieldSpec { indices })
}
} |
While the listed error message wasn't too helpful in figuring out what was going on, earlier I got this error message
|
So it's just the |
That is an artifact of the derive API being built on top of the builder API. This is happening on two layers
|
Clone
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
For the issue described in my last comment, the error message will go away if you give Update: I filed an issue for it: #5360 |
Related clap changelog: * (help) Make DeriveDisplayOrder the default and removed the setting. To sort help, set next_display_order(None) (#2808) * (derive) Remove arg_enum attribute in favor of value_enum to match the new name (we didn't have support in v3 to mark it deprecated) (#4127) * clap::ArgEnum to clap::ValueEnum (#3799) * (error) clap::error::ErrorKind is now preferred over clap::ErrorKind (#3395) Related bugs: * clap-rs/clap#4286 Derive requires field types to impl Clone BUG=b:337717817 FIXED=b:337717817 TEST=CQ Change-Id: Ib8e81a9ad387c52fff76a166a50191b297da98ed Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5500461 Tested-by: [email protected] <[email protected]> Reviewed-by: Chih-Yang Hsia <[email protected]> Commit-Queue: Li-Yu Yu <[email protected]>
Maintainer's notes
Send
andSync
#4347Please complete the following tasks
Rust Version
rustc 1.64.0 (a55dd71d5 2022-09-19)
Clap Version
4.0.2
Minimal reproducible code
Steps to reproduce the bug with the above code
cargo check
Actual Behaviour
Fails with an elaborate error pointing to many trait bounds with
_AutoValueParser<FieldSpec>
:Expected Behaviour
As in clap 3, this should just work.
Additional Context
I find it very surprising that
From<&str>
andFrom<String>
are supported, butFromStr
orTryFrom
impls are not.Debug Output
No response
The text was updated successfully, but these errors were encountered: