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

config proc macro doesn't support generic parameters with inline bounds #30

Open
luketpeterson opened this issue Jun 20, 2023 · 2 comments

Comments

@luketpeterson
Copy link
Contributor

luketpeterson commented Jun 20, 2023

The following code won't compile:

#[config]
#[derive(clap::Parser, Debug, Default)]
pub struct FrameworkConfig<AppArgsT: clap::Args + std::fmt::Debug + Default + DeserializeOwned + Serialize> {

    #[command(flatten)]
    #[serde(skip)]
    pub app_args: AppArgsT,

    #[serde(rename = "scratchPath")]
    pub scratch_path: Option<PathBuf>,
}

I get the following error:

struct takes 1 generic argument but 0 generic arguments were supplied
expected 1 generic argument
@luketpeterson
Copy link
Contributor Author

Investigating further, The issue is due to the fact that I inlined the bounds for the type parameter. The problem is that it's not valid Rust syntax to include the bounds in the type instantiation part of the impl. See the second use of #struct_gen here:

impl #struct_gen #struct_name #struct_gen #struct_where {

Unfortunately there isn't an easy fix offered by the syn crate. The params field of https://docs.rs/syn/latest/syn/struct.Generics.html needs to be parsed manually to separate the type identifiers from the bounds.

Luckily, using a where clause in the struct definition instead of inline bounds gets past this problem, but it appears there is a problem onion.

@luketpeterson
Copy link
Contributor Author

luketpeterson commented Jun 20, 2023

So this code gets further:

#[config]
#[derive(Parser, Debug, Default)]
pub struct FrameworkConfig <AppArgsT>
    where AppArgsT: Args + Clone + Sync + Send + std::fmt::Debug + Default + DeserializeOwned + Serialize
{
    #[command(flatten)]
    #[serde(skip)]
    pub app_args: AppArgsT,

    #[serde(rename = "scratchPath")]
    pub scratch_path: Option<PathBuf>,
}

But it still hits the rocks in the clap::Parser derive because the #[config] macro eats the attribute intended for clap. But that's a separate issue.

@luketpeterson luketpeterson changed the title config proc macro doesn't support generic parameters config proc macro doesn't support generic parameters with inline bounds Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant